Java 连接池
这是我实现的连接池。将所有变量和方法设置为静态是一个好的设计吗?请解释为什么或为什么不
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;
}
}
我知道存在异常处理和其他问题,但如果您能坚持上述问题,我将不胜感激
编辑::
看起来使用静态不受欢迎。所以我尽可能地重构以消除静态。虽然这有效,但不确定这是否是好的设计
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;
}
}
Here is a ConnectionPool that i implemented. Is it a good design to have all variables and methods as static. Please explain why or why not
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;
}
}
I know there are issues with exceptional handling and others, but i would appreciate if you can please stick to the above mentioned question
EDIT::
It looks like using static is not favored. So I refactored as much as i could to get rid of static. While this works, not sure if this is good design
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对不是。您拥有的更多的是对象回收器,如果这是您需要的,那就很好。
(但是,作为回收器,您仍然不需要静态字段,但只需创建回收器的一个实例。)
对于连接池(如果是用于 JDBC 连接之类的连接池),它需要是线程安全的,理想情况下您不需要返回连接。
线程安全的连接池将使用 ThreadLocal 返回一个仅在该线程上使用的连接。如果不可用,它将通过实施 ThreadLocal.initialValue()。
此外,您的线程应该使用 ExecutorService< 创建/a>) 所以你也可以重用线程。
Absolutely not. What you have is more of an object recycler, which is fine if that's what you need.
(As a recycler, however, you still don't want static fields, but you'd just create one instance of the recycler.)
For a connection pool (and if this for something like JDBC Connections) it needs to be thread-safe, and ideally you shouldn't need to return the connection.
Connection pools that are thread-safe will use ThreadLocal to return a connection that will only ever be used on that thread. If one is not available, it will then create a new connection by implementing ThreadLocal.initialValue().
Furthermore, your threads should be created using an ExecutorService) so you reuse threads as well.