본문 바로가기

IT 서비스 바라보기/Programming

게시판 만들기 -5- (커넥션 풀 부분)



커넥션풀을 만들어주는 부분

ConnectionPool.java

----------------------------------------------------------------------------------

package mydb.poolfact ;
import java.sql.*;
import java.util.*;


public class ConnectionPool {
  private static ConnectionPool cp = null;
  private ConnectionFactory cf = null;
  private Vector pool = null;
  private int initCon = 0;  
  private int maxCon = 0;
  private int users = 0;
  private ConnectionPool(){}

  private ConnectionPool(int initCon, int maxCon) throws SQLException {
    this.initCon = initCon;
    this.maxCon = maxCon;
    cf = new ConnectionFactory();
    pool = new Vector();
    for (int i=0; i < initCon; i++) {
      pool.add(createConnection());
    }
  }


  public static synchronized ConnectionPool getInstance() throws SQLException {
    if (cp == null) {
      cp = new ConnectionPool(4,20);
    }
    return cp;
  }

  //
  public synchronized Connection getConnection() throws SQLException {
    Connection conn = null;
    while ((conn = getPooledConnection()) == null)
    {
      try {
        wait();
      } catch (InterruptedException ie) {}
    }
    users++;
    return conn;
  }


  public synchronized void releaseConnection(Connection conn) {
    pool.add(conn);
    users--;
    notifyAll();
  }


  private Connection getPooledConnection() throws SQLException {
    Connection conn = null;
    int size = pool.size();
    if (size > 0) {
      conn = (Connection)(pool.elementAt(0));
      pool.removeElementAt(0);
    }  else if (users < maxCon || maxCon == 0) {
      pool.add(createConnection());
    }
    return conn;
  }


  private Connection createConnection()  throws SQLException {
   Connection conn = cf.getConnection(ConnectionFactory.MYSQL);
  // Connection conn = cf.getConnection(ConnectionFactory.ORACLE);
    System.out.println("== a connection was created");
    return conn;
  }
}

--------------------------------------------------------------------------------

필요한 부분만 설명하자면

  private int initCon = 0;          //초기연결자수
  private int maxCon = 0;       //최대 연결자수
  private int users = 0;         //유저수


  private ConnectionPool(int initCon, int maxCon) throws SQLException {  

//초기연결자수와 최대수를 인자로 받아서
    this.initCon = initCon;
    this.maxCon = maxCon;

// this로 넣어주고
    cf = new ConnectionFactory();
    pool = new Vector();
    for (int i=0; i < initCon; i++) {

//만약 초기연결자수보다 작다면
      pool.add(createConnection());

//풀을 계속 생성
    }
  }


  public static synchronized ConnectionPool getInstance() throws SQLException {
    if (cp == null) {

//cp... 처음 'private static ConnectionPool cp = null;' 로 선언했고 그게 null이라면
      cp = new ConnectionPool(4,20);

//ConnectionPool을 최소4 최대20까지 늘려라
    }
    return cp;
  }


----------------------------------------------------------------------------

이상 4개의 java를 만들어보았다.

변수를 담을수있는 벡터형빈과 그냥빈...

커넥션풀부분과 접속을 당담해주는 ConnectionFactory.java...

여기까지 완성했다면 요리를 하기 앞서 필요한 양념과 소스들이 준비된거라 보면 되겠다.

앞으로 요리를 하면서 여기서 필요한 소스를 아무때나 끌어쓸수있게 준비해놓은거라고

생각하면 되겠다.




출처 : http://blog.naver.com/midniteblaze/10016477037