과거 Tomcat 5.0 버전 까지는 커넥션 풀링을 구현하려면, DBCP, Collections, Pool library를 각각 설치해야 했지만, 자카르타 톰캣에서는 다음과 같이 하나의 라이브러리로 제공하고 있다.
Tomcat5.5의 경우 톰캣경로 /common/lib/naming-factory-dbcp.jar
Tomcat6.0의 경우 톰캣경로 /lib/tomcat-dbcp.jar
테스트환경
==============
Oracle 11g
Tomcat 6.0
Eclipse
==============
1. Context configuration
Tomcat Server - server.xml 설정
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
username="scott" password="비밀번호" maxActive="20" maxIdle="10"
maxWait="-1"/>
2. web.xml configuration
[출처] Oracle DBCP Connection pool Eclipse환경|작성자 bench87
Project - web.xml 설정
<web-app>
....
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app
3. Code example
text page 작성 - jdbc6.0.jsp
<%@ page import="javax.naming.*,
java.sql.*,
javax.sql.*" %>
<%
response.setContentType("text/html;");
Connection conn= null;
Statement stmt = null;
ResultSet rs = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/oracleDB");
System.out.println("[DataSource] :: " + ds.toString());
conn = ds.getConnection();
//
stmt = conn.createStatement();
// ResultSet
String query =
"SELECT * FROM TEMPLET";
rs = stmt.executeQuery(query);
%><table border="1" cellspacing="0">
<tr>
<td>Seq</td>
<td>Str1</td>
<td>Str2</td>
</tr>
<%
while(rs.next()) {
%><tr>
<td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
</tr>
<%
} // end while
%></table><%
rs.close();
stmt.close();
} catch (SQLException e) {
out.println("err:"+e.toString());
} finally {
// conn.close();
}
%>
'Server Enterprise > JDBC & DBCP' 카테고리의 다른 글
PreparedStatement 객체 재사용하기 (0) | 2016.06.22 |
---|---|
[Oracle] JDBC Connection (0) | 2013.07.17 |
DBCP 에서 JNDI 설정 방법 (0) | 2012.07.20 |