Tomcat DBCP(Data Base Connection Pool) & JNDI 사용하기
사용방법
필요한 Libraris
1. commons-dbcp.jar 1.4
2. commons-pool.jar 1.5.6
3. mysql-connector-java.jar 5.1.13 (MySQL)
4. oracle-connector.jar (Oracle)
3.4 필요한 드라이버만 설치하여도 된다
위의 드라이버들을 해당(Web Project) 의 WEB-INF/lib 에 복사
5. 위의 사항을 준비하였으면
6. Tomcat/Conf/Server.xml 화일에
<Server port="8005" shutdown="SHUTDOWN">
<GlobalNamingResources>
<!-- Resource name = "리소드네임지정" // 이것은 앞으로 리소스를 불러쓸 이름이다
auth = "Container" // 이것은 Container 즉 Tomcat 깡통이 Authorizer (권한을 같는다는것이다)
type = "java.sql.DataSource"
driverClassName = "com.mysql.jdbc.Driver" // 매우중요 드라이버설정이다 (MySQL 이다) Oracle 은
// dirverClassName = "oracle.jdbc.OracleDriver" 이다
url = "jdbc.mysql://localhost:3306/bbs" // 여기서 'bbs' 는 DataBase 이다
// localhost:3306 은 로컬데이터 베이스에 포트 3306 을 사용한다는것
// 예) jdbc.mysql://데이터베이스아이피주소또는 도메인주소:포트/데이터베이스
username = "root" // 데이터 베이스 접근 ID 이다
password = "1234" // 해당데이터 베이스 암호이다
// 중요한것은 여기까지
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
validationQuery="select 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
minEvictableIdleTimeMillis="60000"
-->
<Resource name="bbs"
global ="bbs" // link 시에 이름을 링크를 건다
auth="Container"
type="javax.sql.DataSource"
maxActive="10" maxIdle="5" maxWait="10000"
username="root" password="1234"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bbs"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
validationQuery="select 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
minEvictableIdleTimeMillis="60000"
/>
</GlobalNamingResources>
<!-- 이렇게 GlobaNamingResources 항목안에 Resource name = "이름" 으로 정의하면된다 -->
7. 이렇게 Server.xml 에 정의하였으면 이것을 불러쓸려면 방법은 몇가지가 있다
7-1. Server.xml 의 GlobalNamingResources 안쪽에 Resource name="이름" 을 정의하여 사용하는방법
7-2. Server.xml 의 GlobalNamingResources 에 올리지 않고 (방법)
Server.xm 의 해당 <Service name = "Catalina">
<Host>
<Context>
<Resource name="bbs"
global = "bbs"
auth="Container"
type="javax.sql.DataSource"
maxActive="10" maxIdle="5" maxWait="10000"
username="root" password="1234"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bbs" />
</Context>
</Host>
이렇게 넣고 사용하여도 된다
7-3. Server.xml 의 GlobalNameResource 안쪽 7-1 과 같이 설정하고
7-2. 의 Context 안쪽에 ResourceLink 필드로 정의 할수도 있다
<ResourceLink
name = "bbs" // Resouce 로 지정한 이름을 Link Name 이다
global = "bbs" /> // Resource gloabal name
7-4. 6 번항목과 같이 Server.mxl 의 GlobalNamingResources 안쪽에 Resouce name = "이름" 을 설정하고
해당프로젝트의 web.xm 에 resouce-ref (Resouce Reference) 로 정의하여 사용할수도 있다
Project/WebContent/WEB-INF/web.xml
<web-app ......................>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>bbs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet> .............
</servlet>
</web-app>
이와 같이 정의 하여 사용할수 있다
8. Java Sample
String query = "select * from person limit 100";
ResultSetMetaData rsmd = null;
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup ("bbs");
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
Statement stmt = null;
ResultSet rset = null;
try {
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()){
out.println (" " + rset.getString(1));
out.println (" " + rset.getString(2));
out.println (" " + rset.getString(3) + "<br>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try { if (rset != null) rset.close(); } catch(Exception e) { }
try { if (stmt != null) stmt.close(); } catch(Exception e) { }
try { if (conn != null) conn.close(); } catch(Exception e) { }
}
필요한 Libraris
1. commons-dbcp.jar 1.4
2. commons-pool.jar 1.5.6
3. mysql-connector-java.jar 5.1.13 (MySQL)
4. oracle-connector.jar (Oracle)
3.4 필요한 드라이버만 설치하여도 된다
위의 드라이버들을 해당(Web Project) 의 WEB-INF/lib 에 복사
5. 위의 사항을 준비하였으면
6. Tomcat/Conf/Server.xml 화일에
<Server port="8005" shutdown="SHUTDOWN">
<GlobalNamingResources>
<!-- Resource name = "리소드네임지정" // 이것은 앞으로 리소스를 불러쓸 이름이다
auth = "Container" // 이것은 Container 즉 Tomcat 깡통이 Authorizer (권한을 같는다는것이다)
type = "java.sql.DataSource"
driverClassName = "com.mysql.jdbc.Driver" // 매우중요 드라이버설정이다 (MySQL 이다) Oracle 은
// dirverClassName = "oracle.jdbc.OracleDriver" 이다
url = "jdbc.mysql://localhost:3306/bbs" // 여기서 'bbs' 는 DataBase 이다
// localhost:3306 은 로컬데이터 베이스에 포트 3306 을 사용한다는것
// 예) jdbc.mysql://데이터베이스아이피주소또는 도메인주소:포트/데이터베이스
username = "root" // 데이터 베이스 접근 ID 이다
password = "1234" // 해당데이터 베이스 암호이다
// 중요한것은 여기까지
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
validationQuery="select 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
minEvictableIdleTimeMillis="60000"
-->
<Resource name="bbs"
global ="bbs" // link 시에 이름을 링크를 건다
auth="Container"
type="javax.sql.DataSource"
maxActive="10" maxIdle="5" maxWait="10000"
username="root" password="1234"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bbs"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
validationQuery="select 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
minEvictableIdleTimeMillis="60000"
/>
</GlobalNamingResources>
<!-- 이렇게 GlobaNamingResources 항목안에 Resource name = "이름" 으로 정의하면된다 -->
7. 이렇게 Server.xml 에 정의하였으면 이것을 불러쓸려면 방법은 몇가지가 있다
7-1. Server.xml 의 GlobalNamingResources 안쪽에 Resource name="이름" 을 정의하여 사용하는방법
7-2. Server.xml 의 GlobalNamingResources 에 올리지 않고 (방법)
Server.xm 의 해당 <Service name = "Catalina">
<Host>
<Context>
<Resource name="bbs"
global = "bbs"
auth="Container"
type="javax.sql.DataSource"
maxActive="10" maxIdle="5" maxWait="10000"
username="root" password="1234"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bbs" />
</Context>
</Host>
이렇게 넣고 사용하여도 된다
7-3. Server.xml 의 GlobalNameResource 안쪽 7-1 과 같이 설정하고
7-2. 의 Context 안쪽에 ResourceLink 필드로 정의 할수도 있다
<ResourceLink
name = "bbs" // Resouce 로 지정한 이름을 Link Name 이다
global = "bbs" /> // Resource gloabal name
7-4. 6 번항목과 같이 Server.mxl 의 GlobalNamingResources 안쪽에 Resouce name = "이름" 을 설정하고
해당프로젝트의 web.xm 에 resouce-ref (Resouce Reference) 로 정의하여 사용할수도 있다
Project/WebContent/WEB-INF/web.xml
<web-app ......................>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>bbs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet> .............
</servlet>
</web-app>
이와 같이 정의 하여 사용할수 있다
8. Java Sample
String query = "select * from person limit 100";
ResultSetMetaData rsmd = null;
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup ("bbs");
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
Statement stmt = null;
ResultSet rset = null;
try {
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()){
out.println (" " + rset.getString(1));
out.println (" " + rset.getString(2));
out.println (" " + rset.getString(3) + "<br>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try { if (rset != null) rset.close(); } catch(Exception e) { }
try { if (stmt != null) stmt.close(); } catch(Exception e) { }
try { if (conn != null) conn.close(); } catch(Exception e) { }
}
댓글
댓글 쓰기
질문이나 의견은 요기에 남겨주세요 ^^,,