Java configuration data sources in several ways

Source: Internet
Author: User
Tags time in milliseconds

Spring applications are frequently deployed to web containers in Java EE application servers, such as Webshpere,jboss,resin or Tomcat, which allow you to configure access to the data source through Jndi. Often encountered in the project configuration data sources, based on the work of learning and book learning, now summed up, is a bit of their own experience it. This article takes resin as an example
1. Using a Jndi data source
This is essentially configuring the Web container's configuration file, as the application starts, connecting the data source, and in the Java application, obtaining the appropriate data source by configuring the Jndi name of the data source.
For example:

<database> <jndi-name>db/demo1</jndi-name> <driver type= "Net.sourceforge.jtds . jdbc. Driver "> <url>jdbc:jtds:sqlserver://192.168.0.0:18991;DatabaseName=aaaaa</url> < user>xxx</user> <password>******</password> </driver> <p Repared-statement-cache-size>8</prepared-statement-cache-size> <max-connections>20</max-conn ections> <max-idle-time>30s</max-idle-time> </database> <database> <       
            jndi-name>db/demo2</jndi-name> <driver type= "Net.sourceforge.jtds.jdbc.Driver" > <url>jdbc:jtds:sqlserver://192.168.0.1:18991;DatabaseName=bbbbbb</url> <user>xxx</use r> <password>******</password> </driver> <prepared-statement-c ache-size>8</prepared-statement-cache-size> <max-connections>20</max-connections> <max-idle- Time>30s</max-idle-time> </database>

This configuration needs to be configured in the Resin container's configuration file, and in Java application development you can obtain the corresponding data source connection using the Jndi-name configured data source name.
2. Using the data source connection pool
is configured primarily in the spring configuration file, in this case using a DBCP data source connection pool, such as Spring-context.xml.

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-ethod= "Close" > <prope Rty name= "Driverclassname" value= "Net.sourceforge.jtds.jdbc.Driver"/> <property name= "url" value= "Jdbc:jtds : Sqlserver://127.0.0.1:18992/demo "/> <property name=" username "value=" xxx "/> <property name= "Password" value= "* * *"/> <!--initialsize: Initializing connection--> <property name= "InitialSize" value= "6"/&gt    
        ;
        <!--maxidle: Maximum idle connection--> <property name= "Maxidle" value= "ten"/> <!--minidle: Min. idle connection--> <property name= "Minidle" value= "6"/> <!--maxactive: Maximum number of connections--> <property name= "maxactive "Value="/> <!--removeabandoned: Automatically recycle timeout connection--> <property name= "removeabandoned" true "/> <!--removeabandonedtimeout: Timeout time (in seconds)--> <property name=" Removeabandonedtimeout "value= "180"/>
        <!--maxwait: Timeout wait time in milliseconds 6000 milliseconds/1000 equals 6 seconds--> <property name= "maxwait" value= "60000"/> <property name= "Minevictableidletimemillis" value= "1"/> <property name= "Timebetweenevictionrunsmillis" V alue= "1"/> <property name= "Testonborrow" > <value>true</value> </proper
        ty> <property name= "Testonreturn" > <value>true</value> </property>
        <property name= "Testwhileidle" > <value>true</value> </property> <property name= "Validationquery" > <value>select 1</value> </property> &l T;/bean>

3. JDBC-driven data sources
This configuration is roughly the same as the above 2 in configuration and configuration parameters, where drivers are used with different drive types and the most basic JDBC database connection pool is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.