달력

5

« 2024/5 »

  • 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

'#jsp'에 해당되는 글 5

  1. 2017.07.02 자카르타 커넥션풀 1
2017. 7. 2. 22:51

자카르타 커넥션풀 1 C Jsp2017. 7. 2. 22:51

자카르타 프로젝트의 DBCP API를 사용할 때 과정


---------------------------------------------------------------------------
   1. DBCP 관련 Jar 파일 및 JDBC 드라이버 Jar 파일 설치하기
   2. server.xml (톰캣홈/conf/server.xml)
   3. JNDI 리소스 사용설정 -web.xml설정
   4. 커넥션 사용하기
----------------------------------------------------------------------------


1. 필요한 Jar 파일 복사 : DBCP API를 사용하기 위해서는 다음과 같은 라이브러리가 필요하다.
                                    라이브러리의 최신 버전은 http://jakarta.apache.org/site/binindex.cgi 에서 다운로드

   * DBCP API 관련 Jar 파일                                        
   * DBCP API가 사용하는 자카르타 Pool API의 Jar 파일
   * Pool API가 사용하는 자카르타 Collection API의 Jar 파일 

     - DBCP 1.2.1 - commons-dbcp-1.2.1.zip
     - Pool 1.2 - commons-pool-1.2.zip
     - Collection 3.1 - commons-collections-3.1.zip

   이들 파일의 압축을 풀면 다음과 같은 Jar 파일들이 있는데, 이들 Jar 파일들을  \WEB-INF\lib 폴더에 복사한다
      commons-dbcp-1.2.1.jar
      commons-pool-1.2.jar
      commons-collections-3.1.jar


2. server.xml (톰캣홈/conf/server.xml)

   server.xml 
   --------------------------------------------------------------------------------------------------------(</GlobalNamingResources> 위에 넣을내용)
 
   <Resource                  name ="jdbc/jsptest" 
                                       auth ="Container"
                                       type ="javax.sql.DataSource"
                    driverClassName ="oracle.jdbc.driver.OracleDriver"

                           loginTimeout ="10"
                                 maxWait ="5000"
                               username ="scott"
                               password ="tiger"
                                           url ="jdbc:oracle:thin:@localhost:1521:ORCL"
     />  
 


(  </Host> 위에 넣을내용 )

<Context path="" docBase="C:\AjaxEx\" debug="1" crossContext="true">
     <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_JspTest_log" suffix=".txt" timestamp="true" />

 
     <Resource                  name ="jdbc/jsptest" 
                                         auth ="Container"
                                         type ="javax.sql.DataSource"
                      driverClassName ="oracle.jdbc.driver.OracleDriver"

                             loginTimeout ="10"
                                   maxWait ="5000"
                                 username ="scott"
                                 password ="tiger"
                          testOnBorrow ="true"
                                             url ="jdbc:oracle:thin:@localhost:1521:ORCL"
       />  
 
 </Context>

   ---------------------------------------------------------------------------------------------------------------

Context           - path : URL 호출시 사용될 이름
                  - docBase : 실제 웹 어플리케이션이 위치한 폴더명
                      - debug : 로그 작성 레벨
            -    reloadable : 톰캣서버의 재시작 없이 수정된 내용을 불러오기 위한 옵션
           - crossContext : myapp이외의 Context에서도 사용 가능하도록 하는 옵션

Resource        - name : Resource명칭(JNDI Lookup 시 사용할 명칭)
                        - auth : Resource 관리자를 지정. 여기서는 톰캣컨테이너가 관리자임
                         - type : Resource 의 형태 지정. 데이타소스 형태를 지정
                - maxActive : 최대 연결 가능한 Connection 숫자를 지정함
                  - maxIdle : Connection pool 유지를 위해 최대 대기 connection 숫자
                 - maxWait : Connection 재 사용을 위해 대기해야 하는 최대 시간(단위:ms)
               - username : DB접속 계정
               - password : DB접속 암호
    - driverClassName : DB와 접속하기 위한 driver 클래스를 지정. 예에서는 MySql임
                           - url : 접속한 DB가 위치한 서버명, 포트명, 사용할 데이타베이스명을 지정

 

3. JNDI 리소스 사용설정 -web.xml설정

   web.xml설정
   -----------------------------------------------------------------------------------------------------------------------
 <resource-ref>
              <description> jsptest DB                  </description>       ->설명기술
          <res-ref-name> jdbc/jsptest                </res-ref-name>    ->server.xml의 <Resource>태그의 name속성과 매치
                 <res-type> javax.sql.DataSource  </res-type>           ->server.xml의 <Resource>태그의 type속성과 매치
                 <res-auth> Container                    </res-auth>          ->server.xml의 <Resource>태그의 auth속성과 매치
</resource-ref>
   ---------------------------------------------------------------------------------------------------------------------


  4. 커넥션 사용하기 
   -------------------------------------------------------------------------------------------------------------------
    import java.sql.*;
    import javax.sql.*;
    import javax.naming.*;
    ....

    try{
          Context        initCtr  = new InitialContext();                                      -> InitialContextr객체생성(server.xml의 context를 받을객체)
          Context        envCtx = (Context)initCtx.lookup("java:comp/env");    -> java:comp/env 안에 기술된 이름(Context객체)을 찾는다.
          DataSource  ds        = (DataSource)envCtx.lookup("jdbc/jsptest");  -> 위에서 찾은객체를 가지고 jdbc/jsptest이름의 객체를 리턴
                                                                                                                         받아서 DataSource객체로 형변환한다.
          Connection   conn    = ds.getConnection();                                       -> DataSource 객체의 getConnection()메소드를 가지고
                                                                                                                         커넥션 객체를 얻어낸다.
    }...
   ------------------------------------------------------------------------------------------------------------------


'C Jsp' 카테고리의 다른 글

input 정렬  (0) 2017.07.02
select box 정렬  (0) 2017.07.02
자카르타 커넥션풀 2  (0) 2017.07.02
JSP 에러페이지 처리  (0) 2017.07.02
액션태그  (0) 2017.07.02
:
Posted by sfeg