¼Ò¼³°°ÀºJSP2ºÎ  8Àå JDBC¿Í JSP  14 PoolingÀÇ È°¿ë  

 

8.14 PoolingÀÇ È°¿ë

 

ÀÌÁ¦ Connection Pool¿¡ °üÇÑ ¿¹Á¦¸¦ »ìÆì º¸°Ú½À´Ï´Ù. ¿¹Á¦¸¦ º¸±â Àü¿¡ ÀÌ ConnectionPool Ŭ·¡½ºµµ J2SDK¿¡¼­ Áö¿øÇϴ Ŭ·¡½º°¡ ¾Æ´Ï¶ó, ´ÜÁö ¹æ¹ý·ÐÀ» ÀÌ¿ëÇØ¼­ »ý¼ºÇغ» Ŭ·¡½ºÀÔ´Ï´Ù. ½º½º·Î Á» ´õ ¹ßÀüµÈ Connection PoolÀ» ¸¸µé¾îº¸´Â °Íµµ ÁÁÀº ¹æ¹ýÀ̶ó »ý°¢ÇÕ´Ï´Ù.

 

PoolingTest.jsp

Pooling Ŭ·¡½º¸¦ Å×½ºÆ®Çϴ ¿¹Á¦

<%@ page import "java.sql.*, org.jabook.sql.*" contentType= "text/html;charset = euc-kr"%> 
<html><body>
<h3>ConnectionPool Class Test</h3>
<hr> 
<% 
  ConnectionPool pool = ConnectionPool.getInstance(); 
  Connection conn = pool.getConnection(); 
  
if (conn != null) { 
  Statement stmt = conn.createStatement(); 
  ResultSet rs = stmt.executeQuery(
"SELECT * FROM MyTest"); 
%>
MyTest Å×ÀÌºí¿¡¼­ ResultSet °¡Á®¿À±â<br> 
  <table border = 
"1" cellspacing="0" cellpadding="1"
  <tr bgcolor = 
"pink"
  <td><b>name</b></td> 
  <td><b>age</b></td> 
  </tr> 
<%
    
while(rs.next()){ 
%>
  <tr> 
    <td><%=rs.getString(
1)%></td> 
    <td><%=rs.getInt(
2)%></td> 
  </tr> 
<% 
  }
    rs.close(); 
    stmt.close(); 
    pool.releaseConnection(conn); 
  }
%>
</table>
</body></html>

ConnectionPool.java

Pooling Ŭ·¡½º

package org.jabook.sql;
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.MSSQL);
    System.out.println(
"== a connection was created"
);
    
return 
conn;
  }
}

C:\jakarta\webapps\MySample\WEB-INF\classes>javac d . ConnectionPool.java

 

¸ÕÀú JSP ÆÄÀÏ¿¡¼­ ConnectionPoolingÀÇ °´Ã¼ poolÀ» ¾ò±â À§ÇØ ConnectionPool Ŭ·¡½ºÀÇ getInstance ¸Þ¼­µå¸¦ È£ÃâÇÏ¿´½À´Ï´Ù. ÀÌ ¶§ »ý¼ºµÈ °´Ã¼°¡ ¾øÀ¸¸é °´Ã¼¸¦ »ý¼ºÇϰí ÀÖÀ¸¸é °´Ã¼¸¦ ¹ÝȯÇÕ´Ï´Ù. °´Ã¼¸¦ »ý¼ºÇÒ ¶§ ¸Å°³º¯¼ö¸¦ ÁÖ¾î Ãʱ⿡ »ý¼ºÇÒ ConnectionÀÇ °³¼ö¿Í Max Connection °³¼ö¸¦ ÁöÁ¤Çϰí ÀÖ½À´Ï´Ù.

 

n       public static synchronized ConnectionPool getInstance() throws SQLException {

n          if (cp == null) {

n             cp = new ConnectionPool(4,20);

n          }

n             return cp;

n       }

 

 ÀÌ·¸°Ô ConnectionPoolÀÇ °´Ã¼ poolÀ» ¾ò¾î ¿À¸é ConnectionÀ» ¾ò¾î ¿À±â À§ÇØ pool.getConnection ¸Þ¼­µå¸¦ È£ÃâÇÕ´Ï´Ù.

 

n       Connection conn = pool.getConnection();

 

 ConnectionPooling Ŭ·¡½ºÀÇ getConnection ¸Þ¼­µå¸¦ È£ÃâÇÏ¸é ³»ºÎÀûÀÎ ÀÛ¿ëÀ» °ÅÃÄ ConnectionÀ» ³Ñ°ÜÁÝ´Ï´Ù. getConnection ¸Þ¼­µå´Â pool¿¡ ¸¸µé¾îÁ® ÀÖ´Â ConnectionÀÌ ÀÖ´ÂÁö ¾ø´ÂÁö¸¦ È®ÀÎÇϰí ÀÖ´Ù¸é Connection °´Ã¼¸¦ ¹ÝȯÇϰí, ¾ø´Ù¸é ¾Õ¿¡¼­ ¼³¸íÇÑ ConnectionFactory Ŭ·¡½º¸¦ »ç¿ëÇÏ¿© Connection °´Ã¼¸¦ »ý¼ºÇÕ´Ï´Ù.

 

n       public synchronized Connection getConnection() throws SQLException {

n          Connection conn = null;

n          while ((conn = getPooledConnection()) == null) {

n             try {

n                wait();

n             } catch (InterruptedException ie) {}

n          }

n          users++;

n          return conn;

n       }

 

JSP ÆÄÀÏÀº Connection °´Ã¼¸¦ ¾ò¾î³½ ÈÄ ÀÚ±âÀÇ ÀÛ¾÷À» ¿Ï·áÇÕ´Ï´Ù. ÀÛ¾÷À» ¿Ï·áÇÑ ÈÄ Connection °´Ã¼¸¦ Á¦¿ÜÇÑ ´Ù¸¥ ¿¬°áÀ» Á¾·áÇÕ´Ï´Ù. Connection °´Ã¼´Â Á¾·á½ÃŰ´Â °ÍÀÌ ¾Æ´Ï°í, Connection Pool¿¡ ¹ÝȯÇÕ´Ï´Ù. Connection °´Ã¼¸¦ µ¹·Á¹ÞÀº Connection PoolÀº Connection °´Ã¼¸¦ ´Ù½Ã »ç¿ëÇÒ ¼ö ÀÖµµ·Ï ÀúÀåÀ» ÇÕ´Ï´Ù.

 

n       rs.close();

n       stmt.close();

n       pool.releaseConnection(conn);

 

ÇѲ¨¹ø¿¡ ¸¹Àº ÀÛ¾÷ÀÌ ÀϾ °¡´É¼º¿¡ ´ëºñÇØ¼­ ¸Þ¼­µå¿¡ synchronized Ű¿öµå¸¦ »ç¿ëÇß½À´Ï´Ù. ±×¸®°í, wait, notify ¸Þ¼­µåµµ »ç¿ëÇÏ¿© µ¿±âÈ­ ¹®Á¦¸¦ ÇØ°áÇÏ¿´½À´Ï´Ù. ´õ ÀÚ¼¼ÇÑ ³»¿ëÀ» º¸½Ã·Á¸é ÀÚºÏȨÆäÀÌÁö(www.jabook.org)¿¡¼­ ¼Ò¼³ °°Àº ÀÚ¹Ù 19Àå JDBCÆíÀ» Âü°í ÇϽñ⠹ٶø´Ï´Ù.



jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:¼Ò¼³°°ÀºJSP2ºÎ
ÀúÀÚ:ÃÖ¿µ°ü