简体   繁体   English

JDBC 使用连接池查询

[英]JDBC Query using connection pool

I have a connection pool interface:我有一个连接池接口:

public interface ConnectionPool {
    Connection getConnection() throws SQLException;
    boolean releaseConnection(Connection connection);
}

I need to implement this methods.我需要实现这个方法。 I found an example of realization of connection pool.我找到了一个实现连接池的例子。 Here is the code:这是代码:

public class ConnectionPoolImpl implements ConnectionPool{

    private List<Connection>availableConnections = new ArrayList<Connection>();
    private List<Connection>usedConnections = new ArrayList<Connection>();
    private final int MAX_CONNECTIONS = 5;

    private String URL;
    private String USERID;
    private String PASSWORD;


    /** Initialize all 5 Connections and put them in the Pool **/
    public ConnectionPoolImpl(String Url, String UserId, String password) throws SQLException {
        this.URL = Url;
        this.USERID = UserId;
        this.PASSWORD = password;

        for (int count = 0; count <MAX_CONNECTIONS; count++) {
            availableConnections.add(this.createConnection());
        }
    }

    /** Private function,
     used by the Pool to create new connection internally **/

    private Connection createConnection() throws SQLException {
        return DriverManager
                .getConnection(this.URL, this.USERID, this.PASSWORD);
    }


    /** Public function, used by us to get connection from Pool **/
    public Connection getConnection() {
        if (availableConnections.size() == 0) {
            System.out.println("All connections are Used !!");
            return null;
        } else {
            Connection con =
                    availableConnections.remove(
                            availableConnections.size() - 1);
            usedConnections.add(con);
            return con;
        }
    }

    /** Public function, to return connection back to the Pool **/
    public boolean releaseConnection(Connection con) {
        if (null != con) {
            usedConnections.remove(con);
            availableConnections.add(con);
            return true;
        }
        return false;
    }

    /** Utility function to check the number of Available Connections **/
    public int getFreeConnectionCount() {
        return availableConnections.size();
    }

}

Now I have a problem of using this pool in my DAO.现在我在我的 DAO 中使用这个池时遇到了问题。

My DAO realization:我的DAO实现:

public class EmployeeImpl implements Employee {

private ConnectionPool connectionPool;

public Employee getById(Long id) {
        try (Connection connection = connectionPool.getConnection();
            PreparedStatement statement = connection.prepareStatement(SELECT_QUERY))
        {
       // 
            }
    

     return employee;
}

I dont know how its better to connect to my pool.我不知道如何更好地连接到我的游泳池。 Better create object ConnectionPoolImpl like this in my EmployeeImpl class:最好在我的 EmployeeImpl class 中像这样创建 object ConnectionPoolImpl:

ConnectionPoolImpl conn = new ConnectionPoolImpl("MyUrl", "user", "password");

and then conn.getConnection to each my query.然后conn.getConnection到我的每个查询。 But this realization doesn't include my interface (field).但是这个实现不包括我的界面(字段)。 How it is better to connect to my pool?如何更好地连接到我的游泳池? The only constraint is I need to implement my interface.唯一的限制是我需要实现我的接口。 Explanation will be appreciated.解释将不胜感激。

I would use like HikariCP connection pooling library to handle database connection pool effectively and configure the number of connections than handling it as described in your questions because there are a lot of things to take care which in my view better to use these kind of pooling library and wrap it to the implementation of ConnectionPool .我会使用像HikariCP连接池库来有效地处理数据库连接池并配置连接数,而不是按照您的问题中描述的那样处理它,因为有很多事情需要注意,在我看来更好地使用这些池库并将其包装到ConnectionPool的实现中。

For the implementation I would prefer to have constructor in EmployeeImpl with parameter ConnectionPool .对于实现,我希望在EmployeeImpl中使用带有参数ConnectionPool的构造函数。 I would create ConnectionPool instance with ConnectionPool conn = new ConnectionPoolImpl("MyUrl", "user", "password");我将使用ConnectionPool conn = new ConnectionPoolImpl("MyUrl", "user", "password");创建ConnectionPool实例and while creating EmployeeImpl instance I would pass the conn in this way my EmployeeImpl is tied to the interface not the implementation which gives me the flexibility of changing the ConnectionPool implementation and does not require any kind of update in EmployeeImpl if I want to create another implementation of ConnectionPool .在创建EmployeeImpl实例时,我会以这种方式传递conn ,我的EmployeeImpl绑定到接口而不是实现,这使我可以灵活地更改ConnectionPool实现,并且如果我想创建另一个实现,则不需要EmployeeImpl中的任何更新的ConnectionPool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM