当前位置:编程学习 > JAVA >>

Tomcat 挂载 BoneCP, 如何配置?

我的项目中有一个访问BoneCP的类如下:

public final class DatabaseConnectionPool {

private static final Logger LOG = LoggerFactory.getLogger(DatabaseConnectionPool.class);
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("connection");
private static final String DRIVER = "driver";
private static final String URL = "url";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String MAX_CONNECTION = "max_connection";
private static BoneCP pool;

/**
 * 开启连接池
 */
public static void startup() {
try {
Class.forName(BUNDLE.getString(DRIVER));
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl(BUNDLE.getString(URL));
config.setUsername(BUNDLE.getString(USERNAME));
config.setPassword(BUNDLE.getString(PASSWORD));
config.setMaxConnectionsPerPartition(Integer.parseInt(BUNDLE.getString(MAX_CONNECTION)));
pool = new BoneCP(config);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.getMessage(), e);
throw new DatabaseException(e);
}
}

/**
 * 关闭连接池
 */
public static void shutdown() {
pool.shutdown();
}

/**
 * @return 数据库连接
 */
public static Connection getConnection() {
try {
//if (pool == null)  我项目调用getConnection(), 时发现整个BoneCP并没有启动。
// startup();
return pool.getConnection();
} catch (SQLException e) {
e.printStackTrace();
LOG.error(e.getMessage(), e);
throw new DatabaseException(e);
}
}

}

我项目调用getConnection(), 时发现整个BoneCP并没有启动。但不应该是每次访问数据库时都去启动连接池吧, 不应该是每次访问数据库完就去调用shutdown吧?  个人觉得应该Tomcat 启动时, 该类也会载入内存。 然后项止可以正常访问该类?
请问各牛人有没有好的做法?
--------------------编程问答-------------------- 亲们, 有人有遇见过吗?
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,