본문 바로가기
Java

[펌] JSP 엔진별 JNDI DB Pool 설정하기

by 사우람 2010. 7. 12.

각 엔진별로 설정법과 사용법이 조금씩 틀립니다.
하나씩 정리해서 올리도록 하겠습니다.

* 기본적으로 JNDI 를 이용한 호출이기 때문에 javax.naming.* 를 jsp 페이지에 import 해야 합니다.


1. JRun3, 4 의 경우
 - 설정은 admin center 에서 해당 dbms 의 종류와 jndi 이름을 입력한뒤 추가하면 됩니다.

 - 설정을 추가한다음 해당 서버컨텐츠를 재시작 합니다.

  - jsp 페이지에서 호출방법
   InitialContext context = null;
   DataSource ds = null;
   try {
          context = new InitialContext();
          ds = (DataSource) context.lookup("mydb");
   } catch(Exception e) {
           out.println("error : " + e);
  }
  
  con = ds.getConnection();


  * JNDI context 를 얻어오는 부분에 try catch 예외처리를 해야 합니다. 그리고 lookup 에서 dns 명을 적어줍니다.

 

2. Resin 2.1.x 의 경우

 - JDBC용 드라이버를 $RESIN_HOME/lib/ 에 복사한다.

 - $RESIN_HOME/conf/resin.conf 에 db pool 용 resource 설정을 추가한다.

  예제

<resource-ref>
  <res-ref-name>jdbc/test</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <init-param driver-name="com.caucho.jdbc.mysql.Driver"/>
  <init-param url="jdbc:mysql_caucho://localhost:3306/test"/>
  <init-param user=""/>
  <init-param password=""/>
  <init-param max-connections="20"/>
  <init-param max-idle-time="30"/>
</resource-ref>

 

 - 설정후에는 resin 엔진을 다시 시작해야 설정내용이 적용됩니다.

 

 - jsp 페이지에서 호출방법

 Context ic = new InitialContext();
 DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/test");

 con = ds.getConnection();

  * res-ref-name 에서 설정한 이름을 java:comp/env/ 다음에 적으면 됩니다.

  * 따로 try catch 를 해주지 않아도 됩니다.

 

3. Tomcat 4.1.x 의 경우 ( 톰캣디 렉토리를 $CALTALIA_HOME 환경변수에 설정)

   mysql 을 예제로 하여 설명하겠습니다.

 - 먼저 해당 jdbc 드라이버를 tomcat 디렉토리의 $CALTALIA_HOME/common/lib 에 복사한다

 - $CALTALIA_HOME/conf/server.xml 을 설정한다.

    설정 위치는 해당 <context> </context> 사이 이다.

<Resource name="mysql" scope="Shareable" type="javax.sql.DataSource"/>
   <ResourceParams name="jdbc/mysql">
     <parameter>
        <name>validationQuery</name>
        <value>show status</value>
     </parameter>
     <parameter>
        <name>maxWait</name>
        <value>5000</value>
     </parameter>
     <parameter>
        <name>maxActive</name>
        <value>4</value>
     </parameter>
     <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost/chat?useUnicode=true&characterEncoding=MS949</value>
     </parameter>
     <parameter>
        <name>driverClassName</name>
        <value>com.mysql.jdbc.Driver</value>
     </parameter>
     <parameter>
        <name>maxIdle</name>
        <value>2</value>
     </parameter>
     <parameter>
        <name>username</name>
        <value>root</value>
     </parameter>
     <parameter>
        <name>password</name>
        <value></value>
     </parameter>
 </ResourceParams>

 

 - 각 context 의 WEB-INF/web.xml 파일을 설정한다.

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

  

 - jsp 페이지에서 호출방법

   Context ic = new InitialContext();
   DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/mysql"); 
   con = ds.getConnection();

 

  * mysql 의 경우 context 가 다르면,  각각 설정을 따로 하셔야 합니다.

 

  * data source 의 접근 방식은 다음과 같이 할 수도 있습니다.

   Context initContext = new InitialContext();
   Context envContext  = (Context)initContext.lookup("java:/comp/env");
   DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
 

  * tomcat 에서 JNDI 관련 에러메시지별 대처 방법

    - Cannot load JDBC driver class 'com.mysql.jdbc.Driver', cause: com.mysql.jdbc.Driver 또는

       Null Pointer Exception '', cause: No suitable driver

    ==> 해당 jdbc 드라이버(위의경우 mysql 드라이버) 가 없기 때문입니다.

          해당 드라이버 파일을 $CATALINA_HOME/common/lib 디렉토리로 복사합니다.

    - Name xxxx is not bound in this Context

      ==> server.xml 과 web.xml 에서 해당 JNID가 설정되지 않은 경우입니다.

           server.xml 과 web.xml 에 JNDI resource 를 설정하고 이름을 동일하게 설정하도록 합니다.

 

* 참고 사이트

 - tomcat 4.1

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

    http://okjsp.pe.kr/lecture/lec03/jndijdbc01.html

 

 - resin 2.1.x

    http://www.caucho.com/resin-3.0/db/config.xtp

   

- Resin 참고 URL 

   http://browserinsight2.lunaimaging.com:8090/ref/app-config.xtp

 

원본 출처 : http://cafe.naver.com/mjava/18 (제 네이버 까페입니다)