当前位置:Gxlcms > 数据库问题 > 9、JDBC-C3P0

9、JDBC-C3P0

时间:2021-07-01 10:21:17 帮助过:10人阅读

xml version="1.0" encoding="UTF-8"?> <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>yofc</groupId> <artifactId>jdbc</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.3</version> </dependency> </dependencies> <build> <plugins> <!-- 指定jdk --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

使用C3P0获取连接,三种方式

1、实例化和配置 ComboPooledDataSource

@Test
public void testC3P0() throws Exception {
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
    cpds.setJdbcUrl("jdbc:mysql://192.168.8.136:3306/jdbc");
    cpds.setUser("root");
    cpds.setPassword("root");

    cpds.setMinPoolSize(5);
    cpds.setAcquireIncrement(5);
    cpds.setMaxPoolSize(20);

    // 打开 PreparedStatement 池
    cpds.setMaxStatements(180);
    cpds.setMaxStatementsPerConnection(10);

    // 获取连接
    System.out.println(cpds.getConnection());

    cpds.close();
}

2、使用配置文件方式

c3p0 DataSources 可以通过名为 c3p0.properties 的简单 java.util.Properties 文件,通过更高级的 HOCON 配置文件(例如application.conf,application.json)或 XML 格式 c3p0-config.xml 进行配置。

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <named-config name="helloc3p0">
        <!-- 指定连接数据源的基本属性 -->
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://192.168.8.136:3306/jdbc</property>
        <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
        <property name="acquireIncrement">5</property>
        <!-- 初始化数据库连接池时连接的数量 -->
        <property name="initialPoolSize">5</property>
        <!-- 数据库连接池中的最小的数据库连接数 -->
        <property name="minPoolSize">5</property>
        <!-- 数据库连接池中的最大的数据库连接数 -->
        <property name="maxPoolSize">10</property>
        <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
        <property name="maxStatements">20</property>
        <!-- 每个连接同时可以使用的 Statement 对象的个数 -->
        <property name="maxStatementsPerConnection">5</property>
    </named-config>
    <default-config>
        <property name="automaticTestTable">con_test</property>
        <property name="checkoutTimeout">30000</property>
        <property name="idleConnectionTestPeriod">30</property>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
        <property name="maxStatements">200</property>
        <user-overrides user="test-user">
            <property name="maxPoolSize">10</property>
            <property name="minPoolSize">1</property>
            <property name="maxStatements">0</property>
        </user-overrides>
    </default-config>
    <!-- This app is massive! -->
    <named-config name="intergalactoApp">
        <property name="acquireIncrement">50</property>
        <property name="initialPoolSize">100</property>
        <property name="minPoolSize">50</property>
        <property name="maxPoolSize">1000</property>
        <!-- intergalactoApp adopts a different approach to configuring statement caching -->
        <property name="maxStatements">0</property>
        <property name="maxStatementsPerConnection">5</property>
        <!-- he‘s important, but there‘s only one of him -->
        <user-overrides user="master-of-the-universe">
            <property name="acquireIncrement">1</property>
            <property name="initialPoolSize">1</property>
            <property name="minPoolSize">1</property>
            <property name="maxPoolSize">5</property>
            <property name="maxStatementsPerConnection">50</property>
        </user-overrides>
    </named-config>
</c3p0-config>
@Test
public void testC3poWithConfigFile() throws Exception {
    // named-config
    DataSource dataSource = new ComboPooledDataSource("helloc3p0");

    // 获取连接
    System.out.println(dataSource.getConnection());

    ComboPooledDataSource comboPooledDataSource = (ComboPooledDataSource) dataSource;
    // 获取配置
    System.out.println(comboPooledDataSource.getMaxStatements());
}

3、使用 DataSources 工厂类

@Test
public void testC3P0Factory() throws Exception {
    DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:mysql://192.168.8.136:3306/jdbc", "root", "root");
    Map overrides = new HashMap();
    overrides.put("maxStatements", "200");
    overrides.put("maxPoolSize", new Integer(50));
    DataSource ds_pooled = DataSources.pooledDataSource(ds_unpooled, "intergalactoApp", overrides);

    System.out.println(ds_pooled.getConnection());

    // 两张关闭方式
    DataSources.destroy(ds_pooled);
    if (ds_unpooled instanceof PooledDataSource) {
        PooledDataSource pds = (PooledDataSource) ds_unpooled;
        pds.close();
    } else {
        System.err.println("Not a c3p0 PooledDataSource!");
    }
}

 


官方文档

 

9、JDBC-C3P0

标签:map   junit   throw   targe   org   多少   .json   connect   rop   

人气教程排行