手写数据库连接池附gp连接jar包地址

手写数据库连接并,测试.

最近数据库要连接GP数据库(GreenplumSQL),在建立连接的时候需要做建立不同的连接数量.

其实当想到写数据库连接时,完全可以通过springdata jpa直接写接口,这是一种思路.

所以在使用的使用,就写了个demo,测试,建立连接所需要的时间,和服务器的性能真的有很大的关系,具体关系后面分析..

package com.trs.idap.config;


import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 * Created by Administrator on 2018/10/12.
 * 描述:自定义数据库连接池
 * @author Young
 * @create 2018-10-12 17:25
     代码实现:
     1.  MyPool.java  连接池类,
     2.  指定全局参数:  初始化数目、最大连接数、当前连接、   连接池集合
     3.  构造函数:循环创建3个连接
     4.  写一个创建连接的方法
     5.  获取连接
             判断: 池中有连接, 直接拿
             池中没有连接,
             判断,是否达到最大连接数; 达到,抛出异常;没有达到最大连接数,
     创建新的连接
     6. 释放连接
         连接放回集合中(..)
 */
@Component
public class CustomConnectionPool {
    private int intCount=50;
    private int maxCount=20;
    private int currentCount;
    //连接池
    private LinkedList<Connection> pool = new LinkedList<>();

    //构造方法,初始化连接池
    public CustomConnectionPool() {
        for (int i = 0 ;i<intCount;i++){
            currentCount++;
            pool.add(this.createConnection());
        }
    }
    //创建一个新的连接的方法
    private Connection createConnection() {
        try {
            //加载驱动程序
            Class.forName("com.pivotal.jdbc.GreenplumDriver");
            //原始的目标对象
            final Connection conn = DriverManager.getConnection("jdbc:pivotal:greenplum://192.168.1.228:5432;DatabaseName=idap3", "gpadmin", "gpadmin");

            /**对Connection对象代理**/
            //对conn创建其代理对象。返回一个指定接口的代理类实例
            Connection proxy = (Connection) Proxy.newProxyInstance(
                conn.getClass().getClassLoader(),// 定义代理类的类加载器。负责加载类的对象。
                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();
                        if("close".equals(methodName)){
                            //连接放入连接池
                            pool.add(conn);
                        }else{
                            result = method.invoke(conn, args);//// 调用目标对象方法
                        }
                        return result;
                    }

                }//指派方法调用的调用处理程序
            );
            return proxy;//返回代理对象
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
        //创建连接
    public Connection getConnection(){
        //3.1 判断连接池中是否有连接,如果有连接,就直接从连接池取出
        if(!pool.isEmpty()){
            return pool.removeFirst();//删除一个,并返回删除的对象Connection
        }
        //3.2连接池中没有连接:判断,如果没有达到最大连接数,创建
        if(currentCount < maxCount){
            //记录当前使用的连接数
            currentCount++;
            //创建连接
            return this.createConnection();
        }
        //3.3如果当前已经达到最大连接数,抛出异常
        throw new RuntimeException("当前连接已经达到最大连接数目");
    }

    /**
     * 4.释放连接(手动释放)
     */
    public void realeaseConnection(Connection conn){
        //4.1 判断,池的数目如果小于初始化连接,就放入池中
        if(pool.size() < intCount){
            pool.addLast(conn);
        }else{
            //4.2关闭
            try {
                currentCount--;//当前连接数-1
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        CustomConnectionPool pool = new CustomConnectionPool();
        System.out.println("创建50个连接用时"+(System.currentTimeMillis()-l)/1000);
        System.out.println("连接池:" + pool.pool.size() + "当前连接:" + pool.currentCount);
        Connection connection = pool.getConnection();
        System.out.println("连接池:" + pool.pool.size() + "当前连接:" + pool.currentCount);
        Connection connection1 = pool.getConnection();
        System.out.println("连接池:" + pool.pool.size() + "当前连接:" + pool.currentCount);
    }
}

BTY:

另外,由于在maven仓库中没有找到gp的连接坐标,所以需要手动压入到本地的pom中,so....如下

由于Greenplum在Maven中未能找到与之匹配的jar驱动,所以需要自己构建本地仓库
(1) cmd 打开运行窗口
(2) 进入  本地项目名称\lib目录
(3) 运行mvn install:install-file -Dfile=greenplum.jar -DgroupId=lib.greenplum -DartifactId=greenplum -Dversion=5.10.2 -Dpackaging=jar
(4) 配置工程的pom.xml.添加pom路径
       <dependency>
            <groupId>lib.greenplum</groupId>
            <artifactId>greenplum</artifactId>
            <version>5.10.2</version>
        </dependency>

附gp连接jar

https://pan.baidu.com/s/1S4UtOlEoHynDhwMbXbNKAg

猜你喜欢

转载自blog.csdn.net/YoungLee16/article/details/83037314