学习bonecp数据库连接池遇到的问题
数据库连接类:DBConnection
package com.xxw.db; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import com.jolbox.bonecp.BoneCP; import com.jolbox.bonecp.BoneCPConfig; public class DBConnection { private static BoneCP bone; static { Properties props = new Properties(); try { props.load(DBConnection.class.getClassLoader().getResourceAsStream( "bonecp.properties")); String driverName = props.getProperty("driverClass"); String jdbcUrl = props.getProperty("jdbcUrl"); String userName = props.getProperty("username"); String password = props.getProperty("password"); int partitionCount = Integer.parseInt(props .getProperty("partitionCount")); //int idleMaxAge = Integer.parseInt(props.getProperty("idleMaxAge")); int acquireIncrement = Integer.parseInt(props .getProperty("acquireIncrement")); int releaseHelperThreads = Integer.parseInt(props .getProperty("releaseHelperThreads")); int maxConnectionsPerPartition = Integer.parseInt(props .getProperty("maxConnectionsPerPartition")); int minConnectionsPerPartition = Integer.parseInt(props .getProperty("minConnectionsPerPartition")); //int idleConnectionTestPeriod = Integer.parseInt(props.getProperty("idleConnectionTestPeriod")); Class.forName(driverName); BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl(jdbcUrl); config.setUsername(userName); config.setPassword(password); //config.setIdleMaxAge(idleMaxAge); config.setPartitionCount(partitionCount); config.setAcquireIncrement(acquireIncrement); config.setReleaseHelperThreads(releaseHelperThreads); config.setMaxConnectionsPerPartition(maxConnectionsPerPartition); config.setMinConnectionsPerPartition(minConnectionsPerPartition); //config.setIdleConnectionTestPeriod(idleConnectionTestPeriod); bone = new BoneCP(config); } catch (Exception e) { e.printStackTrace(); } } private static Connection conn; public static Connection getConnection() throws SQLException { if (conn == null) { conn = bone.getConnection(); } return conn; } public static void closeConnection() throws SQLException { if (conn != null) { conn.close(); conn = null; } } public static void main(String[] args) { /* * try { System.out.println(DBConnection.getConnection()); * DBConnection.closeConnection();} catch (SQLException e) { * e.printStackTrace();} */ } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
......
每次需要连接的时候,就问连接池要,而不是把第一次获取的conn保存下来!!!!!!!!!!!!
public static Connection getConnection() throws SQLException {
......
每次需要连接的时候,就问连接池要,而不是把第一次获取的conn保存下来!!!!!!!!!!!!
......
每次需要连接的时候,就问连接池要,而不是把第一次获取的conn保存下来!!!!!!!!!!!!