Tomcat DataSource 생성

2025. 2. 5. 13:14WAS/Tomcat


# 테스트 환경

  • OS : Oracle Linux Server release 8.6
  • JDK : 1.8
  • Tomcat ver. : 9.0.89
  • Oracle DB : 19c
  • JDBC Driver : ojdbc8.jar


이전 글인 멀티 인스턴스 구성 환경 중 server1에 Oracle DB를 연결하여 JDBC Connection 테스트를 진행하였다.

 

# 설정 파일 수정

1. server.xml

server.xml 내용 중 <GlobalNamingResources> 태그가 존재하며 이는 해당 server에 대한 Global JNDI 리소스를 정의하는 태그이다.

 

해당 태그로 정의된 리소스는 반드시 context.xml 파일에서 <ResourceLink> 태그 작성하여 명시적으로 연결해 주어야 한다.

 

server.xml 파일 내의 내용은 아래와 같다.

<GlobalNamingResources>
      <Resource name="jdbc/orcl" auth="Container" type="javax.sql.DataSource"
              username="ID"
              password="PASSWORD"
              url="jdbc:oracle:thin:@IP:PORT:DBNAME"
              driverClassName="oracle.jdbc.driver.OracleDriver"
              maxTotal="50"
              maxIdle="50"
              minIdle="50"
              maxWaitMillis="5000"
              initialSize="50" />
  </GlobalNamingResources>

 

  • name : 콘텍스트를 기준으로 생성할 환경 항목의 이름으로 jdbc/ 뒤의 네이밍은 설정하고자 하는 JNDI 명으로 설정
  • auth : Container가 default 값
  • type : 해당 리소스 항목에 대해 application에서 사용되는 정규화된 Java Class 이름
  • username, password : DB의 ID, PWD
  • url : DB 연결 url
  • driverClassName : JDBC driver의 class 이름으로 각 DB마다 다름
  • maxTotal : 최대 connection 개수
  • maxIdle : 유휴 connection 최대 개수
  • maxWaitMillis : connection pool에서 connection을 기다리는 최대 시간 (ms)

 

driverClassName은 각 DB마다 다르며 대표적으로 Oracle, MySQL, PostgreSQL의 driver name은 아래와 같다.

  • Oracle DB : oracle.jdbc.driver.OracleDriver
  • MySQL : com.mysql.cj.jdbc.Driver
  • PostgreSQL : org.postgresql.Driver

 

2. context.xml

context.xml의 <ResourceLink>는 server.xml의 <GlobalNamingResources>에서 정의한 리소스를 개별 웹 애플리케이션에서 참조할 수 있도록 도와주는 역할을 수행한다.

 

<ResourceLink>는 공유해야 하는 리소스를 전역으로 관리할 수 있도록 도와준다는 장점이 있다.

ex) DB Connection Pool

 

context.xml 파일의 내용은 아래와 같다.

<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <ResourceLink global="jdbc/orcl" name="jdbc/orcl" type="javax.sql.DataSource"></ResourceLink>
</Context>

 

  • global : 전역 설정된 리소스 이름
  • name : 애플리케이션 내에서 사용할 JNDI 이름 (global과 동일하게 설정)
  • type : 해당 리소스의 타입

 

 

3.  WEB-INF/web.xml

web.xml에서 작성되는 <resource-ref>는 server.xml과 context.xml에 정의된 JNDI를 애플리케이션에서 사용할 수 있도록 매핑하는 역할을 수행한다.

 

web.xml 파일의 내용은 아래와 같다.

<web-app>
   <resource-ref>
        <description>Oracle Datasource example</description>
        <res-ref-name>jdbc/orcl</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
   </resource-ref>
</web-app>

 

  • description : 하위의 내용들에 대한 정보
  • res-ref-name : 애플리케이션에서 사용할 JNDI 이름
  • res-type : 리소스의 Java Class 이름
  • res-auth : Container가 default 값

 

 

# Test 결과

어플리케이션에서 아래와 같이 resoruce-ref에 정의된 데이터 소스의 리소스를 조회하여 Test 진행

DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/orcl");
conn = ds.getConnection();

 

 

 

참고자료

https://tomcat.apache.org/tomcat-9.0-doc/config/globalresources.html

 

Apache Tomcat 9 Configuration Reference (9.0.98) - The GlobalNamingResources Component

You can declare the characteristics of resources to be returned for JNDI lookups of and elements in the web application deployment descriptor by defining them in this element and then linking them with elements in the element. You MUST also define any othe

tomcat.apache.org

 

'WAS > Tomcat' 카테고리의 다른 글

Tomcat 멀티 인스턴스 구성  (0) 2025.02.04
Jakarta EE 변경점  (0) 2025.01.06
Tomcat 버전 및 EOS 날짜  (0) 2025.01.06
Tomcat에 Application 배포하기  (0) 2025.01.02