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连接数据库的工具类

下面是基本用法

  1. package cn.bl.v4_DataSource.c3p0;
  2. import java.sql.SQLException;
  3. import org.junit.Test;
  4. import com.mchange.v2.c3p0.ComboPooledDataSource;
  5. public class Demo1 {
  6. /**
  7. * 方式1:使用纯Java方式
  8. * @throws Exception
  9. */
  10. @Test
  11. public void test1() throws Exception {
  12. ComboPooledDataSource source = new ComboPooledDataSource();
  13. source.setDriverClass("com.mysql.jdbc.Driver");
  14. source.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=utf-8");
  15. source.setUser("root");
  16. source.setPassword("000");
  17. //获取链接
  18. System.out.println(source.getConnection());
  19. /*输出如下信息(除了最后一行,前面的其实都是自动输出的基本信息):
  20. 九月 23, 2018 11:40:45 上午 com.mchange.v2.log.MLog <clinit>
  21. 信息: MLog clients using java 1.4+ standard logging.
  22. 九月 23, 2018 11:40:45 上午 com.mchange.v2.c3p0.C3P0Registry banner
  23. 信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
  24. 九月 23, 2018 11:40:45 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
  25. 信息: 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 ]
  26. com.mchange.v2.c3p0.impl.NewProxyConnection@d64342
  27. */
  28. }
  29. @Test
  30. public void test2() throws SQLException {
  31. //1.空参--默认配置
  32. ComboPooledDataSource ds = new ComboPooledDataSource();
  33. //2.带参--指定名称的配置
  34. //ComboPooledDataSource ds = new ComboPooledDataSource("BarryLee");
  35. System.out.println(ds.getConnection());
  36. //输出的信息类似test1
  37. }
  38. /*
  39. * 测试c3p0工具类
  40. */
  41. @Test
  42. public void testUtil() {
  43. System.err.println(C3P0Utils.getConnection());
  44. }
  45. }

下面是配置文件,配置文件一定到放置到src下,命名为:c3p0-config.xml

  1. <c3p0-config>
  2. <!-- 默认配置,如果没有指定则使用这个配置 -->
  3. <default-config>
  4. <property name="driverClass">com.mysql.jdbc.Driver</property>
  5. <property name="jdbcUrl">
  6. <![CDATA[jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=UTF-8]]>
  7. </property>
  8. <property name="user">root</property>
  9. <property name="password">000</property>
  10. <!-- 初始化池大小 -->
  11. <property name="initialPoolSize">2</property>
  12. <!-- 最大空闲时间 -->
  13. <property name="maxIdleTime">30</property>
  14. <!-- 最多有多少个连接 -->
  15. <property name="maxPoolSize">10</property>
  16. <!-- 最少几个连接 -->
  17. <property name="minPoolSize">2</property>
  18. <!-- 每次最多可以执行多少个批处理语句 -->
  19. <property name="maxStatements">50</property>
  20. </default-config>
  21. <!-- 命名的配置 -->
  22. <named-config name="BarryLee">
  23. <property name="driverClass">com.mysql.jdbc.Driver</property>
  24. <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/aa</property>
  25. <property name="user">root</property>
  26. <property name="password">000</property>
  27. <property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 -->
  28. <property name="initialPoolSize">100</property>
  29. <property name="minPoolSize">50</property>
  30. <property name="maxPoolSize">1000</property>
  31. <property name="maxStatements">0</property>
  32. <property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
  33. </named-config>
  34. </c3p0-config>

下面是一个工具类

  1. package cn.bl.v4_DataSource.c3p0;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import javax.sql.DataSource;
  5. import com.mchange.v2.c3p0.ComboPooledDataSource;
  6. public class C3P0Utils {
  7. private C3P0Utils() {}
  8. private static ComboPooledDataSource ds = null;
  9. private static ThreadLocal<Connection>tLocal = new ThreadLocal<>();
  10. static {
  11. ds = new ComboPooledDataSource();//读取默认配置文件
  12. }
  13. public static DataSource getDataSource() {
  14. return ds;
  15. }
  16. public static Connection getConnection() {
  17. Connection con = tLocal.get();
  18. if(con==null) {
  19. try {
  20. con = ds.getConnection();
  21. tLocal.set(con);
  22. } catch (SQLException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. return con;
  27. }
  28. }

 

posted @ 2019-06-22 22:19  ouyangyangyang  阅读(798)  评论(0)    收藏  举报