본문 바로가기

Dev.../플밍 관련 자료

[펌] Tomcat5 db pool 사용

#cp ojdbc14.jar tomcat5/common/lib/  //Copy JDBC Driver

 

#tomcat5/conf/server.xml

<GlobalNamingResources>

 

<ResourceParams name="jdbc/dsTicketBoxTX">
  <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
  </parameter>
  <parameter>
    <name>driverClassName</name>
    <value>oracle.jdbc.driver.OracleDriver</value>
  </parameter>
  <parameter>
    <name>url</name>
    <value>jdbc:oracle:thin:@localhost:1521:peace</value>
  </parameter>
  <parameter>
    <name>username</name>
    <value>name</value>
  </parameter>
  <parameter>
    <name>password</name>
    <value>pwd</value>
  </parameter>
  <parameter>
    <name>maxActive</name>
    <value>20</value>
  </parameter>
  <parameter>
    <name>maxIdle</name>
    <value>10</value>
  </parameter>
  <parameter>
    <name>maxWait</name>
    <value>-1</value>
  </parameter>

  <parameter>
    <name>removeAbandoned</name>
    <value>true</value>
  </parameter>

</ResourceParams>


 

</GlobalNamingResources>

 

#tomcat5/conf/Catalina/localhost/mw.xml

 

<Context path="/mw" docBase="${catalina.home}/webapps/mw"
        debug="0" privileged="true">

<ResourceLink global="jdbc/dsTicketBoxTX" name="jdbc/dsTicketBoxTX" type="javax.sql.DataSource" />

</Context>


 

#tomcat5/webapps/mw/WEB-INF/web.xml

 

<resource-ref>
 <description>Oracle Datasource example</description>
 <res-ref-name>jdbc/dsTicketBoxTX</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

 

 

#tomcat5/webapps/mw/dbTest.jsp

 

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

<%!
    String dspath = "java:comp/env/jdbc/dsTicketBoxTX";
    //String dspath = "jdbc/dsTicketBoxTX"; // Weblogic

 

    public String insertPrepared(String mdn) {
        String foo = "Not Connected";

        try{
           Context ctx = new InitialContext();
           if(ctx == null ) throw new Exception("Boom - No Context");

           DataSource ds = (DataSource)ctx.lookup(dspath);

           if (ds != null) {
                Connection conn = ds.getConnection();

                if(conn != null)  {
                        PreparedStatement stmt =
                        conn.prepareStatement(
                        " insert into tb_chnl_join (" +
                        "SEQ, CHNL_CODE, MEM_NO, SSN, NAME, MDN, " +
                        "EMAIL, REF1, REF2, REF3, REG_ID, REG_DT, MOD_ID, MOD_DT) " +
                        " values (" +
                        "SQ_TB_CHNL_JOIN_SEQ.NEXTVAL, 'CH106', '', '', ''," +
                        " ?, '', '', '', '', '', sysdate, '', sysdate)");
                        stmt.setString(1, mdn);
                        int rst = stmt.executeUpdate();
                        foo = rst + "";
                        conn.close();
                }
            }
        }catch(Exception e) {
          e.printStackTrace();
        }

        return foo;
   }

    public String selectPrepared(String mdn) {
        String foo = "Not Connected";

        try{
          Context ctx = new InitialContext();
          if(ctx == null ) throw new Exception("Boom - No Context");

          DataSource ds = (DataSource)ctx.lookup(dspath);

          if (ds != null) {
                Connection conn = ds.getConnection();

                if(conn != null)  {
                        PreparedStatement stmt = conn.prepareStatement(
                        "SELECT count(*) as count FROM TB_CHNL_JOIN  " +
                        "WHERE ROWNUM = 1 AND CHNL_CODE='CH106' AND MDN= ?");
                        stmt.setString(1, mdn);
                        ResultSet rst = stmt.executeQuery();
                        if(rst.next()) {
                           foo=rst.getString(1);
                        }
                        conn.close();
                }
          }
        }catch(Exception e) {
          e.printStackTrace();
        }

        return foo;
    }

%>


 <html>
  <head>
    <title>DB Test</title>
  </head>
  <body>

  <h2>Results =
    <%= selectPrepared(request.getParameter("MDN")) %> </h2>
    <%= insertPrepared(request.getParameter("MDN")) %> </h2>
  </body>
</html>

 

 

 

 

 

참고자료

Developer Forums : Tomcat 5.0.* global JNDI database connect pooling

http://www.jguru.com/faq/view.jsp?EID=531070 : UserTransaction