java数据库连接池分析

参考地址:http://blog.csdn.net/fysuccess/article/details/66972554
1. Tomcat Jdbc Pool (已tomcat7为例)

doc文档:http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

The JDBC Connection Pool org.apache.tomcat.jdbc.pool is a replacement or an alternative to the Apache Commons DBCP connection pool.

java数据库连接池分析_第1张图片
这里写图片描述

开始学习: 测试代码


package cn.gyyx.whl.yanjiu.tomcat_jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

public class JdbcTest {
    protected static Logger log = Logger.getLogger(JdbcTest.class);
    public static void main(String[] args) throws Exception {
        PoolProperties p = new PoolProperties();
        p.setUrl("jdbc:mysql://192.168.6.109:3306/test");
        p.setDriverClassName("com.mysql.jdbc.Driver");
        p.setUsername("root");
        p.setPassword("111111");
        p.setJmxEnabled(true);
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMaxActive(5);
        p.setMaxIdle(5);
        p.setInitialSize(1);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(60);
        p.setMinEvictableIdleTimeMillis(30000);
        p.setMinIdle(1);
        p.setLogAbandoned(true);
        p.setRemoveAbandoned(true);
        p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
                + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        final DataSource datasource = new DataSource();
        datasource.setPoolProperties(p);
        Thread.sleep(5000);
        for(int i = 0;i< 7;i++){
            Thread ts = new Thread(new Runnable() {

                public void run() {
                    Connection con = null;
                    try {
                        con = datasource.getConnection();
                        Thread.sleep(5000);
                        Statement st = con.createStatement();
                        ResultSet rs = st.executeQuery("select * from student");
                        int cnt = 1;
                        while (rs.next()) {
                            log.info(Thread.currentThread().getName()+":get info");
//                            log.info((cnt++) + ". id:" + rs.getString("id")
//                                    + " name:" + rs.getString("name") + " age:"
//                                    + rs.getString("age"));
                        }

                        rs.close();
                        st.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (con != null)
                            try {
                                con.close();
                            } catch (Exception ignore) {}
                    }
                }
            });
            ts.setName("name:"+i);
            ts.start();
        }

        while(true){

        }

    }
}

理解:

java数据库连接池分析_第2张图片

你可能感兴趣的:(java基础)