0
点赞
收藏
分享

微信扫一扫

JDBC 数据库连接池的简单实现


连接池代码:

public class MyDataSource2{

    private static String url = "jdbc:mysql://localhost:3306/jdbc";

    private static String user = "root";

    private static String password = "";

    private static int initCount = 1;

    private static int maxCount = 1;

    int currentCount = 0;

    LinkedList<Connection> connectionsPool = new LinkedList<Connection>();

    public MyDataSource2() {

        try {

            for (int i = 0; i < initCount; i++) {

                this.connectionsPool.addLast(this.createConnection());

                this.currentCount++;

            }

        } catch (SQLException e) {

            throw new ExceptionInInitializerError(e);

        }

    }

    public Connection getConnection() throws SQLException {

        synchronized (connectionsPool) {

            if (this.connectionsPool.size() > 0)

                return this.connectionsPool.removeFirst();

            if (this.currentCount < maxCount) {

                this.currentCount++;

                return this.createConnection();

            }

            throw new SQLException("已没有链接");

        }

    }

    public void free(Connection conn) {

        this.connectionsPool.addLast(conn);

    }

    private Connection createConnection() throws SQLException {

        Connection realConn = DriverManager.getConnection(url, user, password);

         MyConnection myConnection = new MyConnection(realConn, this);

         return myConnection;

    }



//自建Connection类,重写了close方法,并且限定了同一个Connection的最大使用次数,其它的都使用realConnection的方法

public class MyConnection implements Connection {

    private Connection realConnection;

    private MyDataSource2 dataSource;

    private int maxUseCount = 5;

    private int currentUserCount = 0;

    MyConnection(Connection connection, MyDataSource2 dataSource) {

        this.realConnection = connection;

        this.dataSource = dataSource;

    }


    public void close() throws SQLException {

        this.currentUserCount++;

        if (this.currentUserCount < this.maxUseCount)

            this.dataSource.connectionsPool.addLast(this);

        else {

            this.realConnection.close();

            this.dataSource.currentCount--;//这段是限定同一个链接的使用次数,如果这个MyConnection对象被重复使用过好多次了,就释放了它。

            //但是这么做有什么意义?同一个链接使用了很多次会造成什么严重后果吗?这块我真是百思不得其解

        }

    }


       ……

}




public final class JdbcUtils {

    private static MyDataSource2 myDataSource = null;

    private JdbcUtils() {

    }

    static {

        try {

            Class.forName("com.mysql.jdbc.Driver");

             myDataSource = new MyDataSource2();//放在这个地方,要加载后才能初始化

            } catch (Exception e) {

            throw new ExceptionInInitializerError(e);

        }

    }

    public static DataSource getDataSource() {

        return myDataSource;

    }

    public static Connection getConnection() throws SQLException {

        return myDataSource.getConnection();

    }

    public static void free(ResultSet rs, Statement st, Connection conn) {

        try {

            if (rs != null)

                rs.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            try {

                if (st != null)

                    st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            } finally {

                if (conn != null)

                    try {

                        conn.close();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

            }

        }

    }

}


举报

相关推荐

0 条评论