Configuration between spring and the database

Source: Internet
Author: User

Three ways to configure a data source in spring

1. Using Org.springframework.jdbc.datasource.DriverManagerDataSource
Configuration file:

<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname" ><value>${jdbc.driverClassName}</value></property>
<property name= "url" ><value>${jdbc.url}</value></property>
<property name= "username" ><value>${jdbc.username}</value></property>
<property name= "Password" ><value>${jdbc.password}</value></property>
</bean>

Description: A simple configuration comparison with spring's own data source. But:

2. Using Org.apache.commons.dbcp.BasicDataSource
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" >
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name= "url" >
<value>jdbc:oracle:thin: @localhost:1521:orcl</value>
</property>
<property name= "username" >
<value>test</value>
</property>
<property name= "Password" >
<value>test</value>
</property>
<property name= "Maxactive" >
<value>255</value>
</property>
<property name= "Maxidle" >
<value>2</value>
</property>
<property name= "Maxwait" >
<value>120000</value>
</property>
</bean>

Description: This is a recommended way to configure the data source, which really uses the connection pooling technology. However, additional jars need to be introduced, but it is still relatively hard for multiple concurrent applications that need to use a database connection pool.

However: DBCP in practice, there are bugs, in some cases will generate a lot of empty connections can not be released, HIBERNATE3.0 has abandoned its support.

2.1 This configuration method also has a comparison of the famous: C3p0 specific configuration method is:

<bean id= "datasourcelocal" name= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<!--Specify the drive to connect to the database
<property name= "Driverclass" value= "${jdbc.driverclassname}"/>
<!--specify url--> to connect to the database
<property name= "Jdbcurl" value= "${jdbc.url}"/>
<!--Specify the user name to connect to the database--
<property name= "user" value= "${jdbc.username}"/>
<!--specify a password to connect to the database--
<property name= "Password" value= "${jdbc.password}"/>
<!--Specifies the maximum number of connections that are kept in the connection pool. Default:15-->
<property name= "maxpoolsize" value= "${jdbc.maxpoolsize}"/>
<!--Specify the minimum number of connections that are kept in the connection pool--
<property name= "minpoolsize" value= "${jdbc.minpoolsize}"/>
<!--the number of initialization connections for the specified connection pool should be between Minpoolsize and Maxpoolsize. Default:3-->
<property name= "initialpoolsize" value= "${jdbc.initialpoolsize}"/>
<!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. Default:0-->
<property name= "MaxIdleTime" value= "${jdbc.maxidletime}"/>
<!--c3p0 The number of connections that are fetched at the same time when the connection in the connection pool is exhausted. Default:3-->
<property name= "acquireincrement" value= "${jdbc.acquireincrement}"/>
<!--JDBC Standard to control the number of preparedstatements loaded within the data source.
However, because the pre-cached statements belong to a single connection instead of the entire connection pool, setting this parameter takes into account a number of factors. If both maxstatements and maxstatementsperconnection are 0, The cache is closed. Default:0-->
<property name= "maxstatements" value= "${jdbc.maxstatements}"/>
<!--check for idle connections in all connection pools every 60 seconds. Default:0--
<property name= "Idleconnectiontestperiod" value= "${jdbc.idleconnectiontestperiod}"/>
</bean>

Note: Additional packages are required, and C3P0 are more resource-intensive and may be less efficient.

2.3

Proxool's negative evaluation is less, it is now recommended, and it also provides instant monitoring of connection pool status to facilitate the discovery of connection leaks.
The configuration is as follows:
1, in the spring configuration file, generally in the Applicationcontext.xml
<bean id= "Proxooldatasource" class= "Org.logicalcobwebs.proxool.ProxoolDataSource" >
<property name= "Driver" value= "${jdbc.connection.driverclassname}"/>
<property name= "Driverurl" value= "${jdbc.connection.url}"/>
<property name= "user" value= "${jdbc.connection.username}"/>
<property name= "Password" value= "${jdbc.connection.password}"/>
<!--test SQL execution Statement--
<property name= "Housekeepingtestsql" value= "${proxool.housekeepingtestsql}"/>
<!--minimum number of idle connections to keep (default 2)-
<property name= "Prototypecount" value= "${proxool.prototypecount}"/>
<!--Proxool automatically detects the time interval (in milliseconds) of each connection status, and detects that an idle connection is immediately recovered, and the timeout is destroyed by default of 30 seconds)-
<property name= "Housekeepingsleeptime" value= "${proxool.hoursekeepingsleeptime}"/>
<!--maximum active time (the thread will be killed over this time, default is 5 minutes)-
<property name= "Maximumactivetime" value= "${proxool.maximumactivetime}"/>
<!--maximum connection time (default 4 hours)--
<property name= "Maximumconnectionlifetime" value= "${proxool.maximumconnectionlifetime}"/>
<!--minimum number of connections (default 2)--
<property name= "Minimumconnectioncount" value= "${proxool.minimumconnectioncount}"/>
<!--maximum number of connections (default 5)-
<property name= "Maximumconnectioncount" value= "${proxool.maximumconnectioncount}"/>
<!---->
<property name= "Statistics" value= "${proxool.statistics}"/>
<!--aliases--
<property name= "Alias" Value= "${proxool.alias}"/>
<!---->
<property name= "Simultaneousbuildthrottle" value= "${proxool.simultaneous-build-throttle}"/>
</bean>
and inject it into the sessionfactory.
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "DataSource" ref= "Proxooldatasource"/>
</bean>
Porxool configuration file
--==--==--==--==--==<proxool.xml>==--==--==--==--==--==--==
<?xml version= "1.0" encoding= "UTF-8"?>
<proxool>
<alias>WMS</alias>
<driver-url>jdbc:postgresql://192.168.210.184:5432/wms</driver-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver-properties>
<property name= "user" value= "Wms_user"/>
<property name= "password" value= "WMS"/>
</driver-properties>
<minimum-connection-count>2</minimum-connection-count>
<maximum-connection-count>40</maximum-connection-count>
<simultaneous-build-throttle>20</simultaneous-build-throttle>
<prototype-count>2</prototype-count>
</proxool>
--==--==--==--==--==<proxool.xml>==--==--==--==--==--==--==
Configuration Description:
Alias-〉 database connection alias (name to be used in the program)
Driver-url-〉 Database Driver
Driver-class-〉 Driver Class
Driver-properties user and password for-〉 online database
Minimum-connection-count-〉 Minimum number of connections, it is recommended to set more than 0, to ensure the first time to connect
Maximum-connection-count-〉 Maximum number of connections, which throws an exception if the maximum number of connections is exceeded. Excessive number of connections, server CPU and memory performance consumption is very
Big.
Simultaneous-build-throttle-〉 Simultaneous maximum number of connections
Prototype-count-〉 the number of connections generated at one time.
Example: If Prototype-count is now set to 4, but now has 2 available connections, then
An attempt is made to create 2 more connections.
However, the maximum number of connections cannot be exceeded.
Maximum-active-time-〉 Connect Maximum time activity default 5 minutes
Maximum-connection-lifetime-〉 Connection Max Life time default 4 hours

3. Using Org.springframework.jndi.JndiObjectFactoryBean

<bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
<property name= "Jndiname" ><value>java:comp/env/jdbc/roseindiadb_local</value ></PROPERTY&G T
</bean>

Description: Jndiobjectfactorybean is able to obtain datasource through Jndi.

4. The other is the configuration of Spring integrated hibernate:

<!--<start id= "Bean_sessionfactory"/>-->
<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<property name= "Mappingresources" >
<list>
<value>Spitter.hbm.xml</value>
</list>
</property>
<property name= "Hibernateproperties" >
<props>
<prop key= "dialect" >org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<!--<end id= "Bean_sessionfactory"/>-->

<!--<start id= "Bean_hibernatetemplate"/>-->
<bean id= "Hibernatetemplate" class= "Org.springframework.orm.hibernate3.HibernateTemplate" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
<!--<end id= "Bean_hibernatetemplate"/>->

<context:component-scan base-package= "Com.habuma.spitter.persistence"/>

Configuration between spring and the database

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.