R: [v.1.1.3 September 28 2005] Global exception.

From: Claudio Rosati (claudio.rosat..csys.it)
Date: Wed Nov 30 2005 - 03:48:37 EST

  • Next message: Andrus Adamchik: "Re: [v.1.1.3 September 28 2005] Global exception."

    Hello Andrus,

    I've following your suggestions but every thing was OK. So I've started
    debugging my application with Cayenne sources to better understand the
    executed operation, in order to identify the problem.

    After a day I've discovered the previously submitted code was not the
    problem's cause, but just an effect of another code executed just before it:

    public static long getNextRecordCode ( )
    {
                    
      long code = -1;
      PreparedStatement ps = null;
      ResultSet rs = null;

      try {

        DataContext dc = CayenneUtility.createDataContext();
        Connection con = dc.lookupDataNode(
     
    dc.getEntityResolver().lookupDbEntity(Record.class).getDataMap()
                    ).getDataSource().getConnection();

        ps = con.prepareStatement("SELECT nextval('code_provider')");
        rs = ps.executeQuery();

        if ( rs.next() )
          code = rs.getLong(1);

      }
      catch ( Exception ex ) {
        ...
      }
      finally {
        try {
          if ( rs != null )
            rs.close();
        }
        catch ( Exception ex ) { ... }
        try {
          if ( ps != null )
            ps.close();
        }
        catch ( Exception ex ) { ... }
      }

      return code;

    }
            

    As you can see, I've not closed the JDBC connection thinking that this could
    cause problems to Cayenne. Instead, after debugging through Cayenne code,
    I've discovered that the returned connection is a PooledConnectionImpl whose
    close() doesn't really close the JDBC connection, but release the connection
    to the pool instead.

    So, closing the connection solved the problem.

    Now I would like to suggest the following:

    1. add notice in the PoolManager.getConnection methods
       documentation about the need to call close() in order
       to release to connection to the pool;

    2. same for PooledConnectionImpl documentation;

    3. add the following method in DataContext (or at least
       a corresponding documentation) in order to easily
       obtain a JDBC connection from Cayenne

       public Connection getConnection ( Class target )
         throws ...
       {
         return lookupDataNode(
           getEntityResolver().lookupDbEntity(target).getDataMap()
         ).getDataSource().getConnection();
       )

       for the rare situations where handling directly
       with JDBC is more convenient (or, maybe, the only
       solution) than using Cayenne (e.g. "SELECT count(*) FROM ...").

    Claudio

    ------------------------------------
    Advanced Computer Systems S.p.A.
    Claudio Rosati
    Project Manager
    claudio.rosat..csys.it
    via Della Bufalotta 378
    00139 Roma, RM
    Italy
    tel: +39 06 8709 0516
    fax: +39 06 8720 1502
    ------------------------------------
     

    > -----Messaggio originale-----
    > Da: Andrus Adamchik [mailto:andru..bjectstyle.org]
    > Inviato: luned́ 28 novembre 2005 21.50
    > A: cayenne-use..bjectstyle.org
    > Oggetto: Re: [v.1.1.3 September 28 2005] Global exception.
    >
    > I am fairly certain that 1.1.3 Cayenne connection pool
    > doesn't leak by itself, as this would've been a common
    > problem for everybody. So try to determine whether the leak
    > can occur somewhere in your application. A few things to check:
    >
    > (1) do you have queries that run longer than 0-10 seconds?
    > (you can check the logs - Cayenne prints JDBC timing)
    >
    > (2) do you use Cayenne ResultIterator, if so do you close it properly?
    >
    > (3) Also it might help to take a thread dump of the
    > application when the error starts to happen to see if there
    > are deadlocks:
    >
    > http://java.sun.com/developer/technicalArticles/Programming/St
    > acktrace/
    >
    > (do "kill -QUIT <pid>" on UNIX or "Ctrl+Break" on Windows)
    >
    > Hope this helps.
    >
    > Andrus
    >
    >
    > On Nov 28, 2005, at 7:47 PM, Claudio Rosati wrote:
    > > Hello all,
    > >
    > > I'm using Cayenne 1.1.3 with PostgreSQL 8.1 on Windows XP & Linux.
    > > The following code, called in a context where the DataContext
    > > __datactx is already set up and possibly some commit were already
    > > called, seems consume a JDBC connection when __datactx.performQuery
    > > (query); is called without releasing it.
    > >
    > > private Collocation getCollocation ( String pressmark ) {
    > > Collocation collocation = null;
    > > Expression exp = ExpressionFactory.matchExp("pressmark",
    > pressmark);
    > > SelectQuery query = new SelectQuery(Collocation.class, exp);
    > > List queriedList = __datactx.performQuery(query);
    > > if ( queriedList.size() > 0 )
    > > collocation = (Collocation) queriedList.get(0);
    > > return collocation;
    > > }
    > > Clearly after a while (a very short while) the following
    > errors occurs
    > >
    > > CADMUS Ver 1.3.0
    > > EXCEPTION: Unexpected exception
    > > org.objectstyle.cayenne.CayenneRuntimeException.
    > > Location is:
    > >
    > org.objectstyle.cayenne.access.util.SelectObserver.nextGlobalException
    > > , line 222.
    > > Message is: [v.1.1.3 September 28 2005] Global exception.
    > > Stack trace: org.objectstyle.cayenne.CayenneRuntimeException: [v.
    > > 1.1.3 September 28 2005] Global exception.
    > > at
    > >
    > org.objectstyle.cayenne.access.util.SelectObserver.nextGlobalException
    > > (SelectObserver.java:222)
    > > at org.objectstyle.cayenne.access.DataNode.performQueries
    > > (DataNode.java:325)
    > > at org.objectstyle.cayenne.access.DataDomain.performQueries
    > > (DataDomain.java:654)
    > > at org.objectstyle.cayenne.access.DataContext.performQueries
    > > (DataContext.java:1408)
    > > at org.objectstyle.cayenne.access.Transaction.performQueries
    > > (Transaction.java:179)
    > > at org.objectstyle.cayenne.access.DataContext.performQueries
    > > (DataContext.java:1366)
    > > at org.objectstyle.cayenne.access.DataContext.performQuery
    > > (DataContext.java:1622)
    > > at org.objectstyle.cayenne.access.DataContext.performQuery
    > > (DataContext.java:1485)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.CollocationImporter.get
    > > Collocation(CollocationImporter.java:222)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.CollocationImporter.imp
    > > ortCollocations(CollocationImporter.java:101)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.RecordImporter.getCollo
    > > cations(RecordImporter.java:798)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.RecordImporter.importRe
    > > cord(RecordImporter.java:412)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.RecordImporter.getRecor
    > > d(RecordImporter.java:336)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.RecordImporter.importRe
    > > cords(RecordImporter.java:168)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.SBNImporter.importRecor
    > > ds(SBNImporter.java:305)
    > > at
    > >
    > it.acsys.multimedia.products.cadmus.plugin.sbn.wizard.Progress.importR
    > > ecords(Progress.java:208)
    > > at
    > > it.acsys.multimedia.products.cadmus.plugin.sbn.wizard.Progress.run
    > > (Progress.java:228)
    > > at it.acsys.swing.wizard.DefaultProgressPage$2.run
    > > (DefaultProgressPage.java:1181)
    > > at java.lang.Thread.run(Thread.java:595)
    > > Caused by: java.sql.SQLException: Can't obtain connection. Request
    > > timed out. Total used connections: 20
    > > at
    > > org.objectstyle.cayenne.conn.PoolManager.uncheckPooledConnection
    > > (PoolManager.java:466)
    > > at org.objectstyle.cayenne.conn.PoolManager.getConnection
    > > (PoolManager.java:392)
    > > at org.objectstyle.cayenne.conn.PoolManager.getConnection
    > > (PoolManager.java:385)
    > > at org.objectstyle.cayenne.access.DataNode.performQueries
    > > (DataNode.java:313)
    > > ... 17 more
    > >
    > > I've tried to give more connection but the problems always
    > occurs (in
    > > a longer time).
    > >
    > > I don't understand is I've missed something in the code or
    > somewhere
    > > in the initialization.
    > >
    > > Anyone has some suggestions?
    > >
    > > Thanks
    > >
    > > Claudio
    > >
    > >
    > >
    > >
    > >
    > >
    > > <642113416>
    > > Multimedia Division
    > > Claudio Rosati
    > > Project ManagerAdvanced Computer Systems S.p.A.
    > > via Della Bufalotta 378
    > > 00139 Roma, RM
    > > Italy
    > > claudio.rosat..csys.it
    > > tel:
    > > fax:+39 06 8709 0516
    > > +39 06 8720 1502
    > > Add me to your address book...Want a signature like this?
    > >
    > >
    >



    This archive was generated by hypermail 2.0.0 : Wed Nov 30 2005 - 03:48:50 EST