c3p0连接池的学习

c3p0的学习 学习 学习 !

     首先c3p0的基本了解,在之前的web项目学习中都是直接使用jdbc的 DriverManager进行数据库的连接,每次执行操作都会建立一个新的连接,在操作完成后,通过判断释放连接,但是如果处于高并发的情况下,就可能会造成服务器崩溃的后果,因为大量的资源同一时间得不到释放或者大量连接请求导向数据库,都会导致数据库宕机;采用c3p0连接池会控制连接池内的连接对象,缓冲对数据库的请求。以下是c3p0的xml配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <c3p0-config>
 3     <default-config>
 4         <property name="user">*</property>
 5         <property name="password">*</property>
 6         <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
 7         <property name="jdbcUrl">jdbc:mysql://*:3306/mysql?useSSL=false</property>
 8         <!-- user:用户名,password:密码,driverClass为ORACLE数据库驱动,jdbcUrl是连接数据库的URL -->
 9         <!--连接池中的连接耗尽时c3p0再次获取的连接数目 -->
10         <property name="acquireIncrement">2</property>
11         <!--初始化时连接池存在的连接数,取值应在minPoolSize与maxPoolSize之间 -->
12         <property name="initialPoolSize">5</property>
13         <!--连接池中最小连接数 -->
14         <property name="minPoolSize">1</property>
15         <!--连接池中最大连接数 -->
16         <property name="maxPoolSize">5</property>
17         <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。
18         但由于预缓存的statements属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
19         如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。默认为0 -->
20         <property name="maxStatements">5</property>
21         <!--maxStatementsPerConnection 连接池内单个连接所拥有的最大缓存statements数。默认为0 -->
22         <property name="maxStatementsPerConnection">5</property>
23     </default-config>
24 </c3p0-config>

如果没有指定位置那么这个配置文件的名字是固定的:

我在调试的时候,因为忘记更换名字,没有使用这个默认的名字,导致一直报错:java.sql.SQLException: No suitable driver,控制台打印的连接池参数也是默认的,导致无法连接,这点需要注意。

 开始c3p0的简单代码学习,项目结构:

commons-dbutils 是 Apache提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。

 1 package com.company;
 2 
 3 import com.mchange.v2.c3p0.ComboPooledDataSource;
 4 import org.apache.commons.dbutils.QueryRunner;
 5 import java.sql.SQLException;
 6 
 7 public class Main {
 8 
 9     private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
10 
11     public static void main(String[] args) throws SQLException {
12         QueryRunner runner = new QueryRunner(dataSource);
13         String sql = "insert into entity values (1,'张三','这个人很懒')";
14         runner.update(sql);
15     }
16 }

启动时控制台会打印配置信息:

1 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1br2pxta9lo1etmcxs9gj|5a2e4553, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.cj.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1br2pxta9lo1etmcxs9gj|5a2e4553, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql://*:3306/mysql?useSSL=false, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 5, maxStatements -> 5, maxStatementsPerConnection -> 5, minPoolSize -> 1, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]

如果上述打印信息正确,说明你的配置信息被正确的导入连接池。执行结果如下:

 另外官方提供了几种配置文件的方式:

 我这里采用的是XML方式的配置:

Overriding c3p0 defaults via c3p0-config.xml

You can use the XML config file for all c3p0 configuration, including configuration of defaults, named configurations, per-user overrides, and configuration extensions.

By default, c3p0 will look for an XML configuration file in its classloader's resource path under the name "/c3p0-config.xml". That means the XML file should be placed in a directly or jar file directly named in your applications CLASSPATH, in WEB-INF/classes, or some similar location.

也可以使用properties的方式:

 官网文档地址:https://www.mchange.com/projects/c3p0/#contents

其实多看看官方外文文档,比乱找解决方法要简单的多。

原文地址:https://www.cnblogs.com/YsirSun/p/7821516.html