[tomcat] Database 연동

2025. 2. 25. 09:28Cloud

728x90
<Context>

    <!-- maxTotal: Maximum number of database connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle database connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP 2 documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWaitMillis: Maximum time to wait for a database connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL username and password for database connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->

    <!-- url: The JDBC connection url for connecting to your MySQL database.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest"/>

</Context>

 

Resource를 추가하여 Tomcat에서 JNDI DataSource를 구성한다.

tomcat/conf/context.xml 파일에서 수정하면 된다.

주요 인자

name : 데이터 소스를 참조하기 위한 이름으로, 애플리케이션에서 이 이름을 사용하여 DB 연결을 가져올 수 있다

auth : DB 인증 관리 주체 선언을 위한 요소

type : 리소스의  유형

maxTotal : 동시에 사용할 수 있는 최대 데이터베이스 연결 수

maxIdle : 유휴 상태로 유지할 수 있는 최대 연결 수

maxWaitMillis : 연결이 반환될 때까지 대기하는 최대 시간(밀리초)

username, password : 데이터베이스 연결에 사용되는 정보

driverClassName : JDBC 연결에 사용될 드라이버 클래스 이름

url : 데이터베이스 연결 문자열

 

auth : "Container" ?

Java EE 스펙에 따른 일반적인 용어를 사용하는 것으로, 이 맥락에서 Container는 웹 애플리케이션을 실행하는 환경을 의미한다. 이는 Tomcat., JBoss, WebLogic, WebSphere 등 다양한 애플리케이션 서버가 될 수 있다. 이렇게 작성하게 되면 코드가 특정 서버에 종속되지 않고 다양한 Java EE 호환 서버에서 동작할 수 있다.

 

728x90

'Cloud' 카테고리의 다른 글

Service Discovery  (0) 2025.03.13
Session Clustering 방식  (0) 2025.02.26
세션  (0) 2025.02.24
[Server] Mount  (0) 2025.02.11
grep vs awk  (0) 2025.02.11