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 开发者_JAVA技巧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;
}
}
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.
精彩评论