Spring JDBC--NOTE: C3P0 database connection pool or Druid connection pool using configuration grooming

Source: Internet
Author: User
Tags valid xmlns

Is the MAVEN configuration file: Pom.xml's code content:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.yiibai</groupId> <artifactid>rowmapper </artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <n ame>rowmapper</name> <url>http://maven.apache.org</url> <dependencies>      <dependency>    <groupId>junit</groupId>    <artifactId> junit</artifactid>    <version>4.12</version>         </dependency>           <dependency>        & nbsp;    <groupid>org.springframework</groupid&Gt             <artifactId>spring-core</artifactId>             <version>4.0.3.RELEASE</version>         </dependency>         <dependency>        & nbsp;    <groupId>org.springframework</groupId>             <artifactId>spring-context</artifactId>             < version>4.0.3.release</version>         </dependency>          <dependency>             <groupId> org.springframework</groupid>             <artifactId> spring-context-support</artifactid>           &NBSP <version>4.0.3.RELEASE</version>         </dependency>          <dependency>             <groupid >org.springframework</groupId>             <artifactId> spring-beans</artifactid>             <version>4.0.3.release </version>         </dependency>         <dependency>             <groupid>org.springframework</
groupid>             <artifactId>spring-aop</artifactId>             <version>4.0.3.RELEASE</version>          </dependency>         <dependency>             <groupId>org.springframework</groupId>              <artifactId>spring-expression</artifactId>              <version>4.0.3.RELEASE</version>         </ dependency>         <dependency>             <groupId>org.springframework</groupId>             < artifactid>spring-jdbc</artifactid>             <version> 4.0.3.release</version>         </dependency>          <dependency>             <groupId> org.springframework</groupid>             <artifactid>spring-test</artifactid>             <version>4.0.3. release</version>         </dependency>       
  <dependency>             <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>             <version>5.1.34</version>          </dependency>        <dependency>             <groupId>commons-logging</groupId>              <artifactId>commons-logging</artifactId>              <version>1.2</version>         </depeNdency> </dependencies> </project> 
Application-beans.xml's code content:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans
    Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!--initialization for data source-- <bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > <p Roperty name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://loc
        Alhost:3306/test?characterencoding=utf8&usessl=true "/> <property name=" username "value=" root "/> <property name= "Password" value= "123456"/> </bean> <!--Definition for Studentjdbctemplate b EAN--<bean id= "DAO" class= "DAO. Dao "> <property name=" dataSource "ref=" DataSource "/> </bean> </beans>

Dao class:

Package DAO;


Import Org.springframework.jdbc.core.support.JdbcDaoSupport;


public class Dao extends Jdbcdaosupport  {

	
}

Spring JDBC reference URL: Https://www.yiibai.com/springjdbc/preparedstatementsetter.html#article-start

Druid:maven Pom.xml Join

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactid>druid</artifactid >
        <version>1.0.2</version>
    </dependency>
C3p0:maven Pom.xml Join
   <dependency>
        <groupId>com.mchange</groupId>
        <artifactid>c3p0</artifactid >
        <version>0.9.5-pre10</version>
    </dependency>
Druid.xml content is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--one, register data source with Druid database connection pool--<bean id=" DataSource "class=" Com.alibaba.druid.pool. Druiddatasource > <!--basic configuration-<property name= "url" value= "${jdbc.url}" ></property> & Lt;property name= "Driverclassname" value= "${jdbc.driver}" ></property> <property name= "username" value= " ${jdbc.user} "></property> <property name=" password "value=" ${jdbc.password} "></property> & lt;! --Key configuration-<!--the number of physical connections that are established when initializing. Initialization occurs in the display call inIt method, or first time getconnection-<property name= "InitialSize" value= "3"/> <!--minimum number of connection pools-- Lt;property name= "Minidle" value= "2"/> <!--maximum number of connection pools--<property name= ' maxactive ' value= ' "/&G
     T <!--Configure get connection Wait time-out-<property name= "maxwait" value= "10000"/> <!--performance Configuration-<!-- Open Pscache, and specify the size of Pscache on each connection-<property name= "poolpreparedstatements" value= "true"/> <propert Y name= "maxpoolpreparedstatementperconnectionsize" value= "x"/> <!--other Configurations-<!--configuration interval how often do you check The idle connection that needs to be closed, in milliseconds-<property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!--Configure a connection in the pool Minimum time to live, in milliseconds-<property name= "Minevictableidletimemillis" value= "300000"/> <!--recommended configured to True, no shadow Performance and ensure safety. When applying for connection detection, if idle time is greater than Timebetweenevictionrunsmillis, perform validationquery to detect if the connection is valid. -<property name= "testWhileidle "value=" true "/> <!--This is recommended to be configured to true, to prevent access to the connection is not available, when requesting a connection to perform validationquery detect whether the connection is valid, this configuration will degrade performance. --<property name= "Testonborrow" value= "true"/> <!--when the connection is returned the Validationquery detects if the connection is valid, this configuration will degrade performance --<property name= "Testonreturn" value= "false"/> </bean> </beans>
C3p0.xml content is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--one, register data source with C3P0 connection pool-<bean id=" DataSource "class=" Com.mchange.v2.c3p0.Combo Pooleddatasource > <!--basic configuration-<property name= "Jdbcurl" value= "${jdbc.url}" &GT;&LT;/PROPERTY&G
     T <property name= "Driverclass" value= "${jdbc.driver}" ></property> <property name= "user" value= "${ Jdbc.user} "></property> <property name=" password "value=" ${jdbc.password} "></property> &lt ;! --Key configuration-<!--get three connections when initializing, value should be in MinpoolsizeBetween the maxpoolsize. Default:3---<property name= "Initialpoolsize" value= "3" ></property> <!--the minimum number of connections left in the connection pool. Default:2---<property name= "Minpoolsize" value= "2" ></property> <!--the maximum number of connections left in the connection pool. Default:15--<property name= "maxpoolsize" value= "></property> <!--when the connection in the connection pool is exhausted c3p0 once The number of connections taken.
     Default:3---<property name= "Acquireincrement" value= "3" ></property> <!--performance Configuration-- <!--controls the number of preparedstatements loaded within the data source. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0--<property name= "maxstatements" value= "8" ></property> <!--Maxstatementsperconne Ction defines the maximum number of cache statements that a single connection in a connection pool has. default:0--<property name= "Maxstatementsperconnection" value= "5" ></property> <!--maximum idle time, 18 If not used within 00 seconds, the connection is discarded. If 0, it will never be discarded. default:0--<property name= "MaxIdleTime" value= "1800" ></property> </bean> </beAns> 
From the top of the configuration can be seen in a lot of places are similar, but some places may function the same but the name is not the same, I here just to tidy up and at the same time with two, only in Spring.xml import <import resource= "C3p0.xml"/ > Time to switch, the actual situation only need a good

Common configuration items for Druid are as follows:

Configuration

Default value

Description

Name


The significance of configuring this property is that if there are multiple data sources, the monitoring

can be distinguished by name. If not configured, a name will be generated,

The format is: "datasource-" + system.identityhashcode (This)

Jdbcurl


The URL of the connection database is different from the database. For example:

Mysql:jdbc:mysql://10.20.153.104:3306/druid2

Oracle:jdbc:oracle:thin:@10.20.149.85:1521:ocnauto

Username

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.