Presentation is loading. Please wait.

Presentation is loading. Please wait.

Connection Pooling 2001/4/3 Kang Seungwoo. Connection Pooling Database Connection cost is very high Connection Pool DB 와 연결된 여러 개의 Connection 객체를 미리 확보.

Similar presentations


Presentation on theme: "Connection Pooling 2001/4/3 Kang Seungwoo. Connection Pooling Database Connection cost is very high Connection Pool DB 와 연결된 여러 개의 Connection 객체를 미리 확보."— Presentation transcript:

1 Connection Pooling 2001/4/3 Kang Seungwoo

2 Connection Pooling Database Connection cost is very high Connection Pool DB 와 연결된 여러 개의 Connection 객체를 미리 확보 개별 servlet, JSP 는 직접 DB 에 연결 하는 것 아니라 Connection Pool 로부터 Connection 객 체를 받아와 사용하고 사용 후에는 다시 반 납

3 Connection pool 을 사용하면 유용한 경우 사용자들이 제한된 수의 일반 database 사용 자 계정을 통해 database 에 접근하는 경우 Database 연결이 하나의 요청 동안만 사용되 는 경우

4 Connection pool model PoolManager client: int instance: PoolManager drivers: Vector pools: Hashtable getInstance: PoolManager getConnection: Connection freeConnection: void release: void ConnectionPool checkedout: int freeConnections: Vector maxConns: int name: String password: String timeOut: int URL: String user: String getConnection: Connection freeConnection: void release: void java.sql.Connection

5 ConnectionPool class 생성자  public ConnectionPool(String name, String URL, String user, String password, int maxConns, int initConns, int timeOut, PrintWriter pw, int logLevel) { … initPool(initConns); … } 초기 연결 열기  Private void initPool(int initConns) { … Connection pc = newConnection(); freeConnections.addElement(pc); … }

6 풀에서 연결 구하기  public Connection getConnection() throws SQLException{ … return getConnection(timeOut * 1000); … }  private synchronized Connection getConnection(long timeout) throws SQLException { … while((conn = getPooledConnection()) == null) { … wait(remaining); } … checkedOut++; return conn; }

7  private Connection getPooledConnection() throws SQLException { if (freeConnections.size() > 0) { Connection conn = (Connection)freeConnections.firstElement(); freeConnections.removeElementAt(0); } else if (maxConns == 0 || checkedOut < maxConns) { conn = newConnection(); } return conn; }  private Connection newConnection() throws SQLException { Connection conn=DriverManager.getConnection(URL, user, password); … return conn; }

8 풀에 연결 리턴하기  public synchrozied void freeConnection(Connection conn){ freeConnection.addElement(conn); checkedOut--; notifyAll(); … } Shut down  public synchronized void release() { Enumeration allConnections = freeConnections.elements(); … Connection con = (Connection) allConnections.nextElement(); con.close(); … freeConnections.removeAllElements(); }

9 PoolManager class JDBC driver 를 로드하고 등록한다. ConnectionPool 객체를 만든다. 연결 풀 이름과 ConnectionPool 객체를 대응시 킨다. Client 요청을 명명되어 있는 특정 ConnectionPool 에 전달해 준다. 연결 풀을 계속 추적, 유지해서 마지막 클라이 언트가 수행을 마쳤을 때 모든 풀을 Shut down 시킨다.

10 연결 풀을 사용하는 servlet 의 동작 init() 에서 PoolManager.getInstance() 를 호출 service() 에서 PoolManager.getConnection() 을 호출, database 동작 수행, freeConnection() 을 사용하여 Connection 을 풀에 리턴 destroy() 에서 PoolManager.release() 를 호출

11 Reference Professional Java Server Programming Source file ftp://ftp.wrox.com/Professional/2777/chap09.zip com/wrox/connectionpool/ConnectionPool.java PoolManager.java


Download ppt "Connection Pooling 2001/4/3 Kang Seungwoo. Connection Pooling Database Connection cost is very high Connection Pool DB 와 연결된 여러 개의 Connection 객체를 미리 확보."

Similar presentations


Ads by Google