10 Replies Latest reply on Dec 26, 2002 6:47 PM by Emily

    Connection handle is not currently associated with a Managed

    Jorg Janke Newbie

      Sometimes, I get the error:

      java.sql.SQLException: Connection handle is not currently associated with a ManagedConnection
      at org.jboss.resource.adapter.jdbc.local.LocalConnection.checkStatus(LocalConnection.java:774)
      at org.jboss.resource.adapter.jdbc.local.LocalConnection.prepareStatement(LocalConnection.java:210)
      at org.compiere.util.DB.prepareStatement(DB.java:1071)

      So far I was not able to replicate it reliably.

      What could be the cause. There are certainly enough connections available?

        • 1. Re: Connection handle is not currently associated with a Man
          Yan Jing Newbie

          I ran into the same problem. My app is running under jboss3.0.3/tomcat4.1.12/Castor0.9.4/jdk1.4. (I changed castor0.9.4 to make it work for jboss3.0.3). We use Stateless session bean to access database through castor jdo.

          I will get "connection handle is not currentlly ...." problem under multi-threading environment and related with Stateless session bean pool.

          When two concurrent threads/transactions try to access methods in the same stateless session bean (container managed transaction), after the first thread finishes invoking one method from that bean instance, that bean instance will be returned back to the bean pool, then the second thread could get the same bean instance. If the second thread finishs the transaction before the first thread, you will get "connection handle is not currentlly ..." error for sure.

          After reading jboss3.0.3 source code, I don't think the problem can be fixed by configuration. So, I changed the org.jboss.ejb.plugins.AbstractInstancePool.get() method's
          pool.removeFirst() ---> pool.removeLast(), which uses the pool as queue instead of stack. And I also configure the StatelessSessionInstancePool to use:

          <container-pool-conf>
          100
          <feeder-policy>
          org.jboss.ejb.plugins.TimedInstancePoolFeeder
          </feeder-policy>
          <feeder-policy-conf>
          20
          1800000
          </feeder-policy-conf>
          </container-pool-conf>


          After those changes, I won't get that error anymore in my app. But those changes won't fix the problem permanently. The real problem is jboss's CachedConnectionManager's IdentityWrapper using the bean instance as key in objectToConnectionManagerMap. I couldn't figure out a better way to fix it right now.


          Hope this will help.

          Yan Jing


          • 2. Re: Connection handle is not currently associated with a Man
            Matthias Lakämper Newbie

            Hi,

            i´m working with jboss-3.0.4 and i have the same Problem.

            I have a message-Driven Bean which calls a Stateless-Session-Bean.
            The stateless-Session-Bean holds a DB-Connection and Prepared-Statements.
            Sometimes (arbitrary) the executeQuery-Statements results in the
            Exception:

            "Connection handle is not currently associated with a ManagedConnection ..."

            in the moment i catch the exception, close the connection and make
            a complete reconnect (new Connection and prepareStatement).

            I think it is the right way to work with Db-Connections in Stateless-Session-Beans.
            Or is this a wrong way ? Is there a known bug ?

            i would be glad to get help.

            Regards Matthias





            • 3. Re: Connection handle is not currently associated with a Man
              Yan Jing Newbie

              I think I found a fix for this problem. After the following change made to CachedConnectionManager.IdentityWrapper class, the problem is gone for me. What I did was just adding the thread information to IdentityWrapper which is used as key in objectToConnectionManagerMap.

              private final static class IdentityWrapper
              {

              private final Object o;
              private Thread currentThread;

              IdentityWrapper(final Object o)
              {
              this.o = o;
              this.currentThread = Thread.currentThread();
              }

              public boolean equals(Object other)
              {
              return (other instanceof IdentityWrapper) && o == ((IdentityWrapper)other).o &&
              (currentThread == ((IdentityWrapper)other).currentThread);
              }

              public int hashCode()
              {
              return (o.toString() + currentThread.toString()).hashCode();
              }
              }

              • 4. Re: Connection handle is not currently associated with a Man
                Yan Jing Newbie

                I think I found a fix for this problem. After the following change made to CachedConnectionManager.IdentityWrapper class, the problem is gone for me. What I did was just adding the thread information to IdentityWrapper which is used as key in objectToConnectionManagerMap.

                private final static class IdentityWrapper
                {

                private final Object o;
                private Thread currentThread;

                IdentityWrapper(final Object o)
                {
                this.o = o;
                this.currentThread = Thread.currentThread();
                }

                public boolean equals(Object other)
                {
                return (other instanceof IdentityWrapper) && o == ((IdentityWrapper)other).o &&
                (currentThread == ((IdentityWrapper)other).currentThread);
                }

                public int hashCode()
                {
                return (o.toString() + currentThread.toString()).hashCode();
                }
                }

                • 5. Re: Connection handle is not currently associated with a Man
                  Yan Jing Newbie

                  I think I found a fix for this problem. After the following change made to CachedConnectionManager.IdentityWrapper class, the problem is gone for me. What I did was just adding the thread information to IdentityWrapper which is used as key in objectToConnectionManagerMap.

                  private final static class IdentityWrapper
                  {

                  private final Object o;
                  private Thread currentThread;

                  IdentityWrapper(final Object o)
                  {
                  this.o = o;
                  this.currentThread = Thread.currentThread();
                  }

                  public boolean equals(Object other)
                  {
                  return (other instanceof IdentityWrapper) && o == ((IdentityWrapper)other).o &&
                  (currentThread == ((IdentityWrapper)other).currentThread);
                  }

                  public int hashCode()
                  {
                  return (o.toString() + currentThread.toString()).hashCode();
                  }
                  }

                  • 6. Re: Connection handle is not currently associated with a Man
                    David Jencks Master

                    I'd sure appreciate some code that demonstrates this problem. The very best would be a testcase for the jca testsuite module.

                    Are you guys holding a connection over method boundaries? Does this problem occur if you close all connection handles in a finally block before returning from the method call?

                    • 7. Re: Connection handle is not currently associated with a Man
                      Emily Newbie

                      I am having the same problem with the JBoss4.0 alpha I built. I do hold a connection over multiple method calls. I have a message driven bean that instantiates an "update" object I've created which then calls multiple methods in multiple classes to do DB updates. If all those updates succeed, the transaction is committed, else rolled back, and in all cases the connection is closed. The code looks like this

                      boolean updateSucceeded = update.updateDB();
                      if(updateSucceeded){
                      update.commit();
                      }else{
                      update.rollback();
                      }


                      where those update methods look like this:

                      public void commit(){
                      logger.info("Committing this transaction");
                      try{
                      connection.commit(); //end transaction
                      connection.close();
                      }catch(SQLException e){
                      logger.error(e.toString());
                      }
                      }

                      public void rollback(){
                      logger.info("Rolling back this transaction");
                      try{
                      connection.rollback(); //end transaction
                      connection.close();
                      }catch(SQLException e){
                      logger.error(e.toString());
                      }
                      }


                      And here is the exception

                      17:02:44,975 ERROR [ProductSummary] Unable to get existing exercise summaries, update aborted for the following reason:
                      java.sql.SQLException: Connection handle is not currently associated with a ManagedConnection
                      17:02:44,995 ERROR [STDERR] java.sql.SQLException: Connection handle is not currently associated with a ManagedConnectio
                      n
                      17:02:45,005 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedConnection.checkStatus(WrappedConnection.java:
                      774)
                      17:02:45,015 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedConnection.checkTransaction(WrappedConnection.
                      java:755)
                      17:02:45,035 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedStatement.checkTransaction(WrappedStatement.ja
                      va:771)
                      17:02:45,055 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPrepared
                      Statement.java:286)
                      17:02:45,055 ERROR [STDERR] at com.scilearn.db.ProductSummary.getProductSummary(ProductSummary.java:202)
                      17:02:45,065 ERROR [STDERR] at com.scilearn.db.ProductSummary.getExerciseSummaries(ProductSummary.java:182)
                      17:02:45,075 ERROR [STDERR] at com.scilearn.db.ProductSummary.updateExerciseSummaries(ProductSummary.java:81)
                      17:02:45,075 ERROR [STDERR] at com.scilearn.db.ProductSummary.updateExerciseSummary(ProductSummary.java:50)
                      17:02:45,085 ERROR [STDERR] at com.scilearn.db.ArchiveToDBUpdate.updateExerciseInDB(ArchiveToDBUpdate.java:188)
                      17:02:45,095 ERROR [STDERR] at com.scilearn.db.ArchiveToDBUpdate.updateDB(ArchiveToDBUpdate.java:75)
                      17:02:45,095 ERROR [STDERR] at com.scilearn.beans.UploadQueueListener.onMessage(UploadQueueListener.java:121)
                      17:02:45,105 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                      17:02:45,115 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                      17:02:45,115 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

                      17:02:45,135 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:324)
                      17:02:45,135 ERROR [STDERR] at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContain
                      er.java:405)
                      17:02:45,145 ERROR [STDERR] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnect
                      ionInterceptor.java:187)
                      17:02:45,165 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:108
                      )
                      17:02:45,176 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractTxInterceptorBMT.invokeNext(AbstractTxInterceptorBMT.ja
                      va:144)
                      17:02:45,186 ERROR [STDERR] at org.jboss.ejb.plugins.MessageDrivenTxInterceptorBMT.invoke(MessageDrivenTxInterceptor
                      BMT.java:33)
                      17:02:45,206 ERROR [STDERR] at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceIn
                      terceptor.java:88)
                      17:02:45,216 ERROR [STDERR] at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:1
                      00)
                      17:02:45,226 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:205)
                      17:02:45,236 ERROR [STDERR] at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderIntercep
                      tor.java:154)
                      17:02:45,246 ERROR [STDERR] at org.jboss.ejb.MessageDrivenContainer.invoke(MessageDrivenContainer.java:312)
                      17:02:45,256 ERROR [STDERR] at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:697)
                      17:02:45,266 ERROR [STDERR] at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContai
                      nerInvoker.java:763)
                      17:02:45,276 ERROR [STDERR] at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:241)
                      17:02:45,286 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java
                      :643)
                      17:02:45,296 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:457)
                      17:02:45,306 ERROR [STDERR] at org.jboss.mq.SpySession.run(SpySession.java:309)
                      17:02:45,306 ERROR [STDERR] at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:177)
                      17:02:45,316 ERROR [STDERR] at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:655)
                      17:02:45,316 ERROR [STDERR] at java.lang.Thread.run(Thread.java:536)
                      17:02:45,326 INFO [ArchiveToDBUpdate] update aborted: java.sql.SQLException: Connection handle is not currently associa
                      ted with a ManagedConnection
                      17:02:45,336 INFO [ArchiveToDBUpdate] Rolling back this transaction


                      Is this a JBoss issue?

                      • 8. Re: Connection handle is not currently associated with a Man
                        Emily Newbie

                        I found that the error was mine. I was passing connections around incorrectly, doing some updates with the the wrong connection. I'm still not quite sure why it would result in the "Connection handle is not currently associated..." error, but at least it is working now.

                        • 10. Re: Connection handle is not currently associated with a Man
                          Emily Newbie

                          Sorry to keep posting so much, but I can narrow down the error a bit. It occurs when I am halfway through a transaction with one connection that I am using for updating...in the middle of said transaction I do a simply query using a different connection, and this is where I get the
                          Connection handle is not currently associated with a ManagedConnection
                          error. The connection I am trying to use to query is a private static Connection object declared as a class variable, which I never close because I am hoping to use a PreparedStatement across multiple calls. Is this not possible?

                          It works the first time, but not the second...