Several ways to configure data sources in Java

Source: Internet
Author: User
Tags time in milliseconds

Spring applications are often deployed to Java EE application servers, such as Webshpere,jboss,resin or web containers such as Tomcat, which allow you to configure data sources to be fetched through Jndi. Often encountered in the project configuration data source, according to the work of learning and book learning, now summed up, is a bit of their own experience it! This paper takes resin as an example
1. Using Jndi data sources
The essence of this approach is to configure the Web container's configuration file, which, as the application starts, connects to the data source, and in a Java application, gets the corresponding data source by configuring the Jndi name of the data source.
For example:

<Database>            <Jndi-name>Db/demo1</jndi-name>            <Drivertype="Net.sourceforge.jtds.jdbc.Driver">            <Url>Jdbc:jtds:sqlserver://192.168.0.0:18991;databasename=aaaaa</url>            <User>Xxx</user>            <Password>******</password>            </driver>            <Prepared-statement-cache-size>8</prepared-statement-cache-size>            <Max-connections> -</Max-connections>            <Max-idle-time> -S</Max-idle-time></database><Database>            <Jndi-name>Db/demo2</jndi-name>            <Drivertype="Net.sourceforge.jtds.jdbc.Driver">                   <Url>Jdbc:jtds:sqlserver://192.168.0.1:18991;databasename=bbbbbb</url>            <User>Xxx</user>            <Password>******</password>            </driver>            <Prepared-statement-cache-size>8</prepared-statement-cache-size>            <Max-connections> -</Max-connections>            <Max-idle-time> -S</Max-idle-time></database>

This configuration needs to be configured in the configuration file of the resin container, which can be used to obtain the corresponding data source connection using the Jndi-name configured data source name in Java application development.
2. Using a data source connection pool
The main configuration is configured in the spring configuration file, in this case the DBCP data source connection pool, such as Spring-context.xml.

<bean id= "dataSource" class=" Org.apache.commons.dbcp.BasicDataSource " destroy-ethod=" Close ">              < property 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: Initialize connection --        < property name="InitialSize" value="6"/>            <!--maxidle: Maximum idle connection --        < property name="Maxidle" value="Ten"/>        <!--minidle: Minimum idle connection --        < property name="Minidle" value="6"/>        <!--maxactive: Maximum number of connections --        < property name="maxactive" value="/> "        <!--removeabandoned: Whether the auto-recycle timeout is connected --        < property name="removeabandoned" value="true"/ >        <!--removeabandonedtimeout: Time-out (in seconds) -        < property name="Removeabandonedtimeout" value="/>"         <!--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" value="1"/ >        < property name="Testonborrow">            <value>True</value>        </Property >        < property name="Testonreturn">            <value>True</value>        </Property >        < property name="Testwhileidle">            <value>True</value>        </Property >        < property name="Validationquery">            <value>SELECT 1</value>        </Property >    </Bean>

3. JDBC-driven data sources
This configuration is actually the same as the above 2 in the configuration and configuration parameters in the same way, that is, when the driver adopted a different driver type, using the most basic JDBC database connection pool.

Several ways to configure data sources in Java

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.