-
Connection Pool을 생성하는 예제(기본)컴퓨터 2008. 1. 27. 20:22
import java.sql.*;
import java.util.*;public class ConnectionPool {
private static ConnectionPool cp = null;
private ConnectionFactory1 cf = null;
private Vector pool = null;private ConnectionPool() {
cf = new ConnectionFactory1();
pool = new Vector();
}public static synchronized ConnectionPool getInstance() {
if (cp == null) {
cp = new ConnectionPool();
}
return cp;
}public synchronized Connection getConnection() throws SQLException {
Connection conn = null;
int size = pool.size(); //pool에 저장되어 있는 connection의 수if (size > 0) { //사용 가능한 connection이 있다면 connection을 하나 꺼내옴
conn = (Connection)(pool.elementAt(0));
pool.removeElementAt(0);
}else {
// 사용 가능한 connection이 없다면 새로 만듬
conn = cf.getConnection(ConnectionFactory1.ODBC);
}
return conn;
}public synchronized void releaseConnection(Connection conn) {
pool.add(conn);
}}