java

[WAS]jndi 설정

gonGon 2008. 9. 17. 23:08
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

http://java.sun.com/products/jndi/tutorial/index.html


jndi를 이용하여 DBPooling외에도 초기화된 bean객체를 생성하거나, javaMail의 session을 얻어오거나 할 수도 있다.

JDBC Data Source 얻어오는 것을 보자.

1. JDBC Driver 설치

연결하려는 DB의 jdbc driver를 $CATALINA_HOME/common/lib에 넣는다.


2. [WebApp]/WEB-INF/web.xml에 소스에서 요청할 jndi Resource를 명기한다.

<resource-ref>
  <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the server.xml file.
  </description>
  <res-ref-name>
    jdbc/EmployeeDB
  </res-ref-name>
  <res-type>
    javax.sql.DataSource
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>



3. 실제 사용은 아래와 같이 한다.

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();



4. 실제 jndi Resource는 [WebApp]/META-INF/context.xml에 아래와 같이 선언해 준다.

<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB" auth="Container"
            type="javax.sql.DataSource" username="dbusername" password="dbpassword"
            driverClassName="org.hsql.jdbcDriver" url="jdbc:HypersonicSQL:database"
            maxActive="8" maxIdle="4"/>
  ...
</Context>

cf) 나는... tomcat/conf/Catalina/localhost/[WEB-APP].xml 에 있는 Context에 작성했다.


http://markmail.org/message/vigu4iein5d3xvkh

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

https://issues.apache.org/jira/browse/DBCP-134?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel



요것도 보자. 보통 Connection을 얻어서 try절 내에서 close한 후 finally에서 확인사살을 하는데
이 때 already closed connection error가 생길 수 있다.
이 때의 해결법.