c3p0简介及简单案例
版权声明:本文为博主原创文章,欢迎转载,转载请注明本文链接! https://blog.csdn.net/qq_38238041/article/details/82826214 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-cd6c485e8b.css">
<div class="htmledit_views" id="content_views">
<p>c3p0应该说是十分常用的连接池了,很常用的dao层框架hibernate都是用c3p0的</p>
他的用法相当简便,当然,要懂一点连接数据库的知识作为前提,直接上代码了。
至于jar包,可以直接百度,也可以到这里下载:Java连接数据库的工具类
下面是基本用法
- package cn.bl.v4_DataSource.c3p0;
-
- import java.sql.SQLException;
-
- import org.junit.Test;
-
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class Demo1 {
-
- /**
- * 方式1:使用纯Java方式
- * @throws Exception
- */
- @Test
- public void test1() throws Exception {
- ComboPooledDataSource source = new ComboPooledDataSource();
- source.setDriverClass("com.mysql.jdbc.Driver");
- source.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=utf-8");
- source.setUser("root");
- source.setPassword("000");
-
- //获取链接
- System.out.println(source.getConnection());
-
- /*输出如下信息(除了最后一行,前面的其实都是自动输出的基本信息):
- 九月 23, 2018 11:40:45 上午 com.mchange.v2.log.MLog <clinit>
- 信息: MLog clients using java 1.4+ standard logging.
- 九月 23, 2018 11:40:45 上午 com.mchange.v2.c3p0.C3P0Registry banner
- 信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
- 九月 23, 2018 11:40:45 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
- 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgf2td9y193nu61qxzvdh|160847b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgf2td9y193nu61qxzvdh|160847b, idleConnectionTestPeriod -> 0, initialPoolSize -> 2, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=utf-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 30, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 2, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
- com.mchange.v2.c3p0.impl.NewProxyConnection@d64342
- */
- }
-
- @Test
- public void test2() throws SQLException {
- //1.空参--默认配置
- ComboPooledDataSource ds = new ComboPooledDataSource();
-
- //2.带参--指定名称的配置
- //ComboPooledDataSource ds = new ComboPooledDataSource("BarryLee");
-
- System.out.println(ds.getConnection());
- //输出的信息类似test1
- }
-
- /*
- * 测试c3p0工具类
- */
- @Test
- public void testUtil() {
- System.err.println(C3P0Utils.getConnection());
- }
- }
下面是配置文件,配置文件一定到放置到src下,命名为:c3p0-config.xml
- <c3p0-config>
- <!-- 默认配置,如果没有指定则使用这个配置 -->
- <default-config>
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <property name="jdbcUrl">
- <![CDATA[jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=UTF-8]]>
- </property>
- <property name="user">root</property>
- <property name="password">000</property>
- <!-- 初始化池大小 -->
- <property name="initialPoolSize">2</property>
- <!-- 最大空闲时间 -->
- <property name="maxIdleTime">30</property>
- <!-- 最多有多少个连接 -->
- <property name="maxPoolSize">10</property>
- <!-- 最少几个连接 -->
- <property name="minPoolSize">2</property>
- <!-- 每次最多可以执行多少个批处理语句 -->
- <property name="maxStatements">50</property>
- </default-config>
- <!-- 命名的配置 -->
- <named-config name="BarryLee">
- <property name="driverClass">com.mysql.jdbc.Driver</property>
- <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/aa</property>
- <property name="user">root</property>
- <property name="password">000</property>
- <property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 -->
- <property name="initialPoolSize">100</property>
- <property name="minPoolSize">50</property>
- <property name="maxPoolSize">1000</property>
- <property name="maxStatements">0</property>
- <property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
- </named-config>
- </c3p0-config>
下面是一个工具类
- package cn.bl.v4_DataSource.c3p0;
-
- import java.sql.Connection;
- import java.sql.SQLException;
-
- import javax.sql.DataSource;
-
- import com.mchange.v2.c3p0.ComboPooledDataSource;
-
- public class C3P0Utils {
- private C3P0Utils() {}
- private static ComboPooledDataSource ds = null;
- private static ThreadLocal<Connection>tLocal = new ThreadLocal<>();
- static {
- ds = new ComboPooledDataSource();//读取默认配置文件
- }
- public static DataSource getDataSource() {
- return ds;
- }
- public static Connection getConnection() {
- Connection con = tLocal.get();
- if(con==null) {
- try {
- con = ds.getConnection();
- tLocal.set(con);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return con;
- }
- }

浙公网安备 33010602011771号