was 2008. 11. 25. 09:39

Tomcat5.x버전에서 DBCP 설정

Tomcat5.x 버전에서 DBCP 설정하기



1. 톰캣이 설치된디렉토리 conf폴더의 server.xml 에 <host>태그 사이 추가
 
<Context path="/myapp" docBase="톰켓설치경로\webapps\ROOT"
        debug="5" reloadable="true" crossContext="true">

<Resource name="jdbc/ora" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@200.000.000.01:1521:XID"
              username="kkk" password="kkk" maxActive="15" maxIdle="10"
              maxWait="-1"/>
</Context>


Context - path : URL 호출시 사용될 이름
            - docBase : 실제 웹 어플리케이션이 위치한 폴더명
            - debug : 로그 작성 레벨
            - reloadable : 톰캣서버의 재시작 없이 수정된 내용을 불러오기 위한 옵션
            - crossContext : myapp이외의 Context에서도 사용 가능하도록 하는 옵션

Resource - name : Resource명칭(JNDI Lookup 시 사용할 명칭)
              - auth : Resource 관리자를 지정. 여기서는 톰캣컨테이너가 관리자임
              - type : Resource 의 형태 지정. 데이타소스 형태를 지정함
              - maxActive : 최대 연결 가능한 Connection 숫자를 지정함
              - maxIdle : Connection pool 유지를 위해 최대 대기 connection 숫자
              - maxWait : Connection 재 사용을 위해 대기해야 하는 최대 시간(단위:ms)
              - username : DB접속 계정
              - password : DB접속 암호
              - driverClassName : DB와 접속하기 위한 driver 클래스를 지정. 예에서는 MySql임
              - url : 접속한 DB가 위치한 서버명, 포트명, 사용할 데이타베이스명을 지정
* 주의 : WEB-INF/conf 디렉토리가 절대아님,  tomcat_server.xml 파일도 아님
 
2. WEB-INF/web.xml에 다음추가
 
 <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>

 
//내용설명
resource-ref - description : 참조할 resource에 대한 설명
                  - res-ref-name : 참조할 resource의 명칭. Server.xml에 정의된 이름과 일치해야함
                  - res-type : 참조할 resource 형태로 여기서는 DataSource를 지정함
                  - res-auth : 참조할 resource를 관리할 주체로 톰캣컨테이너를 지정함
 
3. 아래와 같은 커넥션 테스트용 페이지를 만들어 테스트해본다.
 
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%
 Context initCtx = new InitialContext();
 Context envCtx = ( Context ) initCtx.lookup( "java:comp/env" );
 DataSource ds = ( DataSource ) envCtx.lookup( "jdbc/ora" );
 Connection conn = ds.getConnection();
 Statement stmt = conn.createStatement();
 String query = "select sysdate from dual";
        ResultSet rs = stmt.executeQuery( query );
        if( rs.next() ) {
                out.println( "DB Connection Success...!!!" );
  out.println( "JNDI Test Success!!!" );
        }
        rs.close();
        stmt.close();
        conn.close();
%>