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:@taesuz.net:1521:taesuzdb" />
<string value="taesuz" />
<string value="1111" />
</object>
<object class="org.apache.commons.pool.impl.GenericObjectPool">
<object class="org.apache.commons.pool.PoolableObjectFactory" null="true" />
<int value="10" />
<byte value="1" />
<long value="10000" />
<int value="10" />
<int value="3" />
<boolean value="true" />
<boolean value="true" />
<long value="600000" />
<int value="5" />
<long value="3600000" />
<boolean value="true" />
</object>
<object class="org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory" null="true" />
<string null="true" />
<boolean value="false" />
<boolean value="true" />
</object>
DBCPInit.java
package taesuz.dbpool;
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("pool");
StringTokenizer st = new StringTokenizer(drivers, ",");
while (st.hasMoreTokens()) {
String DB = st.nextToken();
Class.forName(DB);
}
Class.forName("org.apache.commons.dbcp.PoolingDriver");
} catch(Exception ex) {
throw new ServletException(ex);
}
}
}
web.xml
<web-app>
<servlet>
<servlet-name>DBCPInit</servlet-name>
<servlet-class>taesuz.dbpool.DBCPInit</servlet-class>
<init-param>
<param-name>pool</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
설정 끝~~~~
테스트를 위한 JSP와 커넥션을 가져오는 클래스
DBConnect.java
package taesuz.dbpool;
import java.sql.*;
public class DBConnect {
/**
* APP/Web 환경에서 쓰이도록 지정 DBCP를 사용한다.
* default poolName : pool
* @return Connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:apache:commons:dbcp:/pool");
}
/**
* APP/Web 환경에서 쓰이도록 지정 DBCP를 사용한다.
* @param poolName
* @return Connection
* @throws SQLException
*/
public static Connection getConnection(String poolName) throws SQLException {
return DriverManager.getConnection("jdbc:apache:commons:dbcp:/"
+ poolName);
}
}
test.jsp
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ page import="java.sql.*" %>
<%@ page import="taesuz.dbpool.DBConnect" %>
<%
try{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = DBConnect.getConnection();
String query = "select * from DUAL";
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if(rs.next())
out.print(rs.getString(1));
} catch(Exception e) {
System.out.println(e);
} finally {
if (rs != null) try { rs.close(); } catch (Exception e) {}
if (stmt != null) try { stmt.close(); } catch (Exception e) {}
if (conn != null) try { conn.close(); } catch (Exception e) {}
}
%>
다른 관련 설정 트랙백 참조.