C3P0 database connection pool technology

C3P0 is an open source JDBC connection pool, which implements data source and JNDI binding, supports JDBC3 specification and JDBC2 standard extension. c3p0 operates asynchronously, and slow JDBC operations are completed through the help process. Extending these operations can effectively improve performance. Currently open source projects using it include hibernate, spring, etc. It is a mature, highly concurrent JDBC connection pool library, used for caching and reuse of PreparedStatements support. c3p0 has the function of automatically recovering idle connections. hibernate3 began to recommend him

Method 1: Get database information directly in Java code and establish database connection pool

@Test
 public  void test () throws Exception { 
    ComboPooledDataSource cpds = new ComboPooledDataSource (); // Create a connection pool 
    cpds.setDriverClass ("com.mysql.jdbc.Driver"); // Database driver 
    cpds.setJdbcUrl ("jdbc: mysql : // localhost: 3306 / test "); // Database address 
    cpds.setUser (" root "); // User name 
    cpds.setPassword (" 123456 "); // Password 
    Connection conn = cpds.getConnection (); / / Get connection 
    System.out.println (conn); // Print connection 
}

Method 2: Obtain database information through configuration and establish database connection pool

@Test 
 public  static Connection getConnection2 () throws SQLException { 
    DataSource cpds = new ComboPooledDataSource ("myc3p0"); // myc3p0 is the same as the name attribute in the named-config tag in the configuration 
    Connection conn = cpds.getConnection (); // Get Connect 
    System.out.println (conn); // Print connection 
}    

The name of the configuration file placed in the src directory: c3p0-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <named-config name="myc3p0">
        <!-- 获取连接的4个基本信息 -->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name= "password" > 123456 </ property > 
' 

        <!- Setting of related properties related to the management of the database connection pool- > 
        <!- If there are not enough connections in the database, how many connections are applied to the database server at one time -> 
        < property name = "acquireIncrement" > 5 </ property > 
        <!- Number of connections when initializing the database connection pool- > 
        < property name = "initialPoolSize" > 5 </ property > 
        <!- database connection The minimum number of database connections in the pool- > 
        < property name = "minPoolSize">5</property > 
        <!- The maximum number of database connections in the database connection pool- > 
        < property name = "maxPoolSize" > 10 </ property > 
        <!- The number of Statements that the C3P0 database connection pool can maintain- > 
        < property name = "maxStatements" > 20 </ property > 
        <!- The number of Statement objects that can be used simultaneously for each connection- > 
        < property name = "maxStatementsPerConnection" > 5 </ property > 

    </ named-config > 
</c3p0-config>

 

Guess you like

Origin www.cnblogs.com/linglongfang/p/12693540.html