¼Ò¼³ java.sql ¼Ò½ºÄڵ堠Advance JDBC ±¸Çö  È®ÀåµÈ ConnectionPooling±â¹ý  

È®ÀåµÈ ConnectionPooling±â¹ý »ç¿ëÀÇ ¿¹

ConnectionPool1.java

import java.sql.*;
import 
java.util.*;
public class 
ConnectionPool1 {
  
private static ConnectionPool1 cp = null
;
  
private ConnectionFactory1 cf = null
;
  
private Vector pool = null
;
  
private int initCon = 0
;  
  
private int maxCon = 0
;
  
private int users = 0
;
  
private ConnectionPool1(int initCon, int maxCon) throws 
SQLException {
    
this
.initCon = initCon;
    
this
.maxCon = maxCon;

    cf = 
new 
ConnectionFactory1();
    pool = 
new 
Vector();
    
for (int i=0
; i < initCon; i++) {
      pool.add(createConnection());
    }
  }
  
public static synchronized ConnectionPool1 getInstance() throws 
SQLException {
    
if (cp == null
) {
      cp = 
new ConnectionPool1(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(ConnectionFactory1.ODBC);
    System.out.println(
"== a connection was created"
);
    
return 
conn;
  }
}

C:\JavaExample\19>javac ConnectionPool1.java

Ä¿³Ø¼ÇÇ®ÀÇ È®ÀåÀ¸·Î, Ä¿³Ø¼ÇÀÇ »ý¼º°³¼ö¸¦ Á¦ÇÑÇÏ°í ±× °³¼ö ÀÌ»óÀÇ ¿äûÀÌ µé¾î¿À¸é ³ª¸ÓÁö ¿äûµéÀº »ç¿ë °¡´ÉÇÑ Ä¿³Ø¼ÇÀÌ »ý±æ¶§ ±îÁö wait()¸¦ »ç¿ëÇØ¼­ ´ë±â ½ÃŲ´Ù.



jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:¼Ò¼³°°Àº¹Ì´ÏÄÚµå
ÀÛ¼ºÀÚ:Àںϸâ¹ö ÇÑâÇå