Tomcat 5 JNDI DataSource를 통한 DB 커넥션 풀 사용

이미 눈치 채셨겠지만, 요즘 내가 RDBMS 배우기에 열을 올리고 있다.
지금까지는 JSP/Servlet에서 직접 커넥션을 맺거나, 웹 컨텍스트내에 커넥션 풀 라이브러리를 두고 호출에서 사용했는데, 좀 바꿔야겠다.
JNDI를 통한 커넥션 풀 사용은 J2EE 표준이고, 현존하는 거의 모든 웹 컨테이너가 지원한다고 한다.

JNDI를 서버에 설정하는 방법은 각 WAS 별로 다르지만, 사용하는 것은 모두 동일하므로 호환성에 문제도 없다.

이 글은 Jakarta의 DBCP 커넥션 풀과 Tomcat JNDI 설정을 통해 데이터베이스 커넥션 풀을 사용하는 방법이다.

JNDI와 커넥션 풀에 관한 자세한 설명이 JavaServer Pages 3rd ed.에 실려있다. 이 책 너무 좋다. 꼭 읽어보라고 강력하게 권하고 싶다.

 * 참조 URL : http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html
이 글은 사실상 저 참조 URL의 번역에 가깝다.
 * Tomcat Admin을 이용한 DataSource 설정 : http://www.okjsp.pe.kr/lecture/viewlet/okjsp2005/05_webdev_datasource.html

기본적으로 필요한 라이브러리

 * commons-dbcp.jar
 * commons-collections.jar
 * commons-pool.jar

예제 JDBC 드라이버

 * Oracle 9i classes12.jar

JNDI Naming Resource 설정

1. 위 라이브러리들을 $CATALINA_HOME/common/lib 에 복사한다. 그 이외 디렉토리에 두면 안된다. ZIP 파일은 JAR로 확장자를 바꿔야 한다. 톰캣은 JAR파일만 클래스패스에 추가한다.

2. Connection 풀을 이용할 경우에는 ResultSet과 Connection 객체를 필히 직접 닫아 줘야만 한다.

3. $CATALINA_HOME/conf/server.xml 혹은 각 웹 컨텍스트별 XML 파일의 <Context>의 자식 요소로 다음을 추가한다.
  <Resource name="jdbc/forumDb" auth="Container" type="javax.sql.DataSource"/>
  <!-- Resource의 name 속성을 이용해서 각 어플리케이션에서
      javax.sql.DataSource 객체를 얻어가게 된다. -->

  
  <!-- 자세한 파라미터 설정은
     http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html 참조 -->
  <ResourceParams name="jdbc/forumDb">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <parameter>
      <name>maxActive</name>
      <value>100</value>
    </parameter>

    <parameter>
      <name>maxIdle</name>
      <value>30</value>
    </parameter>

    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>

    <!-- DB 사용자명과 비밀번호 설정 -->
    <parameter>
     <name>username</name>
     <value>dbuser</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>dbpasswd</value>
    </parameter>

    <!-- JDBC 드라이버 클래스 -->
    <parameter>
       <name>driverClassName</name>
       <value>oracle.jdbc.driver.OracleDriver</value>
    </parameter>
    
    <!-- JDBC 접속 URL -->
    <parameter>
      <name>url</name>
      <value>jdbc:oracle:thin:@dbhost:1521:ORA</value>
    </parameter>

        <!-- 커넥션에 문제 없는지 테스트하는 쿼리 -->
    <parameter>
      <name>validationQuery</name>
      <value>select sysdate</value>
    </parameter>
  </ResourceParams>

 4웹 어플리케이션의 web.xml파일에 다음을 추가하여 JNDI 리소스를 사용할 수 있도록 한다.
    <resource-ref>
        <description>Forum DB Connection</description>
        <!-- 다음이 바로 리소스의 이름 -->
        <res-ref-name>jdbc/forumDb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

JSP/Servlet 에서 사용하기

이제 다음과 같이 JNDI를 이용해 DataSource 객체를 얻고, 그 객체에서 커넥션을 얻어오면 된다.

다음은 서블릿을 작성하고, 그 서블릿에서 DB커넥션을 얻고, 쿼리를 날린 뒤, 그 결과를 JSP 파일에 포워딩하여 JSTL을 이용해 출력하는 것이다.
 1. 예제 테이블과 데이터 SQL - 오라클 계정으로 다음과 같은 데이터를 생성했다고 가정하면
create table test (
    num NUMBER NOT NULL,
    name VARCHAR2(16) NOT NULL
);

truncate table test;

insert into test values(1,'영희');
insert into test values(2, '철수');
insert into test values(3, '미숙');
commit;

 2. test.JndiDataSourceTestServlet 소스
package test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class JndiDataSourceTestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        try {
            // 커넥션을 얻기 위한 전초작업. 이 부분을 메소드화 하면 되겠다. ------------
            Context initContext = new InitialContext();
            Context envContext = (Context)initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/forumDb");
            
            // 커넥션 얻기
             conn = ds.getConnection();
            //------------------------------------------------------------------
            
            String sql = "SELECT * from test";
            stmt = conn.createStatement();
            
            rs = stmt.executeQuery(sql);
            
            List results = new ArrayList();
            
            while (rs.next()) {
                Map row = new HashMap();
                
                row.put("num", rs.getString("num"));
                row.put("name", rs.getString("name"));
                
                results.add(row);
            }
            
            request.setAttribute("results", results);
            
            RequestDispatcher rd = request.getRequestDispatcher("/dbtest.jsp");
            rd.forward(request, response);
            
        } catch (NamingException e) {
            throw new ServletException("JNDI 부분 오류", e);
        } catch (Exception e) {
            throw new ServletException("뭔가 다른 부분 오류", e);
        } finally {
            // 리소스를 필히 반환할 것!
            if (rs != null) { try { rs.close(); } catch (Exception ignored) {} }
            if (stmt != null) { try { stmt.close(); } catch (Exception ignored) {} }
            if (conn != null) { try { conn.close(); } catch (Exception ignored) {} }
        }
    }
}

 3. web.xml에 서블릿 등록
    <servlet>
        <servlet-name>dbtest.svl</servlet-name>
        <servlet-class>test.JndiDataSourceTestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dbtest.svl</servlet-name>
        <url-pattern>/dbtest.svl</url-pattern>
    </servlet-mapping>

 4. /dbtest.jsp 소스
<%@ page contentType="text/html" pageEncoding="EUC-KR" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>JNDI DataSource Test</title>
</head>
<body bgcolor="#FFFFFF">
    <h2>Results</h2>
    
    <c:forEach var="row" items="${results}">
            NUM : ${row.num}<br />
            Name : ${row.name}<br />
            <hr />
    </c:forEach>
</body>
</html>

5. 이제 웹 브라우저에서 "/dbtest.svl" 을 호출해보면 결과를 볼 수 있다.


<Resource>와 <ResourceParams> 요소를 server.xml의 <GlobalNamingResources> 의 자식노드로 옮기면 특정 웹 어플리케이션이 아니라, 이 톰캣에 설치된 전체 웹 어플리케이션에서 사용 할 수 있게 된다. 하지만 각 웹 어플리케이션 "<Context>"에 다음과 같은 설정을 해야 한다.
    <ResourceLink 
        name="jdbc/forumDb"
        global="jdbc/forumDb"
        type="javax.sql.DataSource"
      />

아니면 server.xml에서 <Host> 요소의 자식으로 다음을 추가하면 각 컨텍스트별 설정 필요없이 전체 웹 어플리케이션 컨텍스트에서 GlobalNamingResources로 지정된 JNDI 리소스를 사용할 수 있다.
    <DefaultContext>
        <ResourceLink 
            name="jdbc/forumDb"
            global="jdbc/forumDb"
            type="javax.sql.DataSource"
          />
        </DefaultContext>

문제가 생겼어요!

 1.DB 커넥션이 간혹 끊어져요.

Tomcat이 작동중인 JVM이 가비지 컬렉션을 할 때, 그 시간이 JNDI Resource에 파라미터로 설정한 maxWait보다 길게 갈 경우 DB 커넥션이 끊어질 수 있다.
CATALINA_OPTS=-verbose:gc 옵션을 주면 $CATALINA_HOME/logs/catalina.out에 가비지 컬렉션 상황이 기록된다. 거기에 GC 작업에 걸린 시간도 나오니 그것을 참조로 maxWait 파라미터를 늘려주면 된다. 보통 10~15초로 주면 된다.
GC 시간은 거의 99% 이상 1초 이내에 끝나야 하나보다..

 2. 무작위로 커넥션이 close 되요.

그건.. Connection 객체를 두 번 이상 close 했기 때문이다.
DB Connection Pool은 close()를 호출할 때 정말로 닫는 것이 아니라, 단지 재사용할 수 있다고 표시만 할 뿐이다.
커넥션을 사용하다가 close()하면 다른 쓰레드이서 이 커넥션을 쓸 수 있는데, 이걸 현재 쓰레드에서 계속 잡고 있다가 또 다시 close()를 해버리면, 다른 쓰레드에서 사용중에 close()됐다고 나와 버리게 되는 거다.
    conn.close();
    conn = null;

위와 같이 커넥션을 close()한 뒤에 바로 null 로 설정하여 절대로 다시 사용할 수 없게 만들면 이런 실수는 생기지 않을 것이다.

Tomcat 5.5 예제

        <Resource name="jdbc/myDbResource" auth="Container" type="javax.sql.DataSource"
               maxActive="10" maxIdle="5" maxWait="10000"
               username="ao" password="ao"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/myWebApp"

               removeAbandoned="true"
               removeAbandonedTimeout="60"
               logAbandoned="true"
               validationQuery="select 1"
               testOnBorrow="true"
               testWhileIdle="true"
               timeBetweenEvictionRunsMillis="10000"
               minEvictableIdleTimeMillis="60000"
       />

저기서 removeAbandoned는 리소스 반환에 실패한 (혹은 안한) 커넥션을 자동으로 닫아주는 것이다.

validationQuery는 커넥션이 살아 있는지 테스트하는 코드이다.

MySQL, Connector/J 와 Tomcat 5 예제

http://dev.mysql.com/doc/connector/j/en/cj-tomcat-config.html 에 나온 내용임.
<Context ....>

  ...

  <Resource name="jdbc/MySQLDB"
               auth="Container"
               type="javax.sql.DataSource"/>

  <!-- The name you used above, must match _exactly_ here!

       The connection pool will be bound into JNDI with the name
       "java:/comp/env/jdbc/MySQLDB"
  -->

  <ResourceParams name="jdbc/MySQLDB">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <!-- Don't set this any higher than max_connections on your
         MySQL server, usually this should be a 10 or a few 10's
         of connections, not hundreds or thousands -->

    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>

    <!-- You don't want to many idle connections hanging around
         if you can avoid it, only enough to soak up a spike in
         the load -->

    <parameter>
      <name>maxIdle</name>
      <value>5</value>
    </parameter>

    <!-- Don't use autoReconnect=true, it's going away eventually
         and it's a crutch for older connection pools that couldn't
         test connections. You need to decide if your application is
         supposed to deal with SQLExceptions (hint, it should), and
         how much of a performance penalty you're willing to pay
         to ensure 'freshness' of the connection -->

    <parameter>
      <name>validationQuery</name>
      <value>SELECT 1</value>
    </parameter>

   <!-- The most conservative approach is to test connections
        before they're given to your application. For most applications
        this is okay, the query used above is very small and takes
        no real server resources to process, other than the time used
        to traverse the network.

        If you have a high-load application you'll need to rely on
        something else. -->

    <parameter>
      <name>testOnBorrow</name>
      <value>true</value>
    </parameter>

   <!-- Otherwise, or in addition to testOnBorrow, you can test
        while connections are sitting idle -->

    <parameter>
      <name>testWhileIdle</name>
      <value>true</value>
    </parameter>

    <!-- You have to set this value, otherwise even though
         you've asked connections to be tested while idle,
         the idle evicter thread will never run -->

    <parameter>
      <name>timeBetweenEvictionRunsMillis</name>
      <value>10000</value>
    </parameter>

    <!-- Don't allow connections to hang out idle too long,
         never longer than what wait_timeout is set to on the
         server...A few minutes or even fraction of a minute
         is sometimes okay here, it depends on your application
         and how much spikey load it will see -->

    <parameter>
      <name>minEvictableIdleTimeMillis</name>
      <value>60000</value>
    </parameter>

    <!-- Username and password used when connecting to MySQL -->

    <parameter>
     <name>username</name>
     <value>someuser</value>
    </parameter>

    <parameter>
     <name>password</name>
     <value>somepass</value>
    </parameter>

    <!-- Class name for the Connector/J driver -->

    <parameter>
       <name>driverClassName</name>
       <value>com.mysql.jdbc.Driver</value>
    </parameter>

    <!-- The JDBC connection url for connecting to MySQL, notice
         that if you want to pass any other MySQL-specific parameters
         you should pass them here in the URL, setting them using the
         parameter tags above will have no effect, you will also
         need to use & to separate parameter values as the
         ampersand is a reserved character in XML -->

    <parameter>
      <name>url</name>
      <value>jdbc:mysql://localhost:3306/test</value>
    </parameter>

  </ResourceParams>
</Context>


Oracle 만을 위한 JNDI 설정

원문 : http://www.microdeveloper.com/html/JNDI_Orcl_Tomcat1p.html

1) Modify the server.xml file
In <CATALINA_HOME>/conf/server.xml between <GlobalNamingResources> and </GlobalNamingResources> add the following
<Resource name="jdbc/<alias>"
   auth="Container"
   type="oracle.jdbc.pool.OracleDataSource"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   factory="oracle.jdbc.pool.OracleDataSourceFactory"
   url="jdbc:oracle:thin:@<host>:<port>:<sid>"
   [user=<user>]
   [password=<password>]
   maxActive="20"
   maxIdle="10"
   maxWait="-1" />

Example
<!-- Global JNDI resources -->
 <GlobalNamingResources>

 <!-- Test entry for demonstration purposes -->
 <Environment name="simpleValue" type="java.lang.Integer" value="30"/>

 <!-- Editable user database that can also be used by
   UserDatabaseRealm to authenticate users -->
 <Resource name="UserDatabase" auth="Container"
   type="org.apache.catalina.UserDatabase"
   description="User database that can be updated and saved"
   factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
   pathname="conf/tomcat-users.xml" />
    <!-- Every connection to 'db1' uses the same user -->
 <Resource name="jdbc/db1"
   auth="Container"
   type="oracle.jdbc.pool.OracleDataSource"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   factory="oracle.jdbc.pool.OracleDataSourceFactory"
   url="jdbc:oracle:thin:@oracle.microdeveloper.com:1521:db1"
   user="scott"
   password="tiger"
   maxActive="20"
   maxIdle="10"
   maxWait="-1" />
    <!-- Every connection to 'db2' must provide a username and password -->  <Resource name="jdbc/db2"
   auth="Container"
   type="oracle.jdbc.pool.OracleDataSource"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   factory="oracle.jdbc.pool.OracleDataSourceFactory"
   url="jdbc:oracle:thin:@oracle.microdeveloper.com:1521:db2"
   maxActive="20"
   maxIdle="10"
   maxWait="-1" />
</GlobalNamingResources>

2) Modify the context.xml file
In <CATALINA_HOME>/conf/context.xml between <Context> and </Context> add the following for each entry in the JNDI resource list:
<ResourceLink global="jdbc/<alias>" name="jdbc/<alias>" type="oracle.jdbc.pool.OracleDataSource"/>

Example
<!-- The contents of this file will be loaded for each web application -->
 <Context>

 <!-- Default set of monitored resources -->
 <WatchedResource>WEB-INF/web.xml</WatchedResource>
 <WatchedResource>META-INF/context.xml</WatchedResource>
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
 <!--
 <Manager pathname="" />
   -->
 <ResourceLink global="jdbc/db1" name="jdbc/db1" type="oracle.jdbc.pool.OracleDataSource"/>
 <ResourceLink global="jdbc/db2" name="jdbc/db2" type="oracle.jdbc.pool.OracleDataSource"/>
 </Context>

3) Modify the context's web.xml file (5.0.x step only - not necessary for 5.5.x)
In the <CONTEXT>/WEB-INF/web.xml between <web-app> and </web-app> add the following:
<resource-ref>
   <description><Your Description></description>
   <res-ref-name>jdbc/<alias></res-ref-name>
   <res-type>oracle.jdbc.pool.OracleDataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

Example
<resource-ref>
   <description>Oracle Development Datasource</description>
   <res-ref-name>jdbc/db1</res-ref-name>
   <res-type>oracle.jdbc.pool.OracleDataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

<resource-ref>
   <description>Oracle Development Datasource</description>
   <res-ref-name>jdbc/db2</res-ref-name>
   <res-type>oracle.jdbc.pool.OracleDataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

4) Restart Tomcat

by 권남 | 2005/03/15 11:58 | 프로그래밍 | 트랙백 | 핑백(2) | 덧글(2)

트랙백 주소 : http://kwon37xi.egloos.com/tb/2852803
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Linked at 후아님의 이글루 : 0904(.. at 2007/09/05 00:44

... destroy()"); }}이 글과 관련있는 글을 자동검색한 결과입니다 [?] Tomcat 5 JNDI DataSource를 통한 DB 커넥션 풀 사용 by 권남 [Java] 파일 다운로드 클래스 - 한글 처리 포함 by 권남 JSP Pa ... more

Linked at 結婚できない男 : Tomcat.. at 2009/11/05 19:02

... 권남님... 블로그http://kwon37xi.egloos.com/2852803 ... more

Commented by 111 at 2009/02/17 11:09
저작권 표시는 좀 하시지..
Commented by 권남 at 2009/02/17 16:09
111님// 어떤 저작권 표시를 말씀하시는 건지요? 글을 읽어보시면 제가 안쓴 것 혹은 참조한 것들에 대한 모든 링크를 제공하고 있습니다.

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶