티스토리 뷰

홈페이지에 있는 문서(http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html)참조

우선 필요한 라이브러리들은

  • Jakarta-Commons DBCP
  • Jakarta-Commons Collections
  • Jakarta-Commons Pool
  • 인데 $CATALINA_HOME/lib/tomcat-dbcp.jar로 묶여 있단다.
    DataSource를 설정하는 방법은 2가지가 있다고 하는데...
    1. Global 리소스 등록방법
    2. Context 리소스 등록방법
    Global 리소스 등록방법만 적을란다~ Context 등록방법은 나중에 테스트 해보고...

    - $CATALINA_HOME/conf/server.xml 파일의 <GlobalNamingResources> 밑에 추가

      <GlobalNamingResources>
        <!-- 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" />

    <Resource name="jdbc/DS-name" auth="Container" type="javax.sql.DataSource"
      username="userid" password="userpassword" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/DB-name?autoReconnect=true"
      removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"
      maxActive="1000" maxIdle="30" maxWait="180" />
      </GlobalNamingResources>

    - $CATALINA_HOME/conf/context.xml 파일의 <Context>밑에 추가

    <Context>

        <!-- Default set of monitored resources -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->

        <!-- Uncomment this to enable Comet connection tacking (provides events
             on session expiration as well as webapp lifecycle) -->
        <!--
        <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
        -->
    <ResourceLink global="jdbc/DS-name" name="jdbc/DS-name" type="javax.sql.DataSource"/>
    </Context>

    - web.xml 파일 <web-app> 밑에 추가

    <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/DS-name</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>

    * bold, italic 처리되어있는 부분을 각자 환경에 맞게 수정
    - test.jsp를 하나 만들고

    <%@ page contentType="text/html; charset=EUC-KR" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="javax.sql.*" %>
    <%@ page import="javax.naming.*" %>
    <%
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Context ictx = new InitialContext();
            Context ectx = (Context)ictx.lookup("java:comp/env");
            DataSource ds = (DataSource)ectx.lookup("jdbc/DS-name");
            conn = ds.getConnection();
            pstmt = conn.prepareStatement("SELECT NOW()");
            rs = pstmt.executeQuery();
            if(rs.next()) {
                out.println(rs.getString(1));
            }
        }
        catch(Exception e) {
            System.out.println(e.toString());
        }
        finally {
            if(rs != null)
                rs.close();
            if(pstmt != null)
                pstmt.close();
            if(conn != null)
                conn.close();
        }
    %>

    브라우저에서 호출해서 정상적으로 현재 시간이 출력되면 OK~!!

     

    출처 : http://prehacke.tistory.com/23?srchid=BR1http%3A%2F%2Fprehacke.tistory.com%2F23