본문 바로가기

JAVA/JSP & Servlet

톰캣에서 JSP 데이터베이스 커넥션 풀 사용하기 (context, 풀링)

1. contxet.xml 파일 생성 경로

 

 

 

 

2. contxet.xml 파일 내용

 

 

 

<?xml version="1.0" encoding="UTF-8" ?>

<Context>

<Resource

        name = "jdbc/oracle"

        auth="Container"

        type="javax.sql.DataSource"

        driverClassName="oracle.jdbc.driver.OracleDriver"

        url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"

        username="scott"

        password="tiger"

        maxActive="20"

        maxIdle="2"

        maxWait="5000"

        removeAbandoned="true"

        testOnBorrow="true"

        loginTimeout="10"

/>

</Context>

 

 

 

 

 

3. jsp 파일내에서 데이터베이스 접속 구문 수정

 

기존

 

 

수정 후

 

 

Context initCtx = new InitialContext();

Context envCtx = (Context)initCtx.lookup("java:comp/env");

DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");

//"jdbc/oracle" -> context.xml resource 이름

conn = dataSource.getConnection();

 

 

 

 

 

4. 톰캣서버 종료 후 재실행