티스토리 뷰

금일은 java에서 oracle connection pool 설정하는 방법에 대해 알아보도록 하겠습니다.


import java.sql.Connection;

import java.sql.SQLException;


import oracle.jdbc.pool.*;


public class DBConnection {


// Data source for the pooled connection

private static OracleDataSource dataSource;


// Host

private static final String dbHost = "localhost";


// Port

private static final String dbPort = "1521";


// DBname

private static final String database = "orcl";


// DBuser

private static final String dbUser = "rocksea";


// DBpassword

private static final String dbPassword = "rocksea";


static {

   OracleConnectionPoolDataSource opds;

   try {

   

       // set cache properties 

       java.util.Properties prop = new java.util.Properties();

     prop.setProperty("MinLimit", "2");       //min pool size

    prop.setProperty("MaxLimit", "20");    //max pool size


       // set DataSource properties    

       OracleDataSource ods = new OracleDataSource();


       String url = "jdbc:oracle:thin:@" + dbHost + ":" + dbPort + ":"+ database;    

       ods.setURL(url);    

       ods.setUser(dbUser);    

       ods.setPassword(dbPassword);    

    ods.setConnectionCachingEnabled(true); // be sure set to true    

       ods.setConnectionCacheProperties (prop);    

       ods.setConnectionCacheName("rocksea"); // this cache's name    


       dataSource = ods;

               

   } catch (SQLException e1) {

       System.err.println("Connection failed!");

   }

   try {

       // Load driver

       Class.forName("oracle.jdbc.driver.OracleDriver");


   } catch (ClassNotFoundException e) {

       System.out.println("Driver not found!");

   }

}


public static Connection getConnection() throws SQLException {

   return dataSource.getConnection();

}

}


[ 그림1 ] connection pool 유지 확인


막상 설명하려고하니 설명할 부분이 별로 없군요. 이상 포스팅을 마치도록 하겠습니다.

bo rocksea.



댓글