Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

까먹지 말자! 자주 보자!

JSP BOARDER(4)_(ConnectionPool .java) 본문

JSP

JSP BOARDER(4)_(ConnectionPool .java)

Phonetographer 2016. 5. 24. 17:13

package board;

import java.sql.*;

import java.util.*;

import board.*;

class ConnectionInfo{

public Connection connection = null;

public long time = 0;

public ConnectionInfo(Connection connection, long time){

this.connection= connection;

this.time = time;

}

}


public class ConnectionPool {

private static int MAX_CONNECTION = 5;

private Vector buffer = new Vector();

private int wait_count = 0;

private static ConnectionPool connectionPool = new ConnectionPool();

static{

try{

initConnectionPool();

}catch(SQLException e){

System.out.println("----Connection Create Error----");

e.printStackTrace();

}catch(ClassNotFoundException e2){

System.out.println("----Driver Calss Not Found Error----");

e2.printStackTrace();

}

}

private ConnectionPool(){}

public synchronized static void initConnectionPool() throws SQLException, ClassNotFoundException{

ConnectionPool.getConnectionPool().destroyConnectionPool();

Vector temp = ConnectionPool.getConnectionPool().getConnectionPoolBuffer();

for (int i = 0; i < MAX_CONNECTION; i++) {

Connection connection = ConnectionFactory.createConnection();

temp.addElement(new ConnectionInfo(connection, System.currentTimeMillis()));

System.out.println("New Connection Created."+connection);

}

}

public synchronized static void destroyConnectionPool(){

Vector temp = ConnectionPool.getConnectionPool().getConnectionPoolBuffer();

int t = temp.size();

System.out.println("버퍼의 크기는 : "+ t);

for (int i = 0; i < t; i++) {

ConnectionInfo connectionInfo = (ConnectionInfo)temp.remove(0);

if(connectionInfo.connection != null){

try{

connectionInfo.connection.close();

System.out.println("Connection Closed.."+connectionInfo.connection);

}catch(SQLException e){

e.printStackTrace();

}

}

}

}

public static ConnectionPool getConnectionPool(){

if(connectionPool == null){

connectionPool = new ConnectionPool();

}

return connectionPool;

}

public synchronized Connection getConnection(){

ConnectionInfo connectionInfo = null;

if(wait_count > MAX_CONNECTION){

return null;

}

try{

while(buffer.size() == 0){

wait_count++;

this.wait();

wait_count--;

}

connectionInfo = (ConnectionInfo)this.buffer.elementAt(0);

long interval = System.currentTimeMillis() - connectionInfo.time;

if(interval> 1000*60 * 30){

try{

System.out.println((interval/1000)+"초를 경과 Connection Close");

connectionInfo.connection.close();

}catch(SQLException e1){

e1.printStackTrace();

System.out.println("Connection Close Err");

}

ConnectionFactory connectionFactory = ConnectionFactory.getDefaultFactory();

connectionInfo.connection=connectionFactory.createConnection();

System.out.println(new java.util.Date().toString()+"Connection Open");

}

}catch(InterruptedException e2){

e2.printStackTrace();

}finally{

connectionInfo = (ConnectionInfo)this.buffer.remove(0);

}

return connectionInfo.connection;

}

public synchronized void releaseConnection(Connection Connection){

this.buffer.addElement(new ConnectionInfo(Connection, System.currentTimeMillis()));

this.notifyAll();

}

public Vector getConnectionPoolBuffer(){

return this.buffer;

}

}