Configure a data source in spring

Source: Internet
Author: User
Tags jboss

Several common ways to configure data sources in spring are:

#mysql Database Configuration (jdbc.properties) jdbc.driverclassname=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql: // localhost:3306/databasename?useunicode=true&characterencoding=utf-8jdbc.username=  Rootjdbc.password=root

1. Use spring's own data source Org.springframework.jdbc.datasource.DriverManagerDataSource;

Way One:

<bean id= "Propertyconfigurer"  class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "  p:location="/web-inf/ Jdbc.properties "/><bean id=" DataSource "   class=" Org.springframework.jdbc.datasource.DriverManagerDataSource "    p:driverclassname=" ${ Jdbc.driverclassname} "    p:url=" ${jdbc.url} "    p:username=" ${jdbc.username} "     P:password= "${jdbc.password}"/>

P Mark Required: xmlns:p= "http://www.springframework.org/schema/p"

Drivermanagerdatasource Source code implementation:

 Public classDrivermanagerdatasourceextendsAbstractdriverbaseddatasource { PublicDrivermanagerdatasource () {} Publicdrivermanagerdatasource (String url) {} Publicdrivermanagerdatasource (string URL, string username, string password) {} Publicdrivermanagerdatasource (String URL, Properties conprops) {} Public voidsetdriverclassname (String driverclassname) {}protectedConnection Getconnectionfromdriver (Properties props)throwsSQLException {}protectedConnection getconnectionfromdrivermanager (String URL, Properties props)throwsSQLException {}}

Way two:

<bean id= "DataSource"   class

2.dbcp data source ;

DBCP (DataBase connection pool). is a Java Connection pool project on Apache and a connection pool component used by Tomcat. Using DBCP alone requires 2 packages:Commons-dbcp.jar,commons-pool.jar because establishing a database connection is a very time-consuming and resource-intensive activity, a connection pool is pre-established with the database and placed in memory. Applications need to establish a database connection directly to the connection pool to apply for a line, run out and then put back.

class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "driverclassname" value= "${ Jdbc.driverclassname} "/> <property name=" url "value=" ${jdbc.url} "/> <property name=" username "value=" ${ Jdbc.username} "/> <property name=" password "value=" ${jdbc.password} "/></bean>

DBCP implementation:
classjdbcutil{Private StaticBasicdatasource BDS;Static{if(bds==NULL) {BDS=NewBasicdatasource ();}//Set connection parameters for the database, respectivelybds.setdriverclassname (); Bds.seturl (); Bds.setusername (); Bds.setpassword ();} Public StaticConnection getconnection () {returnbds.getconnection ();}

3. c3p0 Data Source; C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. The current use of it has hibernate,springsuch as Dependent jar Packages: C3p0-0.9.1.jar, C3p0-0.9.1.2.jar, C3p0-0.9.1-pre6.jarC3P0 and DBCP difference: DBCP does not automatically reclaim the function of idle connection C3P0 has the automatic recycle idle connection function
<bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" ><property name= "Driverclass" >< Value>oracle.jdbc.driver.oracledriver</value></property><property name= "JdbcUrl" ><value >jdbc:oracle:thin: @localhost: 1521:test</value></property><property name= "User" ><value >root</value></property><property name= "Password" ><value>root</value></ property><!--The minimum number of connections that are kept in the connection pool. --><property name= "minpoolsize" value= "/><!--the maximum number of connections that are kept in the connection pool. Default:15--><property name= "maxpoolsize" value= "1800"/><!--maximum idle time, and the connection is discarded if not used in the same period. If 0, it will never be discarded. default:0--><property name= "maxidletime" value= "1800"/><!--when the connection in the connection pool is exhausted c3p0 the number of connections that are fetched at the same time. Default:3--><property name= "Acquireincrement" value= "3"/><property name= "maxStatements" value= "1000"/ ><property name= "initialpoolsize" value= "/><!--check idle connections in all connection pools every 60 seconds. default:0--><property name= "IdleconnectiontEstperiod "value="/><!--defines the number of repeated attempts to obtain a new connection from the database after a failure. default:30--><property name= "acquireretryattempts" value= "/><property name=" Breakafteracquirefailure "value=" true "/><property name=" Testconnectiononcheckout "value=" false "/></ Bean>

4. jndi data source;If the application is configured on a high-performance application server, such as WebLogic or WebSphere, we may prefer to use the data source provided by the application server itself. The data source for the application server is used by Jndi open callers, and spring specifically provides the Jndiobjectfactorybean class that references the Jndi resource. The following is a simple configuration:
Way One:
class= "Org.springframework.jndi.JndiObjectFactoryBean" >              <property name= "Jndiname" value= "Java : COMP/ENV/JDBC/BBT "/>          </bean>
Value values are the names of Jndi     

Mode two :

class= "Org.springframework.jndi.JndiObjectTargetSource" > <property name= "jndiname" > <value >queue/testQueue</value> </property></bean>

Way three:

If you do not use Jnditemplate to implement the INITIALCONTEXT environment variable configuration, you need to jndi.properties file (placed in classpath, usually under SRC), the content is as follows

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactoryjava.naming.provider.url=JNP:/ /localhost:1099java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Mode four:

Use Jnditemplate to implement configuration of InitialContext environment variables, such as

class class= "Org.springframework.jndi.JndiTemplate" ><property name= "Environment" ><props>< Prop key= "java.naming.factory.initial" >org.jnp.interfaces.namingcontextfactory</prop><prop key= " Java.naming.provider.url ">JNP://localhost:1099</prop><prop key=" java.naming.factory.url.pkgs ">org.jboss.naming:org.jnp.interfaces</prop></props></property ></bean>

Reference:

http://blog.csdn.net/orclight/article/details/8616103

Configure a data source in spring

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.