Maximum number of Connections
832801Feb 29 2012 — edited Mar 1 2012Hi,
I am using a static initialization block to register the driver and a static synchronized method to get a connection. The problem is I need to run 15 threads but always only two threads get the connection. I want to know if there is a default maximum number of concurrent connections a DriverManager can provide or is it my threading logic that may be faulty.
CODE:
static{
try
{
Class jdbcDriverClass = Class.forName( JDBC_DRIVER );
driver = (Driver) jdbcDriverClass.newInstance();
DriverManager.registerDriver( driver );
}
catch (Exception e)
{
System.out.println( "Failed to initialise JDBC driver" );
e.printStackTrace();
}
}
public static synchronized Connection getConnection(boolean autoCommit)
throws SQLException
{
Connection conn =null;
java.util.Properties props = new java.util.Properties();
props.put("user",JDBC_USER);
props.put("password",JDBC_PASSWORD);
props.put("defaultRowPrefetch",FETCH_DATA);
try{
conn = DriverManager.getConnection( JDBC_URL, props );
conn.setAutoCommit(autoCommit);
}catch(SQLException ex){
ex.printStackTrace();
if (ex.getMessage().startsWith("Closed Connection")){
conn = DriverManager.getConnection( JDBC_URL, props );
conn.setAutoCommit(autoCommit);
}
}
System.out.println(Thread.currentThread().getName()+" GOT CONNECTION"); //Always only two threads print this line
return conn;
}