我是靠谱客的博主 甜美微笑,这篇文章主要介绍C3P0连接池配置,现在分享给大家,希望可以做个参考。

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目Hibernate,Spring等。

闲来无事,学习了一下C3P0的配置和使用,特此把一些步骤和出现的问题记录一下:

1、JdbcUtils.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/** * 使用本类的方法,必须提供c3p0-copnfig.xml文件 * * @author C */ public class JdbcUtils { // 饿汉式 private static DataSource ds = new ComboPooledDataSource(); /** * 它为null表示没有事务 * 它不为null表示有事务 * 当开启事务时,需要给它赋值 * 当结束事务时,需要给它赋值为null * 并且在开启事务时,让dao的多个方法共享这个Connection */ private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); public static DataSource getDataSource() { return ds; } /** * dao使用本方法来获取连接 * * @return * @throws SQLException */ public static Connection getConnection() throws SQLException { /* * 如果有事务,返回当前事务的con 如果没有事务,通过连接池返回新的con */ Connection con = tl.get();// 获取当前线程的事务连接 if (con != null) { return con; } return ds.getConnection(); } /** * 开启事务 * @throws SQLException */ public static void beginTransaction() throws SQLException { Connection con = tl.get();//获取当前线程的事务连接 if (con != null) { throw new SQLException("已经开启了事务,不能重复开启!"); } con = ds.getConnection();//给con赋值,表示开启了事务 con.setAutoCommit(false);//设置为手动提交 tl.set(con);把当前事务连接放到tl中 } /** * 提交事务 * @throws SQLException */ public static void commitTransaction() throws SQLException { Connection con = tl.get();//获取当前线程的事务连接 if(con == null) throw new SQLException("没有事务不能提交!"); con.commit();//提交事务 con.close();//关闭连接 con = null;//表示事务结束! tl.remove(); } /** * 释放Connection * @param con * @throws SQLException */ public static void releaseConnection(Connection connection) throws SQLException { Connection con = tl.get();//获取当前线程的事务连接 if(connection != con) {//如果参数连接,与当前事务连接不同,说明这个连接不是当前事务,可以关闭! if(connection != null &&!connection.isClosed()) {//如果参数连接没有关闭,关闭之! connection.close(); } } } }
2、c3p0-copnfig.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?> <c3p0-config> <default-config> <property name="jdbcUrl"> jdbc:mysql://localhost:3306/cms </property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> </c3p0-config>
3、需要导入的jar包

  c3p0.jar   mysql-connector.jar  mchange-commons-0.2.jar

问题1:如果不加入mchange-commons-0.2.jar,会出现以下问题:

     Caused by: java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

4、进行单元测试(导入junit4库)

JdbcUtilsTest.java

复制代码
1
2
3
4
5
6
7
public class JdbcUtilTest { @Test public void jdbcUtils() throws SQLException { Connection con = JdbcUtils.getConnection(); System.out.println(con); } }
控制台输出以下内容:
复制代码
1
2
3
4
5
6
7
2014-8-13 9:17:30 com.mchange.v2.log.MLog <clinit> 信息: MLog clients using java 1.4+ standard logging. 2014-8-13 9:17:30 com.mchange.v2.c3p0.C3P0Registry banner 信息: Initializing c3p0-0.9.2-pre1 [built 27-May-2010 01:00:49 -0400; debug? true; trace: 10] 2014-8-13 9:17:30 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 -> 1bqv9u3931kv3noythxveh|1786e64, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1bqv9u3931kv3noythxveh|1786e64, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://localhost:3306/cms, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 2, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, statementDestroyerNumActiveThreads -> -1, statementDestroyerNumConnectionsInUseAllUsers -> -1, statementDestroyerNumConnectionsInUseDefaultUser -> -1, statementDestroyerNumConnectionsWithDeferredDestroyStatementsAllUsers -> -1, statementDestroyerNumConnectionsWithDeferredDestroyStatementsDefaultUser -> -1, statementDestroyerNumDeferredDestroyStatementsAllUsers -> -1, statementDestroyerNumDeferredDestroyStatementsDefaultUser -> -1, statementDestroyerNumIdleThreads -> -1, statementDestroyerNumTasksPending -> -1, statementDestroyerNumThreads -> -1, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ] <span style="color:#ff0000;">com.mchange.v2.c3p0.impl.NewProxyConnection@1e87719</span>
说明已经成功连接数据库,但是 在创建ComboPooledDataSource类时会输出一堆日志,而且竟然是用System.err.println输出的,实在是令人不爽,想办法把它干掉。

方法一:修改mchange-commons-java-0.2.3.3.jar中文件,没经过深入研究,不再赘述;

方法二:和Log4j配合使用(导入log4j.jar与log4j.properties);

log4j.properties

复制代码
1
2
3
4
5
6
7
8
log4j.rootLogger=WARN,c1 log4j.appender.c1=org.apache.log4j.ConsoleAppender log4j.appender.c1.layout=org.apache.log4j.PatternLayout log4j.appender.c1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m [%t] %c [%l]%n #log4j.appender.f1.File=/WEB-INF/logs/file.log #log4j.appender.f1=org.apache.log4j.DailyRollingFileAppender #log4j.appender.f1.layout=org.apache.log4j.PatternLayout #log4j.appender.f1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m [%t] %c [%l]%n
此时 ,控制台日志输出为

复制代码
1
com.mchange.v2.c3p0.impl.NewProxyConnection@c62c8
至此,整个C3P0连接池配置完成,可直接在dao中访问数据库。










最后

以上就是甜美微笑最近收集整理的关于C3P0连接池配置的全部内容,更多相关C3P0连接池配置内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部