Is singleton not good for get JDBC connection? Any

2020-04-01 07:02发布

问题:

I used singleton pattern to get jdbc connection for my standalone application. 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?

回答1:

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).

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.