데이터베이스 커넥션은 데이터베이스에 한 번 연결하기 위한 작업인데 이러한 작업들이 매번 새로운 데이터베이스 연결에 대한 요청이 들어올 때마다 수행해야 한다면 많은 부담이 된다.
이런 커넥션 객체를 생성하고 관리하는 방법에는 다음과 같은 3가지 방법이 있다.
1). service method(doGet, doPost)에서 커넥션 객체를 생성
요청당 한 개씩 커넥션 객체가 생성되어서 시스템의 부하가 커지고 메모리가 낭비된다.
커넥션 시간이 요청시간에 포함된다.
2). init method에서 커넥션 객체를 생성
서비스 될 때 단 한 번만 사용하는 init method에서 커넥션 객체를 생성한다.
init method에서 생성하므로 connection 시간이 안 걸리나 하나의 커넥션을 쓰면 커넥션에 쿼리가 쌓이게 되어 응답시간이 증가한다.
3). 커넥션 풀에서 커넥션 객체 생성 및 관리하기
이 방법은 자원을 쓰고 회수하는 방법을 쓴다.
한 번 만들어져서 사용된 커넥션 객체는 다시 커넥션 풀로 회수되는 것이다.
권장하는 형태이다.
자카르타 DBCP API를 이용한 커넥션 풀
1). 자카르타 DBCP API관련 jar 파일 설치
-> commons-dbcp-1.2.1.jar, commons-collections-3.1.jar, commons-pool-1.2.jar을 "웹어플리케이션폴더\WEB-INF\lib" 안에 넣는다.
-> DBCP API를 사용할 경우 JDBC 커넥터인 mysql-connector-java-5.0.0-beta-bin.jar을 인식 못할수도 있으므로 "톰캣홈\common\lib"에 넣어준다.
+- 이대로 했지만, 되지 않았다...;; 2일 고생해서 해결한 것은... 저 jar파일들을 c:\apache\common\lib에 넣어주고 나서였다...;;;
2). DBCP에 관한 정보 설정 - 톰캣홈\conf\server.xml (새로 추가하는 것임.)
<Resource name="jdbc/jsptest" auth="Container"
type="javax.sqlDataSource"
driverClassName="com.mysql.jdbc.Driver"
loginTimeout="10"
maxWait="5000"
username="jspid"
password="jsppass"
testOnBorrow="true"
url="jdbc:mysql://localhost:3306/jsptest"
/>
</GlobalNamingResources>
////////////////////////////////////////////////////////////////////////////////////
<Context path="/projectStudy" docBase="C:\apache\webapps\projectStudy" debug="1" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_JspTest_log." suffix=".txt" timestamp="true" />
<Resource name="jdbc/jsptest"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
loginTimeout="10"
maxWait="5000"
username="jspid"
password="jsppass"
testOnBorrow="true"
url="jdbc:mysql://localhost:3306/jsptest"
/>
</Context>
3). JNDI 리소스 사용 설정 - web.xml
\webapps\projectStudy\WEB-INF\web.xml에 아래를 추가한다.
<resource-ref>
<description>jsptest</description>
<res-ref-name>jdbc/jsptest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4). JSP 페이지에서 커넥션 풀 사용
<%@ page contentType="text/html;charset=euc-kr"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*"%>
.
.
.
try{
Context initCtx=new InitialContext();
Context envCtx=(Context) initCtx.lookup("java:comp/env");
DataSource ds=(DataSource)envCtx.lookup("jdbc/jsptest");
Connection conn=ds.getConnection();
'JSP' 카테고리의 다른 글
오라클 long형 4000바이트 이상 DB에 입력하기.. (0) | 2009.05.21 |
---|---|
[펌] 이클립스 커넥션풀 설정 (0) | 2009.04.09 |
[펌] MySQL JDBC JSP 저장 로드시 한글깨짐 현상 해결법 정리 (0) | 2009.04.06 |
[펌] The value for the useBean class attribute ~ is invalid 문제 해결 (0) | 2009.04.06 |
[펌] 톰갯 5.5에서 커넥션풀을 이용하는 방법 (0) | 2009.04.06 |