gpt4 book ai didi

java - Java 连接池

转载 作者:行者123 更新时间:2023-11-30 05:00:00 24 4
gpt4 key购买 nike

这是我实现的连接池。将所有变量和方法设置为静态是一个好的设计吗?请解释为什么或为什么不

public class MyCp1 {

private static final int MAX_SIZE=100;
private static final BlockingQueue<Connection> bq;

static{
System.out.println("Inside begin static block" );
bq= new ArrayBlockingQueue<Connection>(MAX_SIZE);
for(int i=0;i<MAX_SIZE;i++)
{
try {
try {
bq.put(makeConnection());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

System.out.println("total size:" + bq.size());
}

public static Connection getConnection() throws InterruptedException
{
System.out.println("size before getting connection "+ bq.size()+ " Thread name "+ Thread.currentThread().getName());
Connection con=bq.take();
System.out.println("size after getting connection "+ bq.size()+" Thread name "+ Thread.currentThread().getName());
return (con);
}

public static boolean releaseConnection(Connection con) throws InterruptedException
{
System.out.println("size before releasing connection "+ bq.size()+" Thread name "+ Thread.currentThread().getName());
boolean bool =bq.add(con);
System.out.println("size after releasing connection "+ bq.size()+" Thread name "+ Thread.currentThread().getName());
return (bool);
}

public static Connection makeConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "java33");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:" + "mysql" + "://"
+ "localhost" + ":" + "3306" + "/test", connectionProps);

System.out.println("Connected to database");
return conn;
}


}

我知道存在异常处理和其他问题,但如果您能坚持上述问题,我将不胜感激

编辑::

看起来使用 static 并不受欢迎。所以我尽可能地重构以消除静态。虽然这有效,但不确定这是否是好的设计

 public class ConnectionPool {

private static final int MAX_SIZE = 100;
private BlockingQueue<Connection> bq;
private static ConnectionPool cp= new ConnectionPool();


private ConnectionPool(){
System.out.println("inside constructor");
bq = new ArrayBlockingQueue<Connection>(MAX_SIZE);
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "java33");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

for (int i = 0; i < MAX_SIZE; i++) {
try {
bq.put(makeConnection(connectionProps));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

System.out.println("total size:" + bq.size());
}
public static ConnectionPool getInstance()
{
return cp;

}

public Connection getConnection() throws InterruptedException {
System.out.println("size before getting connection" + bq.size());
Connection con = bq.take();
System.out.println("size after getting connection" + bq.size());
return (con);
}

public void releaseConnection(Connection con)
throws InterruptedException {
System.out.println("size before releasing connection" + bq.size());
bq.put(con);
System.out.println("size after releasing connection" + bq.size());
//return (bool);
}

private Connection makeConnection(Properties connectionProps) throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection("jdbc:" + "mysql" + "://"
+ "localhost" + ":" + "3306" + "/test", connectionProps);

System.out.println("Connected to database");
return conn;
}

}

最佳答案

绝对不是。您拥有的更多的是一个对象回收器,如果您需要的话,这很好。

(但是,作为回收器,您仍然不需要静态字段,但您只需创建回收器的一个实例。)

对于连接池(如果是像 JDBC Connections 这样的东西),它需要是线程安全的,并且理想情况下您不需要返回连接。

线程安全的连接池将使用 ThreadLocal返回仅在该线程上使用的连接。如果不可用,它将通过执行 ThreadLocal.initialValue() 创建一个新连接.

此外,您的线程应该使用 ExecutorService 创建)这样你也可以重用线程。

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

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