嗨我已经为我的Java Stand alone程序创建了连接池。
好的更清楚..
private static ComboPooledDataSource = pooledDataSource;
public static void createDataSource(String dbName)
{
if(dbName.equals("DB_ONE"))
{
pooledDataSource = new ComboPooledDataSource();
// Here passing parameters will be different for DB_ONE
pooledDataSource.setDriverClass(dbDriver);
pooledDataSource.setJdbcUrl(dbUrl);
pooledDataSource.setUser(userID);
pooledDataSource.setPassword(password);
pooledDataSource.setMinPoolSize(Integer.parseInt(minCon));
pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon));
pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize));
pooledDataSource.setPreferredTestQuery(testQuery);
}
if(dbName.equals("DB_TWO"))
{
pooledDataSource = new ComboPooledDataSource();
// Here passing parameters will be different for DB_TWO
pooledDataSource.setDriverClass(dbDriver);
pooledDataSource.setJdbcUrl(dbUrl);
pooledDataSource.setUser(userID);
pooledDataSource.setPassword(password);
pooledDataSource.setMinPoolSize(Integer.parseInt(minCon));
pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon));
pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize));
pooledDataSource.setPreferredTestQuery(testQuery);
}
}
public Connection getConnection(String dbName)
{
createDataSource(dbName);
return pooledDataSource.getConnection();
}
每当我调用此方法getConnection(dbName)
在两个不同的数据库之间频繁地进行切换..对于每个连接调用,再次创建特定数据库池。
我不想创建重复的池......
如何避免创建新的connection pool
?
任何帮助 谢谢你。
答案 0 :(得分:0)
查看内部方法:
return pooledDataSource.getConnection();
无论何时调用方法,您都要创建新连接。只需输入pooledDataSource.getConnection();内部静态内容:
//change private variable to Connection
private Connection connection
static{
pooledDataSource = new ComboPooledDataSource();
pooledDataSource.setDriverClass(dbDriver);
pooledDataSource.setJdbcUrl(dbUrl);
pooledDataSource.setUser(userID);
pooledDataSource.setPassword(password);
pooledDataSource.setMinPoolSize(Integer.parseInt(minCon));
pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon));
pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize));
pooledDataSource.setPreferredTestQuery(testQuery);
//here is new line
connection = pooledDataSource.getConnection();
}
并在方法中执行:
public Connection getConnection()
{
return connection;
}
答案 1 :(得分:0)
也许你可以考虑这样的事情:
private static Map<String, ComboPooledDataSource> dataSources;
static {
dataSources = new Map<String, ComboPooledDataSource>();
}
public static void createDataSource(String dbName) {
ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
if(dbName.equals("DB_ONE")) {
// Here passing parameters will be different for DB_ONE
pooledDataSource.setDriverClass(dbDriver);
pooledDataSource.setJdbcUrl(dbUrl);
pooledDataSource.setUser(userID);
pooledDataSource.setPassword(password);
pooledDataSource.setMinPoolSize(Integer.parseInt(minCon));
pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon));
pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize));
pooledDataSource.setPreferredTestQuery(testQuery);
dataSources.put(dbName, pooledDataSource);
} else if(dbName.equals("DB_TWO")) {
// Here passing parameters will be different for DB_TWO
pooledDataSource.setDriverClass(dbDriver);
pooledDataSource.setJdbcUrl(dbUrl);
pooledDataSource.setUser(userID);
pooledDataSource.setPassword(password);
pooledDataSource.setMinPoolSize(Integer.parseInt(minCon));
pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon));
pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize));
pooledDataSource.setPreferredTestQuery(testQuery);
dataSources.put(dbName, pooledDataSource);
}
}
public synchronized Connection getConnection(String dbName) {
if (!dataSources.containsKey(dbName)) {
createDataSource(dbName);
}
return dataSources.get(dbName).getConnection();
}
使用此解决方案,您只能在需要时创建数据源(但在创建后保持活动状态),并且可以从所需的数据源访问连接,而无需创建新池。