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

这篇博客探讨了如何在Java中创建一个简单的连接池管理类,用于管理数据库连接。作者通过Vector存储连接,并使用同步方法确保在多线程环境中安全地获取和返回连接。当客户端获取连接后关闭连接时,实际并未物理关闭,而是将其放回池中以供复用。博客还展示了如何检查连接池是否已满以及如何创建新的连接实例。
摘要由CSDN通过智能技术生成

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

我们如何确保连接池不返回已经在使用的同一对象?

如果客户端在从连接池中取出连接后关闭连接,会发生什么?

更新1:

我想在Simple Java术语中创建它,并希望了解它在多线程环境中的工作原理。我的意思是哪些方法将被同步,哪些不是。此类也将是一个公共类?如果是,那么任何人都可以访问此类并重新初始化连接池?

更新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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值