'JAVA/JSP'에 해당되는 글 71건
- 2008.10.06 Connection pooling
===============================================
소설같은 자바에서. Connection Factory 와 Connection Pooling 기법이다.
==============================================
package ConnectionFactory;
import MinguTest.ConnFactory;
import java.io.*;
import java.util.*;
import java.sql.*;
public class ConnectionFactory {
private static int maxconn = 0 ;
private static String url = null ;
private static String driver = null;
private static String user = null ;
private static String password = null ;
static{
try{
loadProperties("jdbc.properties") ;
}catch(Exception e){
System.out.println("JDBC.PROPERTIS 파일을 읽어들일수 없습니다.") ;
e.printStackTrace() ;
}
}
private static ConnectionFactory connFactory = new ConnectionFactory() ;
private ConnectionFactory() {} ;
public static ConnectionFactory getDefultFactory(){
if(connFactory == null){
connFactory = new ConnectionFactory() ;
}
return connFactory ;
}
public Connection createConnection() throws SQLException , ClassNotFoundException{
Class.forName(driver) ;
Connection con = DriverManager.getConnection(url,user,password) ;
return con ;
}
public static void loadProperties(String fileName){
try {
Properties p = new Properties() ;
FileInputStream fs = new FileInputStream(fileName) ;
p.load(fs) ;
fs.close() ;
url = p.getProperty("url") ;
driver = p.getProperty("driver") ;
user = p.getProperty("user") ;
password = p.getProperty("password") ;
maxconn = Integer.parseInt(p.getProperty("maxconn")) ;
} catch (Exception e) {
// TODO: handle exception
}
}
public static int getMaxConn(){
return maxconn ;
}
}
ConnectionPooling.java
package ConnectionFactory;
import java.sql.*;
import java.util.*;
public class ConnectionPooling {
private Vector buffer = new Vector() ;
private static ConnectionPooling connPool = new ConnectionPooling() ;
static{
try{
initConnPool() ;
}catch(SQLException e){
e.printStackTrace() ;
}catch(ClassNotFoundException e2){
e2.printStackTrace() ;
}
}
public ConnectionPooling(){}
//여기서 Connection 객체를 10개를 만들어서 , Vector 에 저장 시킨다 .
public static void initConnPool()throws SQLException , ClassNotFoundException {
destroyConnPool() ;
Vector temp = connPool.getConnPool().getConnPoolBuffer() ;
ConnectionFactory factory = ConnectionFactory.getDefultFactory() ;
for(int i=0;i<factory.getMaxConn();i++){
Connection conn = factory.createConnection() ;
temp.addElement(conn) ;
}
}
//Vector 에 저장된 Connection 객체를 다 닫아 버린다 .
public synchronized static void destroyConnPool(){
Vector temp = connPool.getConnPool().getConnPoolBuffer() ;
Enumeration en = temp.elements() ;
while(en.hasMoreElements()){
Connection conn = (Connection)en.nextElement() ;
if(conn != null){
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
}
}
//자기 자신의 객체를 만든다 .
public static ConnectionPooling getConnPool(){
if(connPool == null){
connPool = new ConnectionPooling() ;
}
return connPool ;
}
//버퍼에 있는 . Connection 객체중 . 하나를 지우고 지운 하나를 반환 한다 .
public synchronized Connection getConnection() throws InterruptedException{
while(buffer.size() == 0){
this.wait() ;
}
return (Connection)this.buffer.remove(buffer.size() -1) ;
}
//Connection 객체를 하나 체움다 .
public synchronized void releaseConnection(Connection conn){
this.buffer.addElement(conn) ;
this.notifyAll() ; //스레드 를 다시 시작 함 .
}
private Vector getConnPoolBuffer(){
return this.buffer ;
}
}
//만들떄 .
java. 만의 특성인가 -_-) 잘모르겠지만 .
일단 자기 자신을 인스턴트화 하는 클래스 전연변수와 . 외부에서 인스턴스화 할수있는 , public 메소드로 자신을
인스턴스 화는 메소드를 만든다 .
Connection 은 . 일단. Connection 정보를 jdbc.properties 에 저장된 정보를 메모리의 로딩시 처음 불러와서 .
전역 변수로 저장 하고 있는다 .
그런다음 . 생성된 인스턴스에서 Connection된 메소드에서 Connection 객체를 리턴 시키는 일만 한다 .
Pooling 은 일단 메모리에 처음 로드 될떄 . Connection 객체를 10 개를 Vector 에 저장 시킨다 .
10 개의 Connection 객체는 Connection 된 상태로서 존재 한다 .
여기서 외부에 . 보여줄거는 . Connection 객체의 Con 객체를 반환 하는 메소드 단 반환 할때는 Vector 에 Connection
객체가 존재 하는지 확인하고 , 만약 없으면 , 기다렸다가 , 존재 하면 ,, Vector에 Remove 해서 하나를 지우면서 Connection 을
반환한다 .
또 다 사용한 Connection은 releaseConnection 으로 버퍼에 Connection 을 집어 넣고 , 죽은 스래드를 시작 시켜준다 .
또 Vector 안의 모든 Connection 을 지우면서 닫아주는 메소드가 필요 하다 .
'JAVA/JSP > Java' 카테고리의 다른 글
CharsetTest (0) | 2009.01.06 |
---|---|
DB Connection (0) | 2008.10.06 |
MouseEvent/ItemEvent (0) | 2008.07.14 |
Exception/ Sort (0) | 2008.07.14 |
instanceof/StringTokenizer/innerclass (0) | 2008.07.14 |