Java--连接池和分页

连接池

分析:

涉及频繁的连接的打开、关闭,影响程序的运行效率!

连接管理:

预先创建一组连接,有的时候每次取出一个; 用完后,放回;

学习连接池:

a.  自定义一个连接池
b.  学习优秀的连接池组件
a)  DBCP
b)  C3P0

自定义连接池

程序实现思路:

    1.   指定“初始化连接数目”     【3】
           (App启动时候,就执行创建!)
    2.  指定“最大连接数”               【6】
    3. 指定“当前使用连接个数”  【不能超出最大连接数】

    代码实现:
    1.  MyPool.java  连接池类,   
    2.  指定全局参数:  初始化数目、最大连接数、当前连接、   连接池集合
    3.  构造函数:循环创建3个连接
    4.  写一个创建连接的方法
    5.  获取连接
    ------>  判断: 池中有连接, 直接拿
     ------>                池中没有连接,
    ------>                                    判断,是否达到最大连接数; 达到,抛出异常;没有达到最大连接数,
            创建新的连接
    6. 释放连接
     ------->  连接放回集合中(..)

代理

如果对某个接口中的某个指定的方法的功能进行扩展,而不想实现接口里所有方法,可以使用(动态)代理模式!
Java中代理模式:静态/动态/Cglib代理(spring)
使用动态代理,可以监测接口中方法的执行!

如何对Connection对象,生成一个代理对象:

|–Proxy

static Object newProxyInstance(
ClassLoader loader,    当前使用的类加载器
Class<?>[] interfaces,   目标对象(Connection)实现的接口类型
InvocationHandler h    事件处理器:当执行上面接口中的方法的时候,就会自动触发事件处理器代码,把当前执行的方法(method)作为参数传入。
)  

public class MyPool {

private int init_count = 3;     // 初始化连接数目
private int max_count = 6;      // 最大连接数
private int current_count = 0;  // 记录当前使用连接数
// 连接池 (存放所有的初始化连接)
private LinkedList<Connection> pool = new LinkedList<Connection>();


//1.  构造函数中,初始化连接放入连接池
public MyPool() {
    // 初始化连接
    for (int i=0; i<init_count; i++){
        // 记录当前连接数目
        current_count++;
        // 创建原始的连接对象
        Connection con = createConnection();
        // 把连接加入连接池
        pool.addLast(con);
    }
}

//2. 创建一个新的连接的方法
private Connection createConnection(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        // 原始的目标对象
        final Connection con = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "root");

        /**********对con对象代理**************/

        // 对con创建其代理对象
        Connection proxy = (Connection) Proxy.newProxyInstance(

                con.getClass().getClassLoader(),    // 类加载器
                //con.getClass().getInterfaces(),   // 当目标对象是一个具体的类的时候 
                new Class[]{Connection.class},      // 目标对象实现的接口

                new InvocationHandler() {           // 当调用con对象方法的时候, 自动触发事务处理器
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        // 方法返回值
                        Object result = null;
                        // 当前执行的方法的方法名
                        String methodName = method.getName();

                        // 判断当执行了close方法的时候,把连接放入连接池
                        if ("close".equals(methodName)) {
                            System.out.println("begin:当前执行close方法开始!");
                            // 连接放入连接池 (判断..)
                            pool.addLast(con);
                            System.out.println("end: 当前连接已经放入连接池了!");
                        } else {
                            // 调用目标对象方法
                            result = method.invoke(con, args);
                        }
                        return result;
                    }
                }
        );
        return proxy;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

//3. 获取连接
public Connection getConnection(){

    // 3.1 判断连接池中是否有连接, 如果有连接,就直接从连接池取出
    if (pool.size() > 0){
        return pool.removeFirst();
    }

    // 3.2 连接池中没有连接: 判断,如果没有达到最大连接数,创建;
    if (current_count < max_count) {
        // 记录当前使用的连接数
        current_count++;
        // 创建连接
        return createConnection();
    }

    // 3.3 如果当前已经达到最大连接数,抛出异常
    throw new RuntimeException("当前连接已经达到最大连接数目 !");
}


//4. 释放连接
public void realeaseConnection(Connection con) {
    // 4.1 判断: 池的数目如果小于初始化连接,就放入池中
    if (pool.size() < init_count){
        pool.addLast(con);
    } else {
        try {
            // 4.2 关闭 
            current_count--;
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

public static void main(String[] args) throws SQLException {
    MyPool pool = new MyPool();
    System.out.println("当前连接: " + pool.current_count);  // 3

    // 使用连接
    pool.getConnection();
    pool.getConnection();
    Connection con4 = pool.getConnection();
    Connection con3 = pool.getConnection();
    Connection con2 = pool.getConnection();
    Connection con1 = pool.getConnection();

    // 释放连接, 连接放回连接池
    //      pool.realeaseConnection(con1);
    /*
     * 希望:当关闭连接的时候,要把连接放入连接池!【当调用Connection接口的close方法时候,希望触发pool.addLast(con);操作】
     *                                                                          把连接放入连接池
     * 解决1:实现Connection接口,重写close方法
     * 解决2:动态代理
     */
    con1.close();

    // 再获取
    pool.getConnection();

    System.out.println("连接池:" + pool.pool.size());      // 0
    System.out.println("当前连接: " + pool.current_count);  // 3
}

}

DBCP连接池

 DBCP 是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文件:

•   Commons-dbcp.jar:连接池的实现
•   Commons-pool.jar:连接池实现的依赖库
   Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
   核心类:BasicDataSource

使用步骤

•   引入jar文件
   commons-dbcp-1.4.jar
   commons-pool-1.5.6.jar


public class App_DBCP {

// 1. 硬编码方式实现连接池
@Test
public void testDbcp() throws Exception {
    // DBCP连接池核心类
    BasicDataSource dataSouce = new BasicDataSource();
    // 连接池参数配置:初始化连接数、最大连接数 / 连接字符串、驱动、用户、密码
    dataSouce.setUrl("jdbc:mysql:///jdbc_demo");            //数据库连接字符串
    dataSouce.setDriverClassName("com.mysql.jdbc.Driver");  //数据库驱动
    dataSouce.setUsername("root");                          //数据库连接用户
    dataSouce.setPassword("root");                          //数据库连接密码
    dataSouce.setInitialSize(3);  // 初始化连接
    dataSouce.setMaxActive(6);    // 最大连接
    dataSouce.setMaxIdle(3000);   // 最大空闲时间

    // 获取连接
    Connection con = dataSouce.getConnection();
    con.prepareStatement("delete from admin where id=3").executeUpdate();
    // 关闭
    con.close();
}

@Test
// 2. 【推荐】配置方式实现连接池  ,  便于维护
public void testProp() throws Exception {
    // 加载prop配置文件
    Properties prop = new Properties();
    // 获取文件流
    InputStream inStream = App_DBCP.class.getResourceAsStream("db.properties");
    // 加载属性配置文件
    prop.load(inStream);
    // 根据prop配置,直接创建数据源对象
    DataSource dataSouce = BasicDataSourceFactory.createDataSource(prop);

    // 获取连接
    Connection con = dataSouce.getConnection();
    con.prepareStatement("delete from admin where id=4").executeUpdate();
    // 关闭
    con.close();
}
}
配置方式实现DBCP连接池,  配置文件中的key与BaseDataSouce中的属性一样:
db.properties的文件内容
url=jdbc:mysql:///jdbc_demo
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=3
maxActive=6
maxIdle=3000

C3P0连接池

C3P0连接池:

最常用的连接池技术!Spring框架,默认支持C3P0连接池技术!

C3P0连接池,核心类:

CombopooledDataSource ds;

使用:

1.  下载,引入jar文件:  c3p0-0.9.1.2.jar
2.  使用连接池,创建连接
a)  硬编码方式
b)  配置方式(xml),xml的文件必须是src下的c3p0-config.xml名字

@Test
//1. 硬编码方式,使用C3P0连接池管理连接
public void testCode() throws Exception {
    // 创建连接池核心工具类
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    // 设置连接参数:url、驱动、用户密码、初始连接数、最大连接数
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_demo");
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setUser("root");
    dataSource.setPassword("root");
    dataSource.setInitialPoolSize(3);
    dataSource.setMaxPoolSize(6);
    dataSource.setMaxIdleTime(1000);

    // ---> 从连接池对象中,获取连接对象
    Connection con = dataSource.getConnection();
    // 执行更新
    con.prepareStatement("delete from admin where id=7").executeUpdate();
    // 关闭
    con.close();
}

@Test
//2. XML配置方式,使用C3P0连接池管理连接
public void testXML() throws Exception {
    // 创建c3p0连接池核心工具类
    // 自动加载src下c3p0的配置文件【c3p0-config.xml】
    ComboPooledDataSource dataSource = new ComboPooledDataSource();// 使用默认的配置,如果要使用第二个,需要传入相应的配置根标签的name值(oracle_config)

    // 获取连接
    Connection con = dataSource.getConnection();
    // 执行更新
    con.prepareStatement("delete from admin where id=5").executeUpdate();
    // 关闭
    con.close();

}

xml内容

<c3p0-config>
<default-config>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo
    </property>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <property name="initialPoolSize">3</property>
    <property name="maxPoolSize">6</property>
    <property name="maxIdleTime">1000</property>
</default-config>


<named-config name="oracle_config">
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <property name="initialPoolSize">3</property>
    <property name="maxPoolSize">6</property>
    <property name="maxIdleTime">1000</property>
</named-config>
</c3p0-config>

分页

PageBean

public class PageBean<T> {
private int currentPage = 1; // 当前页, 默认显示第一页
private int pageCount = 4;   // 每页显示的行数(查询返回的行数), 默认每页显示4行
private int totalCount;      // 总记录数
private int totalPage;       // 总页数 = 总记录数 / 每页显示的行数  (+ 1)
private List<T> pageData;       // 分页查询到的数据

// 返回总页数
public int getTotalPage() {
    if (totalCount % pageCount == 0) {
        totalPage = totalCount / pageCount;
    } else {
        totalPage = totalCount / pageCount + 1;
    }
    return totalPage;
}
public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
}

public int getCurrentPage() {
    return currentPage;
}
public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
}
public int getPageCount() {
    return pageCount;
}
public void setPageCount(int pageCount) {
    this.pageCount = pageCount;
}
public int getTotalCount() {
    return totalCount;
}
public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
}

public List<T> getPageData() {
    return pageData;
}
public void setPageData(List<T> pageData) {
    this.pageData = pageData;
}
}

EmployeeDao

public class EmployeeDao implements IEmployeeDao {

@Override
public void getAll(PageBean<Employee> pb) {

    //2. 查询总记录数;  设置到pb对象中
    int totalCount = this.getTotalCount();
    pb.setTotalCount(totalCount);

    /*
     * 问题: jsp页面,如果当前页为首页,再点击上一页报错!
     *              如果当前页为末页,再点下一页显示有问题!
     * 解决:
     *     1. 如果当前页 <= 0;       当前页设置当前页为1;
     *     2. 如果当前页 > 最大页数;  当前页设置为最大页数
     */
    // 判断
    if (pb.getCurrentPage() <=0) {
        pb.setCurrentPage(1);                       // 把当前页设置为1
    } else if (pb.getCurrentPage() > pb.getTotalPage()){
        pb.setCurrentPage(pb.getTotalPage());       // 把当前页设置为最大页数
    }

    //1. 获取当前页: 计算查询的起始行、返回的行数
    int currentPage = pb.getCurrentPage();
    int index = (currentPage -1 ) * pb.getPageCount();      // 查询的起始行
    int count = pb.getPageCount();                          // 查询返回的行数


    //3. 分页查询数据;  把查询到的数据设置到pb对象中
    String sql = "select * from employee limit ?,?";

    try {
        // 得到Queryrunner对象
        QueryRunner qr = JdbcUtils.getQueryRuner();
        // 根据当前页,查询当前页数据(一页数据)
        List<Employee> pageData = qr.query(sql, new BeanListHandler<Employee>(Employee.class), index, count);
        // 设置到pb对象中
        pb.setPageData(pageData);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

@Override
public int getTotalCount() {
    String sql = "select count(*) from employee";
    try {
        // 创建QueryRunner对象
        QueryRunner qr = JdbcUtils.getQueryRuner();
        // 执行查询, 返回结果的第一行的第一列
        Long count = qr.query(sql, new ScalarHandler<Long>());
        return count.intValue();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
}

猜你喜欢

转载自blog.csdn.net/qq_17503037/article/details/80399068
今日推荐