连接池Java

时间:2011-08-19 20:50:38

标签: java connection pool

这是我实现的ConnectionPool。将所有变量和方法都设置为静态是一种好的设计。请解释原因或原因

public class MyCp1 {

    private static final int MAX_SIZE=100;
    private static final BlockingQueue<Connection> bq;

    static{
         System.out.println("Inside begin static block" );
        bq= new ArrayBlockingQueue<Connection>(MAX_SIZE);
        for(int i=0;i<MAX_SIZE;i++)
        {
            try {
                try {
                    bq.put(makeConnection());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        System.out.println("total size:" + bq.size());
    }

    public static Connection getConnection() throws InterruptedException
    {
        System.out.println("size before getting connection "+ bq.size()+ "  Thread name  "+ Thread.currentThread().getName());
        Connection con=bq.take();
        System.out.println("size after getting connection  "+ bq.size()+"  Thread name  "+ Thread.currentThread().getName());
        return (con);
    }

    public static boolean releaseConnection(Connection con) throws InterruptedException
    {
        System.out.println("size before releasing connection  "+ bq.size()+" Thread name  "+ Thread.currentThread().getName());
        boolean bool =bq.add(con);
        System.out.println("size after releasing connection  "+ bq.size()+"  Thread name  "+ Thread.currentThread().getName());
        return (bool);
    }

    public static Connection makeConnection() throws SQLException {
        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", "root");
        connectionProps.put("password", "java33");
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        conn = DriverManager.getConnection("jdbc:" + "mysql" + "://"
                + "localhost" + ":" + "3306" + "/test", connectionProps);

        System.out.println("Connected to database");
        return conn;
    }


}

我知道有特殊处理和其他问题,但如果你能坚持上述问题,我将不胜感激

EDIT ::

看起来使用静态不受青睐。所以我尽可能地重构以摆脱静态。虽然这有效,但不确定这是否是好的设计

 public class ConnectionPool {

    private static final int MAX_SIZE = 100;
    private    BlockingQueue<Connection> bq;
    private static ConnectionPool cp= new ConnectionPool();


    private ConnectionPool(){
        System.out.println("inside constructor");
         bq = new ArrayBlockingQueue<Connection>(MAX_SIZE);
        Properties connectionProps = new Properties();
        connectionProps.put("user", "root");
        connectionProps.put("password", "java33");
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (int i = 0; i < MAX_SIZE; i++) {
            try {
                bq.put(makeConnection(connectionProps));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        System.out.println("total size:" + bq.size());
    }
    public static ConnectionPool getInstance()
    {
        return cp;

    }

    public  Connection getConnection() throws InterruptedException {
        System.out.println("size before getting connection" + bq.size());
        Connection con = bq.take();
        System.out.println("size after getting connection" + bq.size());
        return (con);
    }

    public  void releaseConnection(Connection con)
            throws InterruptedException {
        System.out.println("size before releasing connection" + bq.size());
         bq.put(con);
        System.out.println("size after releasing connection" + bq.size());
        //return (bool);
    }

    private  Connection makeConnection(Properties connectionProps) throws SQLException {
        Connection conn = null;
        conn = DriverManager.getConnection("jdbc:" + "mysql" + "://"
                + "localhost" + ":" + "3306" + "/test", connectionProps);

        System.out.println("Connected to database");
        return conn;
    }

}

1 个答案:

答案 0 :(得分:2)

绝对不是。你拥有的更多的是一个对象回收器,如果这就是你需要的那么就好了。

(作为回收商,你仍然不想要静态字段,但你只需要创建一个回收器实例。)

对于连接池(如果是JDBC连接),它需要是线程安全的,理想情况下你不需要返回连接。

线程安全的连接池将使用ThreadLocal返回仅在该线程上使用的连接。如果没有,则会通过实施ThreadLocal.initialValue()来创建新连接。

此外,您的线程应使用ExecutorService创建,因此您也可以重用线程。