java - Spot the concurrency issue -


One of our heritage has a database connection leak in one application, and I'm tracking this little gem. With debugging, I can see the same logical connection being returned for more than one thread (no good!). But I am struggling to understand why this is happening

We are using the setup ojdbc6 driver on the weblogic data source with connection pooling.

The code that creates a problem

  Public category MyDummyDaoUtil {// Note: This is a public area in a singleton (though a constant The area is not ...) conn public connection; Private MaidamidouUutil () {} Public Static MyMediaOutilIstance () (if (instance == blank) {instance = New MyDummyDaoUtil ();} Return Example;} Private Datacource throws the GetDataSource (Last String DSNName) Naming Explanation {return ServiceLocator.getInstance GetDataSource (dsName);} Get the public static connection throws naming upstation (last string source) {return MyDummyDaoUtil.getInstance (). GetDBConnection (source);} Private connection getDBConnection (last String source) NamingException throws / is the same logical connection generated by the data source or something else? Conn = getDataSource (Source) .getConnection (); conn.setAutoCommit (wrong); Return Canon;}}   

Updated fixes

  public class MyDummyDaoUtil {Private MyDummyDaoUtil () {} Public Fixed MyDummyDaoUtil getInstance () {if (example == Faucet) {Example = New MyDummyDaoUtil ();} Return Example;} Private Datacours GetDataSource (Last String DSNName) Nomination Expression Enkta is {return ServiceLocator.getInstance (). GetDataSource (dsName); } Public static connection throws naming upstation (Connection MyDummyDaoUtil.getInstance) to get connection (final string source). GetDBConnection (source); } Private connection throws the getDBConnection (final string source) NamingException {connection conn = getDataSource (source) .getConnection (); Conn.setAutoCommit (wrong); Return canon; The wrong lazy initialization of the example  
  • should be a connection.
  • Local variables in method, singleton class

    You singleton

    / P> getInstance () synchronize method

      public Static synchronized MyDummyDaoUtil getInstance () {if (example == zero) {example = New MyDummyDaoUtil (); } Return Example; }        

    Public stable MyDummyDaoUtil getInstance () {return example;

  • Use the double check locking system

      public static MyDummyDaoUtil getInstance () { If (example == faucet) {synchronize (MyDummyDaoUtil.class) {if (example == blank) {example = new MyDummyDaoUtil (); }}} Return example;    

    Note: Do not forget to close the connection.



  • Comments

    Popular posts from this blog

    php - PDO bindParam() fatal error -

    logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

    java - Why my included JSP file won't get processed correctly? -