달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

http://jakarta.apache.org/commons/dbcp/apidocs/org/apache/commons/dbcp/package-summary.html#package_description

 

여기 내용보고 열쉬미 멜링 리스트 부터.. cvs... google 을 뒤진 결과.... 아주 간단하게

풀을 사용하는 법을 알아냄...

 

커먼스 관련 lib 는 최신으로 도배함 ㅡㅡ

 

1. jocl 만들기...

  파일 이름은 intranet.jocl 로 했음.. 저장은 classes 에 함...

  각 설명은 해당 내용 옆의 주석을 참고하면 됨

 

<object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">

  <object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
    <string value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=euc-kr" /> <!-- jdbcurl -->
    <string value="root" />  <!-- user -->
    <string value="123456" />  <!-- password -->
  </object>
 
  <object class="org.apache.commons.pool.impl.GenericObjectPool">
    <object class="org.apache.commons.pool.PoolableObjectFactory" null="true" />
    <int value="80" />  <!-- max active -->
    <byte value="1" />  <!-- when exhausted action, 0 = fail, 1 = block, 2 = grow -->
    <long value="2000" />  <!-- max wait -->
    <int value="10" />  <!-- max idle -->
    <boolean value="true" />  <!-- test on borrow -->
    <boolean value="true" />  <!-- test on return -->
    <long value="10000" />     <!-- time between eviction runs -->
    <int value="5" />   <!-- number of connections to test per eviction run -->
    <long value="5000" />    <!-- min evictable idle time -->
    <boolean value="true" />   <!-- test while idle -->
  </object>
 
  <object class="org.apache.commons.pool.impl.StackKeyedObjectPoolFactory">
    <int value="10" />
  </object>
 
  <string value="select now()" />  <!-- validation query -->
  <boolean value="false" />  <!-- default read only -->
  <boolean value="true" /><!-- default auto commit -->
</object>

 

 

2. 해당 jocl 을 읽어 들여 서블릿에서 사용하기 위해 ServletListener 를 만듬..

 

/*
 * Created on 2004. 9. 7.
 * Package Name : com.gndsoft.action.admin
 * Version :
 * Author : 유희성(over30tornado@msn.com)
 *
 * History : 2004. 9. 7. 유희성(over30tornado@msn.com) 최초 작성
 *

 *
 */

 

package com.gndsoft.intranet.db;

 

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

 

/**
 * @author Tornado
 *
 */


public class DBCPPoolListener implements ServletContextListener {


 public void contextInitialized(ServletContextEvent sce) {
 
        try {
           
         Class.forName("com.mysql.jdbc.Driver");

 

         Class.forName("org.apache.commons.dbcp.PoolingDriver");
           
         System.setProperty( "org.xml.sax.driver",  "org.apache.xerces.parsers.SAXParser" );

 

        } catch(Exception ex) {
        
         ex.printStackTrace();
        
        }
 }


 public void contextDestroyed(ServletContextEvent sec) {

     // 커먼스 풀에서 뭔가 닫아줘야 할 경우 작성하면 된다.

 }

 

}

 

-------

Class.forName() 에서 뭘 적어줘야 할지 메일링리스트에서 찾다가 질문에 올라온거로 해서 성공함 ㅡㅡ

 

System.setProperty() 때문에 메일링 리스트 엄청 뒤짐 ㅜㅜ ... 저거 몰라서 열라 헤맴..

 

3. web.xml 에 해당 Listener 등록

 

<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <!-- Context Param -->

 <context-param>
      <param-name>poolName</param-name>
      <param-value>intranet</param-value>
    </context-param>
 
 <listener>
   <listener-class>com.gndsoft.intranet.db.DBCPPoolListener</listener-class>
 </listener>

</web-app>

 

 

-------

Pool 이름이 하드코딩될까 두려운 나머지.. 테스트 하는데 이름을 Init-param 으로 뺌 --

 

 

4. Test 를 하기 위한 JSP 작성

 

<%@ page contentType="text/html; charset=euc-kr" %>

<%@ page import="java.sql.*" %>

<%
 
 Connection conn = null;
 Statement stmt = null;
 ResultSet rs = null;
 
 try{
 
  String name = application.getInitParameter("poolName");
  
  if(name == null || "".equals(name) )  name = "intranet";
  
  conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/" + name);
  
  stmt = conn.createStatement();
  
  rs = stmt.executeQuery("select now()");
  
  while(rs.next()){
  
   out.print( rs.getString(1) );
  }
  
 }catch(Exception e){
 
     e.printStackTrace();
    
 }finally{
 
  try{ if(rs != null) rs.close(); }catch(Exception e){ e.printStackTrace(); }
  
  try{ if(stmt != null) stmt.close(); }catch(Exception e){ e.printStackTrace(); }
  
  try{ if(conn != null) conn.close(); }catch(Exception e){ e.printStackTrace(); }    
  
 }
 

%>

 

끝~!

 

DriverManager.getconnection() 할 때 jdbc URL 이 아닌 커먼스 풀을 적어줘야 풀링된 자원을 가져온다고 함...

몇일 써보고 좋으면 바로 적용~~~~

 

 

Posted by tornado
|