C3P0 一机多库
C3P0 one server multiple databases
早上好,
正如标题所说,我想创建一个解决方案来使用连接池连接到同一 Mysql 服务器上的不同数据库。我已按照以下 post:
中指示的步骤进行操作
不,这绝对不是一个有效的解决方案。每次你想要获取一个连接时,你都在创建一个新的整个连接池。尝试这样的事情。
private static ComboPooledDataSource createDataSource(String db, String user, String pass) {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setJdbcUrl("jdbc:mysql://X.X.X.X:3306/"+db);
cpds.setUser(user);
cpds.setPassword(pass);
// Optional Settings
cpds.setInitialPoolSize(5);
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxStatements(100);
return cpds;
}
private static final class DbUserPassword {
final String db;
final String user;
final String password;
DbUserPassword( String db, String user, String password ) {
this.db = db;
this.user = user;
this.password = password;
}
@Override
public boolean equals( Object o ) {
if ( o instanceof DbUserPassword ) {
DbUserPassword that = (DbUserPassword) o;
return ( this.db.equals( that.db ) && this.user.equals( that.user ) && this.password.equals( that.password ) );
}
else {
return false;
}
}
@Override
public int hashCode() {
return db.hashCode() ^ user.hashCode() ^ password.hashCode();
}
}
// MT: protected by class' lock
private static HashMap<DbUserPassword,ComboPooledDataSource> poolMap = new HashMap<>();
public static synchronized ComboPooledDataSource getDataSource(String db, String user, String pass) {
DbUserPassword dbup = new DbUserPassword( db, user, pass );
ComboPooledDataSource out = poolMap.get(dbup);
if ( out == null ) {
out = createDataSource( db, user, pass );
poolMap.put( dbup, out );
}
return out;
}
早上好,
正如标题所说,我想创建一个解决方案来使用连接池连接到同一 Mysql 服务器上的不同数据库。我已按照以下 post:
中指示的步骤进行操作不,这绝对不是一个有效的解决方案。每次你想要获取一个连接时,你都在创建一个新的整个连接池。尝试这样的事情。
private static ComboPooledDataSource createDataSource(String db, String user, String pass) {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setJdbcUrl("jdbc:mysql://X.X.X.X:3306/"+db);
cpds.setUser(user);
cpds.setPassword(pass);
// Optional Settings
cpds.setInitialPoolSize(5);
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxStatements(100);
return cpds;
}
private static final class DbUserPassword {
final String db;
final String user;
final String password;
DbUserPassword( String db, String user, String password ) {
this.db = db;
this.user = user;
this.password = password;
}
@Override
public boolean equals( Object o ) {
if ( o instanceof DbUserPassword ) {
DbUserPassword that = (DbUserPassword) o;
return ( this.db.equals( that.db ) && this.user.equals( that.user ) && this.password.equals( that.password ) );
}
else {
return false;
}
}
@Override
public int hashCode() {
return db.hashCode() ^ user.hashCode() ^ password.hashCode();
}
}
// MT: protected by class' lock
private static HashMap<DbUserPassword,ComboPooledDataSource> poolMap = new HashMap<>();
public static synchronized ComboPooledDataSource getDataSource(String db, String user, String pass) {
DbUserPassword dbup = new DbUserPassword( db, user, pass );
ComboPooledDataSource out = poolMap.get(dbup);
if ( out == null ) {
out = createDataSource( db, user, pass );
poolMap.put( dbup, out );
}
return out;
}