C3P0 一机多库

C3P0 one server multiple databases

早上好,

正如标题所说,我想创建一个解决方案来使用连接池连接到同一 Mysql 服务器上的不同数据库。我已按照以下 post:

中指示的步骤进行操作

我的解决方案是这样的:

public class DatabaseUtility
{
        public static ComboPooledDataSource getDataSource(String db, String user, String pass) throws PropertyVetoException
        {
                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;
        }

        public static void main(String[] args) throws SQLException
        {
                Connection connection = null;
                PreparedStatement pstmt = null;
                ResultSet resultSet = null;
                try
                {
                        //QUERY to DATABASE 1
                        ComboPooledDataSource dataSource = DatabaseUtility.getDataSource("bd1", "user1", "pass1");
                        connection = dataSource.getConnection();
                        pstmt = connection.prepareStatement("SELECT * FROM municipio");

                        System.out.println("The Connection Object is of Class: " + connection.getClass());

                        resultSet = pstmt.executeQuery();
                        while (resultSet.next())
                        {
                                System.out.println(resultSet.getString(1) + "," + resultSet.getString(2) + "," + resultSet.getString(3));
                        }


                        //QUERY to DATABASE 2
                        dataSource = DatabaseUtility.getDataSource("bd2", "user2", "pass2");
                        connection = dataSource.getConnection();
                        pstmt = connection.prepareStatement("SELECT * FROM alojamiento");

                        System.out.println("The Connection Object is of Class: " + connection.getClass());

                        resultSet = pstmt.executeQuery();
                        while (resultSet.next())
                        {
                                System.out.println(resultSet.getString(1) + "," + resultSet.getString(2) + "," + resultSet.getString(3));
                        }

                }
                catch (Exception e)
                {
                        connection.rollback();
                        e.printStackTrace();
                }
        }
}

每次连接到数据库时,我都会使用特定的连接字符串调用构造函数。

但是,我怀疑这是否是一个有效的解决方案,或者我做错了什么......

P.S。我正在使用一个没有 Spring.

的项目

非常感谢您的建议,提前致谢,

你好。

不,这绝对不是一个有效的解决方案。每次你想要获取一个连接时,你都在创建一个新的整个连接池。尝试这样的事情。

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;
}