简体   繁体   English

单例不适合获取 JDBC 连接吗? 连接池有什么好的实现吗?

[英]Is singleton not good for get JDBC connection? Any good implementation of connection pooling?

I used singleton pattern to get jdbc connection for my standalone application.我使用单例模式为我的独立应用程序获取 jdbc 连接。 The code is as following.代码如下。

public static synchronized MysqlConnect getDbCon() {
    if ( db == null ) {
        db = new MysqlConnect();
    }
    return db;
}

But I saw in many discussions singleton in not good for getting connection.但是我在许多讨论中看到单身人士不利于获得联系。 Is it true?这是真的吗? And the suggested using connection pooling.并建议使用连接池。 Can anyone give me a good implementation of connection pooling to use instead of the above code?谁能给我一个很好的连接池实现来代替上面的代码?

Here's a simple, singleton based implementation of a connection pool.这是一个简单的、基于单例的连接池实现。 This connection pool supports getting a connection using either the DriverManager interface or via a DataSource (JNDI).此连接池支持使用 DriverManager 接口或通过数据源 (JNDI) 获取连接。

I use this connection pool for some of my projects (though the actual implementation is slightly more complex)我在我的一些项目中使用了这个连接池(虽然实际实现稍微复杂一些)

public class ConnectionPool{
private String jdbcDriver;
private String jdbcURL;
private String user;
private String password;
private int connectionCount=10;
private  List<Connection> connections;  
private static boolean loadFromDataSource;
private static DataSource dataSource;
private  static ConnectionPool connPool;


private ConnectionPool() throws EasyORMException{}

private void setConnection(String jdbcDriver, String jdbcURL, String user, String password,String dbDataSource) throws EasyORMException{

    this.jdbcDriver = jdbcDriver;
    this.jdbcURL=jdbcURL;
    this.user=user;
    this.password=password;

    connections= new ArrayList<Connection>();   
    connections.add((ConnectionPool.loadFromDataSource) ? getConnectionFromDataSource() : getConnection());     
}
static ConnectionPool getInstance(){ 
    return connPool; 
}
private static ConnectionPool getInstanceFromJndi(String propertyFile,boolean loadFromJndi) throws EasyORMException{
    ConnectionProp cp=readPropFromFile(propertyFile);

    if(loadFromJndi){
        dataSource=createDatasource(cp.getDataSource());
        loadFromDataSource=true;             
    }
    return ConnectionPool.createConnectionPool(cp.getJdbcDriver(),cp.getDbURL(), cp.getUsername(), cp.getPassword(),cp.getDataSource())
}
public static ConnectionPool getInstance(String propertyFile,boolean loadFromJndi) throws EasyORMException{

    return ConnectionPool.getInstanceFromJndi(propertyFile, loadFromJndi, false);
}

public static ConnectionPool getInstance(ConnectionProp cp) throws EasyORMException{
    return ConnectionPool.createConnectionPool(cp.getJdbcDriver(),cp.getDbURL(), cp.getUsername(), cp.getPassword(),cp.getDataSource());
}
public static ConnectionPool getInstance(String jndiName) throws EasyORMException{ 

    dataSource=createDatasource(jndiName);
    loadFromDataSource=true;
    return ConnectionPool.createConnectionPool(null,null, null, null,jndiName);
}
public static ConnectionPool getInstance(String jdbcDriver, String jdbcURL, String user, String password) throws EasyORMException{ 

    return ConnectionPool.createConnectionPool(jdbcDriver,jdbcURL, user, password,null);
}
private static ConnectionPool createConnectionPool(String jdbcDriver, String jdbcURL, String user, String password,String dbDataSource) throws EasyORMException{
    if(connPool==null)  {   
        connPool = new ConnectionPool();
        connPool.setConnection(jdbcDriver, jdbcURL, user, password, dbDataSource);
    }
    return connPool; 
}

synchronized Connection getAvailableConnection() throws EasyORMException { 
    Connection conn=null; 
    int connSize = connections.size(); 
    if(connSize>0){ 
        conn=connections.remove(connSize-1); 
    }else{ 
        if(connSize<connectionCount){ 
            for(int i=0;i<initialConnCount;i++) 
                conn=(ConnectionPool.loadFromDataSource)?getConnectionFromDataSource() :getConnection(); 
        }else{ 
                throw new EasyORMException(EasyORMException.CONNECTION_NUM_EXCEEDED); 
            } 
    } 
    return conn; 
} 

synchronized void returnConnection(Connection conn){
    connections.add(conn);
}


private Connection getConnection() throws EasyORMException {

    Connection conn=null;
    try {
        Class.forName(jdbcDriver);
        conn = DriverManager.getConnection(jdbcURL, user, password);
    } catch (ClassNotFoundException e) {
        throw new EasyORMException(e);
    } catch (SQLException e) {
        throw new EasyORMException(e);
    }

    return conn;
}
private  Connection getConnectionFromDataSource() throws EasyORMException  {
    try{
        return dataSource.getConnection();
    }catch(SQLException e){
        throw new EasyORMException(e);
    }
}

public void setNumberOfConnections(int count){
    this.connectionCount=count;
}
public int getNumberOfConnections(){
    return connectionCount;
}
private static DataSource createDatasource(String jndiDb)throws EasyORMException{
    InitialContext initCtx=null;
    try {
        initCtx = new InitialContext();
        return (DataSource)initCtx.lookup("java:comp/env/"+jndiDb);
    } catch (NamingException e) {
        throw new EasyORMException(e);
    }finally{
        if(initCtx!=null){
            try {
                initCtx.close();
            } catch (NamingException e) {
                throw new EasyORMException(e);
            }
        }
    }
}
}

Some methods don't have public access (because they're written as part of a library) but you can change them to public if you need to.有些方法没有公共访问权限(因为它们是作为库的一部分编写的),但如果需要,您可以将它们更改为公共。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM