java - does singleton instance of connection object creates issue in web application -
java - does singleton instance of connection object creates issue in web application -
i using below code snippet create singleton instance of connection object web application used multiple users.
static { seek { string driver = propertyreader.getpropertyreader("driverclassname"); class.forname(driver).newinstance(); } grab (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 prepared connection.."); } } catch(exception e) { e.printstacktrace(); } homecoming conn; }
this class homecoming same instance of connection until , unless connection closed or null. suppose same connection shared users on different machine static one.
if 1 user setting auto commit off commit couple of statements transaction, create problems other users restricting connection disable autocommit or commiting transaction in mid way if 1 user has used con.commit()?
yes, cause problems. sharing same instance, statement wrong
if 1 user setting auto commit off commit couple of statements transaction, create problems other users restricting connection disable autocommit or commiting transaction in mid way if 1 user has used con.commit()?
it should read
if 1 user setting auto commit off commit couple of statements transaction, create problems other users because connection sharing has been set not autocommit , of statements become part of new transaction**
since users (threads) using same instance, changes made 1 user impact others.
as shivam kalra says, connection pools improve tool. web server provide them, , if not there 3rd party libraries.
java jdbc connection singleton
Comments
Post a Comment