java - 如何使用连接池

标签 java mysql connection

我是连接池的新手。我在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 在应用程序中使用它。

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

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

关于java - 如何使用连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22549433/

相关文章:

通过引用链: java. util.HashMap出现java.lang.UnsupportedOperationException

Java Applet 在 focusLost 和 focusGained 上闪烁

java - 从类创建 jar 并在其他程序中使用

MySQL 左外连接和合并结果

java - 最大TCP连接持续时间一般还是在java中?

java - 在 Spring Controller 中返回原始类型/包装器的最佳方法是什么?

javascript - 适合这种场景的技术是什么?

mysql - 实现大型数据集 : MySQL MEMORY(HEAP), Memcached 或其他的快速查找

mysql - 无法远程登录MySQL 5

java - 有没有办法在 Java App Engine 中重用 MySQL Db 连接