zoukankan      html  css  js  c++  java
  • 自定义数据库连接池

    数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个,应用程序和数据库创建连接需要消耗很大的资源。

    package com.lbx.myDataSource;
    
    import javax.sql.DataSource;
    import java.io.PrintWriter;
    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.sql.SQLFeatureNotSupportedException;
    import java.util.LinkedList;
    import java.util.logging.Logger;
    
    /**
     * @Author:
     * @date 2018/4/2 9:53
     */
    public class MyDataSource implements DataSource {
        //数据库连接信息
        String url = "jdbc:mysql://172.31.12.180:3306/bos";
        String user = "root";
        String password = "liaoBAOxin123...";
        //创建一个连接池,因为频繁需要增删,所以选择链表数据结构,LinkedList
        LinkedList<Connection> poll = new LinkedList<Connection>();
    
        public MyDataSource() {
            try {
                //注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                //初始化两条连接
                for (int i = 0; i < 2; i++) {
                    //创建连接
                    Connection conn = DriverManager.getConnection(url, user, password);
                    poll.add(conn);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public Connection getConnection() throws SQLException {
            //并发情况下,只能一个线程进去
            synchronized (this) {
                //如果池中没有连接,则创建连接
                if (poll.isEmpty()) {
                    for (int i = 0; i < 2; i++) {
                        //创建连接
                        Connection conn = DriverManager.getConnection(url, user, password);
                        poll.add(conn);
                    }
                }
                //removeFirst执行方法的返回值是connection
                final Connection conn = poll.removeFirst();
                //利用动态代理技术,增强close方法
                Connection proxyconn = (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if (method.getName().equals("close")) {
                            System.out.println("方法名称" + method.getName());
                            System.out.println("连接给回给连接池");
                            return poll.add(conn);
                        } else {
                            return method.invoke(conn, args);
                        }
                    }
                });
                return proxyconn;
            }
        }
    
        @Override
        public Connection getConnection(String username, String password) throws SQLException {
            return null;
        }
    
        @Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
            return null;
        }
    
        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            return false;
        }
    
        @Override
        public PrintWriter getLogWriter() throws SQLException {
            return null;
        }
    
        @Override
        public void setLogWriter(PrintWriter out) throws SQLException {
    
        }
    
        @Override
        public void setLoginTimeout(int seconds) throws SQLException {
    
        }
    
        @Override
        public int getLoginTimeout() throws SQLException {
            return 0;
        }
    
        @Override
        public Logger getParentLogger() throws SQLFeatureNotSupportedException {
            return null;
        }
    }

    “`

    package com.lbx.myDataSource;
    import org.junit.Test;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    public class MyTest {

    @Test
    public void fun1() throws SQLException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        // 1.创建自定义连接池对象
        MyDataSource dataSource = new MyDataSource();
        try {
            // 2.从池子中获取连接
            conn = dataSource.getConnection();
            String sql = "insert into t_user values(?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "张三");
            int rows = pstmt.executeUpdate();
            if (rows > 0) {
                System.out.println("添加成功!");
            } else {
                System.out.println("添加失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            conn.close();
        }
    
    }
    

    }

    源码
    https://github.com/liaobaoxin/MyDataSource

  • 相关阅读:
    vue-cli之加载ico文件
    arcgisJs之featureLayer中feature的获取
    浏览器兼容设置
    global.css
    sass之mixin的全局引入(vue3.0)
    arcgis之隐藏设置放大缩小按钮
    vue之scoped穿透
    关闭google默认打开翻译提醒
    ...args剩余参数用法
    js之向div contenteditable光标位置添加字符
  • 原文地址:https://www.cnblogs.com/aotemanzhifu/p/9192370.html
Copyright © 2011-2022 走看看