Know/Java

DataSource를 이용한 ConnectionPool 설정

Marine™ 2006. 5. 16. 18:36
반응형
<pre>
■ Context의 ChildNode 설정

- 4.*버전대는 server.xml에 설정
- 5.*버전대는 {$catalina_home}/conf/{$domain}/{$context}.xml 파일에 설정
----------------------------------------------------------------------
<Context docBase="C:\web\webroot" debug="0" reloadable="true">
<Resource name="jdbc/Mysql" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="scott" password="tiger" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/codecode?autoReconnect=true" />
</Context>
----------------------------------------------------------------------







■ 해당 어플리케이션의 web.xml의 설정
----------------------------------------------------------------------
<description>MySQL Connection Pool</description>
<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>
----------------------------------------------------------------------



■ DataSource로 설정된 DBCP ConnectionPool객체로부터 데이타베이스 커넥션 로드
----------------------------------------------------------------------
import java.util.*;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


Connection conn = null;

try {

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

}catch(NamingException e) {
e.printStatckTrace();
}

System.out.println(conn);
----------------------------------------------------------------------
</pre>
반응형