===================================================================
1. jakarta 홈피에서 dbcp 관련 jar 를 다운 받는다.
여기 - > http://ftp.apache-kr.org/jakarta/commons
commons-collections-3.1.jar
commons-dbcp-1.2.1.jar
commons-pool-1.2.jar
===================================================================
2. ${WAS_HOME}/common/lib 에 위치 시키고 classespath 를 확인한다.
===================================================================
3. Init ( DBCPInit.java ) 파일 생성
package com.zzang.db;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.util.StringTokenizer;
public class DBCPInit extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
try {
String drivers = config.getInitParameter("jdbcdriver");
StringTokenizer st = new StringTokenizer(drivers, ",");
while (st.hasMoreTokens()) {
String jdbcDriver = st.nextToken();
Class.forName(jdbcDriver);
}
Class.forName("org.apache.commons.dbcp.PoolingDriver");
System.setProperty("org.xml.sax.drvier",
"org.apache.crimson.parser.XMLReaderImpl");
} catch(Exception ex) {
throw new ServletException(ex);
}
}
}
===================================================================
4. web.xml 설정 하기
${WAS_HOME}/webapps/ROOT/WEB-INF/web.xml
<!-- DBCP config start -->
<servlet>
<servlet-name>DBCPInit</servlet-name>
<servlet-class>com.zzang.db.DBCPInit</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>jdbcdriver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
</servlet>
<!-- DBCP config end -->
===================================================================
5. config 파일 생성
${WAS_HOME}/webapps/ROOT/WEB-INF/classes/pool.jocl
<object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">
<object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
<string value="jdbc:oracle:thin:@localhost:1521:ora92" />
<string value="userid" />
<string value="userpw" />
</object>
<object class="org.apache.commons.pool.impl.GenericObjectPool">
<object class="org.apache.commons.pool.PoolableObjectFactory" null="true" />
<int value="10" /> <!-- maxActive -->
<byte value="1" /> <!-- whenExhaustedAction -->
<long value="20000" /> <!-- maxWait -->
<int value="100" /> <!-- maxIdle -->
<int value="10" /> <!-- minIdle -->
<boolean value="true" /> <!-- testOnBorrow -->
<boolean value="true" /> <!-- testOnReturn -->
<long value="5000" /> <!-- timeBetweenEvctionRunsMillis -->
<int value="5" /> <!-- numTestsPerEvictionRun -->
<long value="5000" /> <!-- minEvictableIdleTimeMillis -->
<boolean value="true" /> <!-- testWhileIdle -->
</object>
<object class="org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory" null="true" />
<string null="true" />
<boolean value="false" />
<boolean value="true" />
</object>
===================================================================
6. WAS 재구동 & 테스트
//initial
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//get Connnection
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/pool");
conn.setAutoCommit(false);
stmt = conn.createStatement();
.................
} catch(Exception ex) {
conn.rollback();
ex.printStackTrace();
out.println(ex.toString());
} finally {
if ( rs != null ) try { rs.close(); } catch( Exception exf ){}
if ( stmt != null ) try { stmt.close(); } catch( Exception exf ){}
if ( conn != null ) try { conn.close(); } catch( Exception exf ){}
}
===================================================================
7. Tip
- 5번의 pool.jocl 파일을 여러개 생성하면, DB개정을 여러개 가져갈 수 있쥐요~
- 5번의 maxWait 값이 작으면 에러가 발생할 수 있다.( 정확한 에럼 메세지가 기억 안남 ^^;;;)
===================================================================