微信登录

c++ mysql 连接池

官网已经炸了
https://mvnrepository.com/

引入文件

c3p0-0.9.1.2.jar

测试

  1. package com.atguigu4.connection;
  2. import java.beans.PropertyVetoException;
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import org.junit.Test;
  6. import com.mchange.v2.c3p0.ComboPooledDataSource;
  7. import com.mchange.v2.c3p0.DataSources;
  8. public class C3P0Test {
  9. //方式一:
  10. @Test
  11. public void testGetConnection() throws Exception{
  12. //获取c3p0数据库连接池
  13. ComboPooledDataSource cpds = new ComboPooledDataSource();
  14. cpds.setDriverClass( "com.mysql.jdbc.Driver" );
  15. cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test" );
  16. cpds.setUser("root");
  17. cpds.setPassword("abc123");
  18. //通过设置相关的参数,对数据库连接池进行管理:
  19. //设置初始时数据库连接池中的连接数
  20. cpds.setInitialPoolSize(10);
  21. Connection conn = cpds.getConnection();
  22. System.out.println(conn);
  23. //销毁c3p0数据库连接池
  24. // DataSources.destroy( cpds );
  25. }
  26. //方式二:使用配置文件
  27. @Test
  28. public void testGetConnection1() throws SQLException{
  29. ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");
  30. Connection conn = cpds.getConnection();
  31. System.out.println(conn);
  32. }
  33. }

配置文件

src/c3p0-config.xml
xml文件名不要改

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <c3p0-config>
  3. <named-config name="hellc3p0">
  4. <!-- 提供获取连接的4个基本信息 -->
  5. <property name="driverClass">com.mysql.jdbc.Driver</property>
  6. <property name="jdbcUrl">jdbc:mysql:///test</property>
  7. <property name="user">root</property>
  8. <property name="password">abc123</property>
  9. <!-- 进行数据库连接池管理的基本信息 -->
  10. <!-- 当数据库连接池中的连接数不够时,c3p0一次性向数据库服务器申请的连接数 -->
  11. <property name="acquireIncrement">5</property>
  12. <!-- c3p0数据库连接池中初始化时的连接数 -->
  13. <property name="initialPoolSize">10</property>
  14. <!-- c3p0数据库连接池维护的最少连接数 -->
  15. <property name="minPoolSize">10</property>
  16. <!-- c3p0数据库连接池维护的最多的连接数 -->
  17. <property name="maxPoolSize">100</property>
  18. <!-- c3p0数据库连接池最多维护的Statement的个数 -->
  19. <property name="maxStatements">50</property>
  20. <!-- 每个连接中可以最多使用的Statement的个数 -->
  21. <property name="maxStatementsPerConnection">2</property>
  22. </named-config>
  23. </c3p0-config>

使用

放在JDBCutil.java
不会关掉连接池

  1. //数据库连接池只需提供一个即可。
  2. private static ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");
  3. public static Connection getConnection1() throws SQLException{
  4. Connection conn = cpds.getConnection();
  5. return conn;
  6. }
c++ mysql 连接池