Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection Pool Java
    primarykey
    data
    text
    <p>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</p> <pre><code>public class MyCp1 { private static final int MAX_SIZE=100; private static final BlockingQueue&lt;Connection&gt; bq; static{ System.out.println("Inside begin static block" ); bq= new ArrayBlockingQueue&lt;Connection&gt;(MAX_SIZE); for(int i=0;i&lt;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; } } </code></pre> <p>I know there are issues with exceptional handling and others, but i would appreciate if you can please stick to the above mentioned question</p> <p>EDIT::</p> <p>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 </p> <pre><code> public class ConnectionPool { private static final int MAX_SIZE = 100; private BlockingQueue&lt;Connection&gt; bq; private static ConnectionPool cp= new ConnectionPool(); private ConnectionPool(){ System.out.println("inside constructor"); bq = new ArrayBlockingQueue&lt;Connection&gt;(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 &lt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload