ABOUT ME

Today
Yesterday
Total
  • 톰캣 Connection Pool 세팅 가이드
    카테고리 없음 2008. 3. 24. 21:44

    톰캣 Connection Pool 세팅 가이드





    테스트 서버환경

    WAS : Tomcat 5.5
    DBMS : Mysql




    [참고자료]

    JNDI Datasource HOW-TO(공식 매뉴얼)

    http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html




    1. Database Connection Pool(DBCP) 관련 파일들을 다운받는다. (첨부파일에 포함)
    Jakarta-Commons Collections : http://commons.apache.org/collections/ 
    Jakarta-Commons DBCP : http://commons.apache.org/dbcp/
    Jakarta-Commons Pool : http://commons.apache.org/pool/


    2. MYSql용 공식 JDBC 드라이버를 다운받는다.(첨부파일에 포함)
    http://www.mysql.com/products/connector-j




    3. 1번에서 다운받은 Commons 파일들을 ''어플리케이션 폴더\WEB-INF\lib''폴더와 ''톰캣홈\common\lib''폴더에 각각 복사한다.( 어플리케이션 폴더\WEB-INF\lib에만 넣을시 \WEB-INF\lib에 들어있는  JDBC 드라이버가 인식되지 않는다 )


    4. 2번에서 다운받은 JDBC드라이버를 ''자바홈/jre/lib/ext''폴더와 ''톰캣홈\common\lib''폴더에 복사해 넣는다.


    5. ''어플리케이션홈\META-INF''폴더에 ''context.xml''라는 파일을 생성하여 다음을 입력한 후 저장한다.(노란색 부분은 각자 환경에 맞게 세팅한다)
    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/" docBase="ROOT"
            debug="5" reloadable="true" crossContext="true">

      <Resource name="jdbc/mysqlDS" auth="Container" type="javax.sql.DataSource"
                   maxActive="4" maxIdle="2" maxWait="5000"
                   username="uname" password="password" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/lifebalance?autoReconnect=true"/>

    </Context>


    6. 다음과같은 jsp파일을 생성하여 브라우저로 로드시 현재 시간이 출력되는지 확인하여 설정을 완료한다.(노란색 부분은 각자 환경에 맞게 세팅한다)
    <%@ page import="java.sql.*,javax.naming.*,javax.sql.DataSource" %>
    <%
     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/mysqlDS");
     conn = ds.getConnection();
       stmt = conn.createStatement();
       rs = stmt.executeQuery("select now()");
       if(rs.next()){
        out.println(rs.getString(1));
       }
       rs.close();
       rs = null;
       stmt.close();
       stmt = null;
       conn.close(); // Return to connection pool
       conn = null;  // Make sure we don''t close it twice
     } catch (SQLException e) {
       out.println(e.toString());
     } finally {
       // Always make sure result sets and statements are closed,
       // and the connection is returned to the pool
       if (rs != null) {
         try { rs.close(); } catch (SQLException e) { ; }
         rs = null;
       }
       if (stmt != null) {
         try { stmt.close(); } catch (SQLException e) { ; }
         stmt = null;
       }
       if (conn != null) {
         try { conn.close(); } catch (SQLException e) { ; }
         conn = null;
       }
     }
    %>

Designed by Tistory.