C3P0一个服务器多个数据库

时间:2017-12-21 08:09:12

标签: java mysql connection-pooling c3p0

早上好,

正如标题所说,我想创建一个解决方案,使用连接池连接到同一个Mysql服务器上的不同数据库。我已按照以下帖子中指出的步骤进行操作:

https://stackoverflow.com/questions/26785842/multiple-data-sources-for-c3p0

我的解决方案是:

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的项目。

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

问候。

1 个答案:

答案 0 :(得分:2)

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

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