Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Maximum number of Connections

832801Feb 29 2012 — edited Mar 1 2012
Hi,
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;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 29 2012
Added on Feb 29 2012
9 comments
2,097 views