欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简单的数据库连接池

程序员文章站 2022-05-31 14:17:56
...
public class DBConnectionManager {
  private final static  DBConnectionManager instance=new DBConnectionManager();
  private DBConnectionPool pool;

   public static DBConnectionManager getInstance() {
    return instance;
  }
    public Connection getConnection() throws SQLException{
    return pool.getConnection();
  }
   public void freeConnection(Connection con)  throws SQLException{
    pool.freeConnection(con);

  }
  private DBConnectionManager() {
    init();
  }
  private void init() {
    Configuration cfg=Configuration.getInstance();
    String db_driver=cfg.getValue("DB.DRIVER");
    String db_url=cfg.getValue("DB.URL");
    String db_user=cfg.getValue("DB.USER");
    String db_password=cfg.getValue("DB.PASSWORD");
    int db_maxConn=Integer.parseInt(cfg.getValue("DB.MAX_CONNECTIONS"));
    try {
      Class.forName(db_driver);
    }
    catch (ClassNotFoundException ex) {
      System.out.println(ex);
    }
    pool = new DBConnectionPool(db_url,db_user,db_password,db_maxConn);
  }

 

class DBConnectionPool {

  private Vector freeConnections = new Vector();
  private int maxConn;
  private int connNumb;

  private String URL;
  private String password;
  private String user;

  public DBConnectionPool(String URL, String user, String password,int maxConn) {
        this.URL = URL;
        this.user = user;
        this.password = password;
        this.maxConn = maxConn;
  }

  public synchronized void freeConnection(Connection con) {
        freeConnections.addElement(con);
        connNumb--;
        notifyAll();
  }

    public synchronized Connection getConnection() throws SQLException{
        Connection con = null;
        if (freeConnections.size() > 0) {
            con = (Connection) freeConnections.firstElement();
            freeConnections.removeElementAt(0);
            try {
                if (con.isClosed()) {
                    con = getConnection();
                }
            }
            catch (SQLException e) {
                  con = getConnection();
            }
        }
        else if (maxConn == 0 || connNumb < maxConn) {
            con = newConnection();
        }
        if (con != null) {
            connNumb++;
        }
        return con;
    }

    private Connection newConnection() throws SQLException{
        Connection con =DriverManager.getConnection(URL,user, password);
        return con;
    }
}

 

 

 

public class Configuration {
    private Properties properties;
    private final static Configuration cfg = new Configuration();

    private Configuration() {
        properties = new Properties();
        InputStream is = null;
        try {
            is = getClass().getResourceAsStream("/app.properties");
            properties.load(is);
        } catch (Exception exception) {
          System.out.println("Can't read the properties file. ");
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException exception) {
                // ignored
            }
        }
    }
        public static Configuration getInstance() {
      return cfg;
    }
        public String getValue(String key) {
        return properties.getProperty(key);
    }
}

 

相关标签: 线程