자바 웹 에서 데이터베이스 연결 탱크 proxool 설정 을 사용 합 니 다.

5354 단어 Java Web
proxool 데이터베이스 연결 탱크 사용 절차:
  • WEB-INF lib 에서 proxool-0.9.0RC3.jar
  • 가 져 오기
  • WEB-INF 에 XML 파일 을 새로 만 들 었 습 니 다.proxool.xml
  • proxool.xml 의 내용 은 다음 과 같다.
    <?xml version="1.0" encoding="UTF-8"?>
    <!--
    the proxool configuration can be embedded within your own application's. Anything outside the "proxool" 
    
    tag is ignored.
    -->
    <something-else-entirely>
         <proxool>
                    <alias>smms</alias> <!--      -->
                    <driver-url>jdbc:mysql://127.0.0.1:3306/smms</driver-url><!--url   -->
                    <driver-class>com.mysql.jdbc.Driver</driver-class><!--   -->
                    <driver-properties>
                            <property name="user" value="root" /><!--   -->
                            <property name="password" value="root" /><!--  -->
                    </driver-properties>         
                    <!--     (   5 ),        ,     ,        ,         
    
    maximum-new-connections  -->
                    <maximum-connection-count>100</maximum-connection-count>
                    <!--     (  2 )-->
                    <minimum-connection-count>10</minimum-connection-count>
                    <!--proxool               (  ),             ,        
    
     30 -->
                    <house-keeping-sleep-time>90000</house-keeping-sleep-time>
                    <!--                       ,                  -
    
    ->
                    <maximum-new-connections>10</maximum-new-connections>
                    <!--          (  2 )-->
                    <prototype-count>5</prototype-count>
                    <!--       -->
                    <test-before-use>true</test-before-use>
                    <!--             -->
                    <house-keeping-test-sql>select id from t_sysadmin</house-keeping-test-sql>
         </proxool>
    </something-else-entirely>
  • 웹.xml 에 다음 노드 를 추가 합 니 다.
      <servlet>     
       <servlet-name>ServletConfigurator</servlet-name>     
       <servlet-class>     
         org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>     
       <init-param>     
         <param-name>xmlFile</param-name>     
         <param-value>WEB-INF/proxool.xml</param-value>     
       </init-param>     
       <load-on-startup>1</load-on-startup>     
    </servlet>
  • 데이터베이스 조작 류 는 다음 과 같다.
    package cn.edu.hbcit.smms.dao.databasedao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import org.apache.log4j.Logger;
    
    import org.logicalcobwebs.proxool.ProxoolException;       
    import org.logicalcobwebs.proxool.ProxoolFacade;       
    import org.logicalcobwebs.proxool.admin.SnapshotIF;
    /**
     *       
     * @author lw
     *
     */
    public class DBConn {
    	private static Connection conn;
    	private static ResultSet rs;
    	private static PreparedStatement ps;
    	private static String driverClass = "org.logicalcobwebs.proxool.ProxoolDriver";//proxool   
    	//private static String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=123";
    	private static int activeCount = 0; //     
    	protected final Logger log = Logger.getLogger(DBConn.class.getName());
    	
        /**
         *        
         * @return Connection
         */
    	public Connection getConn() {
    		try {
    			Class.forName(driverClass); 
    			conn = DriverManager.getConnection("proxool.smms"); //   smms  proxool.xml         
    			this.showSnapshotInfo(); //       
    		} catch (Exception e) {
    			log.error(e.getMessage());
    			log.debug("       !");
    			//System.out.println(e.getMessage());
    			//System.out.println("       !");
    		}
    		return conn;
    	}
    	
    	/**     
         *          
         * freeConnection     
         * @param conn     
         */
    	public void freeConnection(Connection conn){
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    	
    
    	/**
    	 *            
    	 */
    	private void showSnapshotInfo(){
            try{       
                SnapshotIF snapshot = ProxoolFacade.getSnapshot("smms", true);       
                int curActiveCount=snapshot.getActiveConnectionCount();//              
                int availableCount=snapshot.getAvailableConnectionCount();//                
                int maxCount=snapshot.getMaximumConnectionCount() ;//             
                if(curActiveCount!=activeCount)//                     
                {
                	log.debug("     :"+curActiveCount+"(active);       :"+availableCount+"(available);    :"+maxCount+"(max)");
                 //System.out.println("     :"+curActiveCount+"(active);       :"+availableCount+"(available);    :"+maxCount+"(max)");                    
                 activeCount=curActiveCount;       
                }
            }catch(ProxoolException e){       
                e.printStackTrace();       
            }       
        }  
    
    }
    
  • 좋은 웹페이지 즐겨찾기