1 Reply Latest reply on Feb 10, 2006 9:06 PM by mikevmoran

    IllegalStateException - CachedConnectionInterceptor

    mikevmoran

      I have found several posts documenting the IllegalStateException from the CachedConnectionInterceptor when developers do not adhere to J2EE semantics. Most of the posts were centered around Hibernate 2 using a Thread Local Pattern.

      I have a case where a bean managed SessionBean (no transaction used) uses another bean managed SessionBean that gets a Hibernate session, performs a query, closes the connection and I get errors below. This is perfectly legal and adheres to J2EE semantics

      What?s interesting is I see this problem using JBoss Portal 2.2, AS 4.0.3sp1, Hibernate 3.0.2, but not while using JBoss Portal 2.0, AS 4.0.1, Hibernate 3.0.2. And to add to the mystery the JBoss 2.2 Bundle (with AS 4.0.3sp1) did not come with Hibernate. However JBoss 4.0.3sp1 come bundled with Hibernate 3.1.rc2.

      Error Messages:
      [CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@5a7dd3
      java.lang.Throwable: STACKTRACE
      at org.jboss.resource.connectionmanager.CachedConnectionManager.registerConnection(CachedConnectionManager.java:333)

      10:57:54,937 INFO [TxConnectionManager] throwable from unregister connection
      java.lang.IllegalStateException: Trying to return an unknown connection2! org.jboss.resource.adapter.jdbc.WrappedConnection@5a7dd3
      at org.jboss.resource.connectionmanager.CachedConnectionManager.unregisterConnection(CachedConnectionManager.java:385)

      First Session bean:
      protected String createProfileInternal(Entity e, Profile p) throws ProfileException {
      try {
      DataServicesLocal dataServices = dataServicesLocalHome.create();

      VersionedModelBase vmb = profileToPojo(e, p);

      String query = SQLGenerator.getEntityByProfile(e, p);
      ModelBase model = dataServices.uniqueQuery(query);
      if (model != null) {
      throw new DuplicateProfileException();
      }

      dataServices.save( vmb );
      return vmb.getGuid();
      } catch (ProfileException pe) {
      throw pe;
      } catch (Exception other) {
      log.error("createProfileInternal: error occurred while creating new profile", other);
      throw new ProfileException(other.getMessage());
      }
      }


      Second Session bean:
      public ModelBase uniqueQuery(String queryString) throws EJBException {
      // find a unique result based on the input query
      Session session = getSession();
      ModelBase result = null;
      try {
      Query query = session.createQuery(queryString);
      result = (ModelBase)query.uniqueResult();
      } catch (HibernateException e) {
      String message = "DataServices: uniqueQuery failed: " + queryString;
      log.error(message, e);
      }
      finally {
      session.close();
      }
      return result;
      }


      protected Session getSession() {
      Session session = null;
      try {
      SessionFactory sf = (SessionFactory) new InitialContext()
      .lookup(SESSION_FACTORY);

      session = session = sf.openSession();
      session.connection().setAutoCommit(false);

      return session;
      } catch (NamingException e) {
      log.error(e);
      throw new RuntimeException("DataServicesBean: Cannot get Hibernate session.");
      } catch (SQLException e) {
      String message = "DataServices: SQLException: " + e.getMessage();
      log.error(message, e);
      }
      return session;
      }

      Thanks in advance for any help with this problem.

      P.S. I'm planning to upgrade to Hibernate 3.1 to see if this problem ges away.

        • 1. Re: IllegalStateException - CachedConnectionInterceptor
          mikevmoran

          Things I've tried:

          Upgraded Hibernate to the same version shipped with AS 4.0.3sp1 (3.1.RC2).
          Did not help.

          Turned of the CachedConnectionManager debug
          Caused the application to run out of connection (set to 100 in -sd.xml, more than enough for my Cactus test)
          17:43:02,890 ERROR [JDBCExceptionReporter] No ManagedConnections available within configured blocking timeout ( 30000 [ms] ); - nested throwable: (javax.resource.ResourceException: No ManagedConnections available within configured blocking timeout ( 30000 [ms] ))
          17:43:02,937 ERROR [DataServicesSession] DataServices: update failed with HibernateException: Cannot open connection
          org.hibernate.exception.GenericJDBCException: Cannot open connection

          Attempted to check the CachedConnectionManager MBean InUseConnections attribute. The number never grows more than 1 or 2 but the test still fails by running out of connections.

          Verified the MySQL default connections are set to 100.