繁体   English   中英

如何使用连接池

[英]How to use connection pooling

我是连接池的新手。我在mysql中创建了一个连接池,该连接池添加了五个连接。现在我想知道连接池的应用是什么,即在创建该池后如何使用它。这里

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.Connection;

class ConnectionPoolManager {

String databaseUrl = "jdbc:mysql://localhost:3306/homeland";
String userName = "root";
String password = "root";

Vector connectionPool = new Vector();

public ConnectionPoolManager() {
    initialize();
}

public ConnectionPoolManager(
// String databaseName,
        String databaseUrl, String userName, String password) {
    this.databaseUrl = databaseUrl;
    this.userName = userName;
    this.password = password;
    initialize();
}

private void initialize() {
    // Here we can initialize all the information that we need
    initializeConnectionPool();
}

private void initializeConnectionPool() {
    while (!checkIfConnectionPoolIsFull()) {
        System.out
                .println("Connection Pool is NOT full. Proceeding with adding new connections");
        // Adding new connection instance until the pool is full
        connectionPool.addElement(createNewConnectionForPool());
    }
    System.out.println("Connection Pool is full.");
}

private synchronized boolean checkIfConnectionPoolIsFull() {
    final int MAX_POOL_SIZE = 5;

    // Check if the pool size
    if (connectionPool.size() < 5) {
        return false;
    }

    return true;
}

// Creating a connection
private Connection createNewConnectionForPool() {
    Connection connection = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = (Connection) DriverManager.getConnection(databaseUrl,
                userName, password);
        System.out.println("Connection: " + connection);
    } catch (SQLException sqle) {
        System.err.println("SQLException: " + sqle);
        return null;
    } catch (ClassNotFoundException cnfe) {
        System.err.println("ClassNotFoundException: " + cnfe);
        return null;
    }

    return connection;
}

public synchronized Connection getConnectionFromPool() {
    Connection connection = null;

    // Check if there is a connection available. There are times when all
    // the connections in the pool may be used up
    if (connectionPool.size() > 0) {
        connection = (Connection) connectionPool.firstElement();
        connectionPool.removeElementAt(0);
    }
    // Giving away the connection from the connection pool
    return connection;
}

public synchronized void returnConnectionToPool(Connection connection) {
    // Adding the connection from the client back to the connection pool
    connectionPool.addElement(connection);
}

public static void main(String args[]) {
    new  ConnectionPoolManager();
}

}

有人可以帮忙吗?

连接池的目的是维护与数据库的许多打开的连接,以便当您的应用程序需要连接时,它不必经历可能耗费资源和时间的打开新连接的过程。

当应用程序需要数据库连接时,它将从池中“借用”一个。 完成后,它会退还给您,以后可以重新使用该连接。

一旦获得连接,就可以通过JDBC(Java数据库连接)API在应用程序中使用它。

可以在http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html上找到Oracle使用JDBC的基本教程。

要记住的另一件事是,很多工作已经投入到开发连接池中,并且可能不需要重新发明轮子,除非可能是作为学习练习。 Apache Tomcat的连接池实现可以在Tomcat之外使用(例如,在独立的Java应用程序中),并且相当灵活且易于配置。 可以在https://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html中找到

我会说代码很容易解释。

您个人创建了一个池实例,我更喜欢使用单例,但这是另一个主题

ConnectionPoolManager connectionPoolManager = new  ConnectionPoolManager();

现在,每个想要建立连接的机构都需要向该管理器提供参考。 必要时,您可以请求池中的免费连接...

public void queryDatabaseForStuff(ConnectionPoolManager cpm) throws SQLException {
    Connection con = cpm.getConnectionFromPool();
    //....

连接完成后,将其传递回经理...

 try {
     //...
 } finally {
     cmp.returnConnectionToPool(con);
 }

现在。 您可能想研究一个阻止过程,该过程将在池为空时阻止当前对getConnectionFromPool调用,这意味着它将抛出异常(如果要包含超时功能)或有效连接。

重新池化Connection ,您可能想检查Connection是否已关闭,并进行某种复兴,以确保池的容量接近容量...

请查看此链接以获取详细的答案-https: //examples.javacodegeeks.com/core-java/sql/jdbc-connection-pool-example/

您无需重新创建Connection对象池,而请使用Apache提供的库。 请清除以下内容:1-为什么以及为什么让您想到连接池? 2-在您的Maven项目中使用以下Apache commons-dbcp lib,然后根据文档使用这些类。 3.这样可以解决您所有的问题吗?

暂无
暂无

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

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