2012-05-25 3 views
0

나는 자신의 연결 풀이 :스프링을 사용하여 속성을 초기화하고로드하고 내 자신의 연결 풀을 삭제하는 방법은 무엇입니까?

public final class ConnectionPool { 


private static final Logger log = Logger.getLogger(ConnectionPool.class); 

private static final int DEFAULT_POOL_SIZE = 10; 


//single instance 
private static ConnectionPool instance; 
//queue of free connections 
private BlockingQueue<Connection> connectionQueue; 

public ConnectionPool(String driver, String url, String user, 
     String password, int poolSize) 
     throws ClassNotFoundException, DAOException{ 
    try{ 
     Class.forName(driver); 
     connectionQueue = new ArrayBlockingQueue<Connection>(poolSize); 
     for(int i = 0; i < poolSize ;i++){ 
      Connection connection = DriverManager.getConnection(url, user, password); 
      connectionQueue.offer(connection); 
     } 
    } 
    catch (SQLException e) { 
     log.error(e); 
     throw new DAOException(e.getMessage()); 
    } 
} 

public static void init() throws DAOException{ 
    try { 
    if(instance == null){ 

     String driver = ConfigurationManager. 
     getInstance().getProperty(ConfigurationManager.DATABASE_DRIVER_NAME); 
     String url = ConfigurationManager. 
     getInstance().getProperty(ConfigurationManager.DATABASE_URL); 
     String user = ConfigurationManager. 
     getInstance().getProperty(ConfigurationManager.DATABASE_USER); 
     String password = ConfigurationManager. 
     getInstance().getProperty(ConfigurationManager.DATABASE_PASSWORD); 
     String poolSizeStr = ConfigurationManager. 
     getInstance().getProperty(ConfigurationManager.DATABASE_POOLSIZE); 
     int poolSize = (poolSizeStr != null) ? 
       Integer.parseInt(poolSizeStr) : DEFAULT_POOL_SIZE; 

     log.info("Trying to create pool of connections..."); 

     instance = new ConnectionPool(driver,url,user,password,poolSize); 

     log.info("Connection pool initialized"); 
    } 
    }catch (ClassNotFoundException e) { 
     log.error(e); 
    } catch (SQLException e) { 
     log.error(e); 
     throw new DAOException(e.getMessage()); 
    } 
} 

public static void dispose() throws DAOException { 
    try { 
     if(instance != null){ 
      instance.clearConnectionQueue(); 
      instance = null; 
      log.info("Connection queue is disposed"); 
     } 
    } catch (DAOException e) { 
     log.info(e.getMessage()); 
     throw new DAOException(e.getMessage()); 
    } 
} 

public static ConnectionPool getInstance(){ 
    return instance; 
} 

public Connection takeConnection() { 
    Connection connection = null; 
    try{ 
     connection = connectionQueue.take(); 
    }catch (InterruptedException e) { 
     log.info("Free connection waiting interrupted.Returned null connection"); 
     log.error(e); 
    } 
    return connection; 
} 

public static void releaseConnection(Connection connection) throws DAOException { 
    try { 

     if(!connection.isClosed()){ 
      if(!getInstance().connectionQueue.offer(connection)){ 
       log.info("Connections is not added."); 
      } 
     } 
     else{ 
      log.info("Trying to release closed connection."); 
     } 
    } catch (SQLException e) { 
     log.info("SQLException at connection isClosed(). Connection is not    added"); 
     throw new DAOException(e.getMessage()); 
    } 
} 

private void clearConnectionQueue() throws DAOException{ 
    try { 
     Connection connection; 
     while((connection = connectionQueue.poll()) != null){ 

      if(!connection.getAutoCommit()){ 
       connection.commit(); 
       connection.close(); 
      } 
    } 
    } catch (SQLException e) { 
     log.info(e.getMessage()); 
     throw new DAOException(e.getMessage()); 
    } 
} 



} 

을 그리고 지금은 초기화 및 ResourceBundle 연결 내 자신의 클래스 ConfigurationManager 사용하여 수신기 및 부하 특성과 그것을 파괴하고 있습니다 :

public final class ConfigurationManager { 

private static ConfigurationManager instance; 
private ResourceBundle resourceBundle; 
//getting info from config.properties 
private static final String BUNDLE_NAME = "config"; 
public static final String DATABASE_DRIVER_NAME = 
    "DATABASE_DRIVER_NAME"; 
public static final String DATABASE_URL = 
    "DATABASE_URL"; 
public static final String DATABASE_USER = 
    "DATABASE_USER"; 
public static final String DATABASE_PASSWORD = 
    "DATABASE_PASSWORD"; 
public static final String ERROR_PAGE_PATH = 
    "ERROR_PAGE_PATH"; 

public static final String BEAN_PATH = 
    "BEAN_PATH"; 
public static final String DATABASE_POOLSIZE = 
    "DATABASE_POOLSIZE"; 


public synchronized static ConfigurationManager getInstance() { 
    if (instance == null) { 
     instance = new ConfigurationManager(); 
     instance.resourceBundle = 
      ResourceBundle.getBundle(BUNDLE_NAME); 
    } 
    return instance; 
} 
public String getProperty(String key) { 
    return (String)resourceBundle.getObject(key); 
} 
} 

을하지만 내가 원하는 (init, destroy, properties), Spring을 사용한다. 그럼 내가 어떻게 할 수 있니? 물론

답변

1

사용 봄의 init 파괴 방법 :

http://www.mkyong.com/spring/spring-init-method-and-destroy-method-example/

는 개인적으로, 나는이 경솔한 생각합니다. 이미 사용 가능한 수영장 (예 : C3P0)을 개선 할 가능성이 없습니다. 당신은 더 악화시킬 수 있습니다. 그러나 그것은 당신의 선택입니다.

+0

감사합니다. 시도해보십시오. 하지만이 일은 정적 init() 메서드를 호출하고 싶지 않습니다. – And

+0

정적이 아닌 것으로 만듭니다. – duffymo