to configure a data source in an application
(1). Create a configuration file for the connection parameters in Classpath, such as Db-config.properties, as follows:
Driverclass=com.mysql.jdbc.driver url=jdbc:mysql://localhost:3306/zzp username=root
(2). Introduce a parameter configuration file in spring's configuration file, with the following code:
<!--configuring spring resource Files-- <bean id= "Propertyconfigurer" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Locations " > <list> <value>/WEB-INF/config/db-config.properties</value> </list> </property>
(3). Configure the data source (there are three ways, JDBC, DBCP, c3p0)
The way in which the A.JDBC is configured in this manner differs from the other two ways in which the JDBC connection database is used, and no connection pooling is used, so the efficiency and performance aspects are not the same as the other two, which are configured in this way:
<!--data Source- <bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource "> <property name=" driverclassname " value=" ${driverclass} "> </property> < Property name= "url" value= "${url}" > </property> <property name= "username" value= "${username}" ></property> <property name= "password" value= "${password}" ></property> </bean >
B. Configuration using DBCP ( reference article ):
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <prope Rty name= "Driverclassname" value= "Referreddriver"/> <property name= "url" value= "Referreddburl"/> <property name= "username" value= "dbloginuser"/> <property name= "password" value= "Dbloginuserpasswor D "/> <property name=" InitialSize "value=" 5 "/> <property name=" Maxidle "value=" + "/> <property name= "Minidle" value= "5"/> <property name= "maxactive" value= "/> <pr" Operty name= "removeabandoned" value= "true"/> <!--auto-reclaim time-out (in seconds)--<property name= "Remo Veabandonedtimeout "value="/> <!--timeout wait time in milliseconds and <property name= "maxwait" value= "3000 "/> <property name=" Defaultautocommit "value=" false "/> <property name=" Validationquery " ; <value>select 1</value> </property> <property name= "Testonborrow" > <value >true</value> </property> <property name= "Testonreturn" > <value> False</value> </property> </bean>
c. Configure using C3P0 ( reference article )
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <!--user name-- <property name= "user" value= "${username}"/> <!--user password--<property name= "PA ssWOrd "value=" ${password} "/> <property name=" Driverclass "value=" ${driver_class} "/> &L T;property name= "Jdbcurl" value= "${url}"/> <!--the maximum number of connections left in the connection pool. Default:---<property name= "maxpoolsize" value= "/> <!--the minimum number of connections left in the connection pool, default to:3--> <property name= "Minpoolsize" value= "2"/> <!--the number of connections in the connection pool is initialized, the value should be in Minpoolsize and maxpoolsize , the default is 3--> <property name= "Initialpoolsize" value= "2"/> <!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. Default value: 0--<property name= "MaxIdleTime" >60</property> <!--when connection pooling When the connection is exhausted, the client calls getconnection () to wait for the new connection time, and then throws the sqlexcept after the timeoutIon, if set to 0, waits indefinitely. Unit milliseconds. Default: 0--<property name= "Checkouttimeout" value= "Up"/> <!--when connected in pool When the c3p0 is exhausted, the number of connections fetched at one time. Default value: 3--<property name= "Acquireincrement" value= "2"/> <!--define repeated attempts after a new connection from the database fails The number of times. Default value: 30, less than or equal to 0 means unlimited--<property name= "acquireretryattempts" value= "0"/> <!--re The time interval of the attempt, by default: 1000 ms--<property name= "Acquireretrydelay" value= ""/> <!--off Commits uncommitted transactions when connected, defaults to False, closes the connection, rolls back uncommitted transactions--<property name= "Autocommitonclose" >false</property> ; <!--C3P0 will build a blank table named Test and test it with its own query statement. If this parameter is defined, then the attribute preferredtestquery will be ignored. You cannot do anything on this test sheet, it will be used only for C3P0 testing. Default value: null--<property name= "automatictesttable" >Test</property> <!--if False, getting a connection failure will cause all threads that wait for the connection pool to get the connection to throw an exception, but the data source is still valid and continues to attempt to get the connection the next time it calls Getconnection (). If set to true, then try toWhen a connection failure is obtained, the data source declares that it has been fractured and closed permanently. Default: False--> <property name= "Breakafteracquirefailure" >false</property> < !--Check for idle connections in all connection pools every 60 seconds. Default value: 0, not checked-<property name= "Idleconnectiontestperiod" >60</property> <!--C3 P0 the size of the global preparedstatements cache. If both maxstatements and maxstatementsperconnection are 0, the cache does not take effect, and if one is not 0, the statement's cache will take effect. If the default value: 0--> <property name= "maxstatements" >100</property> <!--Maxstatementspe Rconnection defines the maximum number of cache statements that a single connection in a connection pool has. Default value: 0--<property name= "Maxstatementsperconnection" ></property> </bean>
To configure a data source in a Web server
1. Use Tomcat as an example to configure the data source you need to add the following code to the Conf/context.xml:
<resource name= "Jndi/mydatasource" auth= "Container" type= "Javax.sql.DataSource" driverclassname= "Com.mysql.jdbc.Driver" url= "jdbc:mysql://localhost:3306/zwkj" username= "root" password= "root123! " Maxactive= "maxidle=" maxwait= "10000"/>
2. In the client application, get the data source in Jndi, in spring, for example, configure the following:
<bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" > <property name= " Jndiname "> <value>java:comp/env/jndi/myDataSource</value> </property> </ Bean>
In addition to the above, you can also use spring's JEE label configuration
Note: in the above two ways, if you do not get the data source properly after configuration, you can add the following code in the Web. xml file:
<resource-ref> <description>jndi datasource</description> <res-ref-name>jndi/ mydatasource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth >Container</res-auth> </resource-ref>
I did not add the above code in the Web. xml file under TOMCAT8, can test success, early in the TOMCAT6 test as if no error.