连接池的好处(C0P3测试)
来源:互联网 发布:cad制图软件 编辑:程序博客网 时间:2024/04/30 09:59
连接池能够使性能最大化,同时还能将资源利用控制在一定的水平之下,如果超过该水平,应用程序将崩溃而不仅仅是变慢。
C3P0连接管理器
package cn.c3p0;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;import com.mchange.v2.c3p0.ComboPooledDataSource;public final class ConnectionManager { private static ConnectionManager instance; private static ComboPooledDataSource dataSource; private ConnectionManager() throws SQLException, PropertyVetoException { dataSource = new ComboPooledDataSource(); dataSource.setUser("hai"); dataSource.setPassword("1223"); dataSource.setJdbcUrl("jdbc:oracle:thin:@192.168.10.38:1521:ztsys"); dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver"); dataSource.setInitialPoolSize(5); dataSource.setMinPoolSize(1); dataSource.setMaxPoolSize(10); dataSource.setMaxStatements(50); dataSource.setMaxIdleTime(60); } public static final ConnectionManager getInstance() { if (instance == null) { try { instance = new ConnectionManager(); } catch (Exception e) { e.printStackTrace(); } } return instance; } public synchronized final Connection getConnection() { Connection conn = null; try { conn = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; }}
测试例子:
package cn.c3p0;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import oracle.jdbc.pool.OracleDataSource;public class ConnectionDemo { public static void main(String[] args) throws SQLException { System.out.println("使用连接池................................"); for (int i = 0; i < 20; i++) { long beginTime = System.currentTimeMillis(); Connection conn = ConnectionManager.getInstance().getConnection(); try { PreparedStatement pstmt = conn.prepareStatement("select * from event t"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { // do nothing... } } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime)); } System.out.println("不使用连接池................................"); for (int i = 0; i < 20; i++) { long beginTime = System.currentTimeMillis(); OracleDataSource ods = new OracleDataSource(); ods.setUser("hai"); ods.setPassword("1223"); ods.setURL("jdbc:oracle:thin:@192.168.10.38:1521:ztsys"); Connection conn = ods.getConnection(); try { PreparedStatement pstmt = conn.prepareStatement("select * from event t"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { // do nothing... } } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime)); } }}
测试结果:
使用连接池................................
2011-8-23 14:23:10 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
2011-8-23 14:23:10 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2011-8-23 14:23:10 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 -> 1hge3728h1w2au7f1c496sy|6e293a, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> oracle.jdbc.driver.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge3728h1w2au7f1c496sy|6e293a, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:oracle:thin:@192.168.10.38:1521:ztsys, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 60, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
第1次执行花费时间为:1031
第2次执行花费时间为:172
第3次执行花费时间为:266
第4次执行花费时间为:156
第5次执行花费时间为:172
第6次执行花费时间为:156
第7次执行花费时间为:172
第8次执行花费时间为:141
第9次执行花费时间为:156
第10次执行花费时间为:172
第11次执行花费时间为:172
第12次执行花费时间为:156
第13次执行花费时间为:172
第14次执行花费时间为:172
第15次执行花费时间为:172
第16次执行花费时间为:171
第17次执行花费时间为:157
第18次执行花费时间为:156
第19次执行花费时间为:187
第20次执行花费时间为:157
不使用连接池................................
第1次执行花费时间为:187
第2次执行花费时间为:188
第3次执行花费时间为:187
第4次执行花费时间为:188
第5次执行花费时间为:187
第6次执行花费时间为:203
第7次执行花费时间为:188
第8次执行花费时间为:187
第9次执行花费时间为:188
第10次执行花费时间为:203
第11次执行花费时间为:203
第12次执行花费时间为:281
第13次执行花费时间为:188
第14次执行花费时间为:219
第15次执行花费时间为:203
第16次执行花费时间为:203
第17次执行花费时间为:187
第18次执行花费时间为:219
第19次执行花费时间为:219
第20次执行花费时间为:203
- 连接池的好处(C0P3测试)
- spring+hibernate的c0p3连接池配置
- 测试驱动开发的好处
- 测试驱动开发的好处
- 测试组尽早介入的好处?
- 线程池的好处
- 数据库连接池的好处
- 数据库连接池的好处
- 线程池的好处
- 数据库连接池的好处
- 线程池的好处
- 数据库连接池的好处
- 使用连接池有什么好处?
- Java线程池的好处
- 使用线程池的好处
- 使用线程池的好处
- Android的线程池好处
- 使用线程池的好处
- 关于sharepoint 503错误
- 纯虚析构函数
- OSI模型和TCP/IP的结构图
- 2、arm启动代码分析
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
- 连接池的好处(C0P3测试)
- 工作效率与Context Switch
- Android中实现整个视图切换的左右滑动效果
- 依赖倒转原则——里氏代换原则
- web开发中我曾经不注意的
- java.lang.OutOfMemoryError: GC overhead limit exceeded解决办法
- java 枚举类型解惑
- Web标准网页调用Flash方法
- Js 中如果判断Null 跟Undefined