gpt4 book ai didi

java - 需要代码在java中创建连接池

转载 作者:IT老高 更新时间:2023-10-28 21:04:34 26 4
gpt4 key购买 nike

需要在java中创建连接池的代码吗?我们如何确保连接池不会返回已在使用的相同对象?如果客户端从连接池中取出连接后关闭连接会怎样?

更新 1:

我想用简单的 Java 术语创建它,并想看看它在多线程环境中是如何工作的。我的意思是哪些方法会被同步,哪些不是。这门课也会是公共(public)课吗?如果是,那么任何人都可以访问这个类并重新初始化连接池?

更新 2:

我有一些代码如下。但我不知道如何“关闭来自池的连接将其返回到池中,它不会物理关闭连接。”另外我不明白这个“因为如果从池中借用连接但尚未返回,则它不“可用”并且不能重新分配给池的另一个客户端。”

import java.util.*;
import java.sql.*;

class ConnectionPoolManager
{

String databaseUrl = "jdbc:mysql://localhost:3306/myDatabase";
String userName = "userName";
String password = "userPass";

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 = 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[])
{
ConnectionPoolManager ConnectionPoolManager = new ConnectionPoolManager();
}

}

最佳答案

Need code to create the connection pool in java?

不确定问题是什么,但不要创建另一个连接池,使用现有的解决方案,如 C3P0 , Apache DBCP , ProxoolBoneCP (该领域的新玩家)。我会使用 C3P0。

How does we make sure that connection pool doesn't return the same object which is already in use?

因为如果一个连接已经从池中借用并且还没有返回,它只是不在池中并且不能分配给池的另一个客户端(资源被从池中移除直到它们被返回)。

How happens if client closed the connection after taking it out from Connection pool?

客户端从池中获得的连接并不是真正的 java.sql.Connection ,它是 java.sql.Connection 的包装器(代理)自定义某些方法的行为。 close() 方法就是其中之一,它 关闭 Connection 实例,而是将其返回到池中。

关于java - 需要代码在java中创建连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2826212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com