I am using below code snippet to create a singleton instance of Connection object for a web application which will be used by multiple users.
static {
try {
String driver = PropertyReader.getPropertyReader("driverClassName");
Class.forName(driver).newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Connection conn = null;
private static synchronized Connection getDBConnection()
{
try{
if(conn == null || conn.isClosed()){
conn = null;
String URL = PropertyReader.getPropertyReader("url");
String userName = PropertyReader.getPropertyReader("username");
String password = PropertyReader.getPropertyReader("password");
conn = DriverManager.getConnection(URL,userName,password);
logger.info("Preparing Connection...");
}
else{
logger.info("Returning already prepared connection..");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
This class will return same instance of connection until and unless connection is closed or null. I suppose same connection will be shared by all users on different machine as it is static one.
If one user is setting auto commit to off to commit couple of statements as transaction, will this create problems for other users by restricting their connection to disable autocommit as well or by commiting their transaction in mid way if one user has used con.commit()?