9 Replies Latest reply on Nov 18, 2008 10:42 AM by w w

    Keep the connection alive after a management bean method exi

    w w Newbie

      In one of my management bean methods I:
      - Create a connection using the injected data source
      - Pass that connection to a legacy code that keeps the connection for further usage
      - Get out of the method without closing the connection

      I wanted the connection to stay alive, however I am getting the following:

      INFO [org.jboss.resource.connectionmanager.CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@434ff9
      java.lang.Throwable: STACKTRACE
      at org.jboss.resource.connectionmanager.CachedConnectionManager.registerConnection(CachedConnectionManager.java:290)
      at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:417)
      at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:842)
      at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:88)
      ... (user code)

      Questions:
      - I know that I should be closing the connections, but since it's just a legacy code wrapper, is there a way not to do that? I cannot set a connection on the legacy code again, it is supported only during the object creation, which is time-wise very costly
      - If the above approach is not a good one, is there an alternative approach that would satisfy the given criteria. I'm on JBoss 4.2.1.GA.

      If you need more info, please let me know.

        • 1. Re: Keep the connection alive after a management bean method
          Peter Johnson Master

          JBossAS maintains a connection pool. Opening a connection really means being given an available, existing, connection from the pool. Closing a connection really means freeing up the connection and placing it back into the pool. Thus, re-opening a connection is not really a big deal.

          Note that the physical connections a established lazily - the first request for a connection will establish the minimum number of connections which are placed into the pool.

          • 2. Re: Keep the connection alive after a management bean method
            w w Newbie

            Peter, thanks for the comment.

            The problem is not that I close the connection, but JBoss does that even though I don't want it to be closed. At the end, I cannot have a connection live after the method exits. I need that to avoid costly initialization of the legacy code and there is really no other way I see except keeping the connection alive.

            Is there a way to tell JBoss (it's CachedConnectionManager, as the below stacktrace says) not to close my connections and that I will take care of that somehow?

            • 3. Re: Keep the connection alive after a management bean method
              Peter Johnson Master

              Are you saying that if the connection is released back to the pool that the next time you ask for it you have to go through the initialization again?

              You mentioned management bean. Is that a JMX mbean?

              As far as I know, if you use a data source, once the user's request completes (and the response is sent), JBossAS will forcibly return the connection to the pool and give you that warning message.

              However, if you work directly with the JDBC driver then you can keep the connection open, and it is your responsibility to manage that connection. Note that this mechanism does not work if you are relying on annotations or similar features whereby JBossAS manages transactions for you. For example, this will not work for EJBs.

              • 4. Re: Keep the connection alive after a management bean method
                w w Newbie

                Peter, here is the pseudo-snippet that should answer your questions about how the things are right now:

                @Service(objectName = "MyServer:service=MyManagement")
                @Management(MyManagement.class)
                @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
                public class MyManagementBean implements MyManagement {
                @Resource(mappedName = "java:/MY_DATA_SOURCE")
                private DataSource dataSource;

                private LegacyClass legacyObject;

                public void doSomething() {
                Connection dbConnection = dataSource.getConnection();
                if(this.legacyObject == null || dbConnection.isClosed()) {
                this.legacyObject = new LegacyClass(dbConnection, ...);
                }
                legacyObject.legacyDoSomething();

                // ...

                // I don't close the connection
                }

                // ...
                }

                I get the message in question after doSomething exits. If I understood you correctly, this is not a good way, since I am using a DataSource, correct? I.e. the only option is not using DataSources, but JDBC directly. JBoss will always forcibly close the connection. Is there some parameter that I can tweak this with?

                If this is not possible, is there a simple way to get the parameters of that DataSource, so I can use them to make the connection myself? I would like to avoid storing the username, password, URL, etc. in my code directly.

                Thanks again for your help!

                • 5. Re: Keep the connection alive after a management bean method
                  Peter Johnson Master

                   

                  Is there some parameter that I can tweak this with?


                  None that I know of.

                  [copy]is there a simple way to get the parameters of that DataSource,[/copy]

                  You can always place a properties file containing the database info into the server/xxx/conf directory, and access it using the jboss.server.config.url system property. Something like this:

                  String confdir = System.getProperties("jboss.server.config.url");
                  FileInputStream fis = new FileInputStream(confdir + "somedb.properties");
                  Properties dbprop = new Properties();
                  dbprop.load(fis);


                  • 6. Re: Keep the connection alive after a management bean method
                    Vicky Kak Master

                     

                    "wimxa" wrote:
                    I.e. the only option is not using DataSources, but JDBC directly. JBoss will always forcibly close the connection. Is there some parameter that I can tweak this with?


                    The CachedConnectionInterceptor(CCI) in the Interceptor that checks for the leaked connections in ejb method calls.You can disable the CachedConnectionInterceptor but I would not recommend you that, I would rather advice you to refactor the LegacyObject, you should not pass the connection object to the LegacyObject.
                    The DataSource object should be passed to the LegacyObject instead of the connection object.
                    Check for the CCI at $JBOSS_HOME/server//conf/standardjboss.xml, just uncomment it in the appropriate container.
                    http://www.jboss.org/community/docs/DOC-12682

                    • 7. Re: Keep the connection alive after a management bean method
                      w w Newbie

                      Peter, OK, I might do something like that. Thanks!

                      vickyk, that would mean that all DataSources would behave like this. The last chapter in the link you gave (Turning off Connection Close Checking) is a setup for the whole CachedConnectionManager MBean. Is there a way to do that on a per-DataSource basis?

                      • 8. Re: Keep the connection alive after a management bean method
                        Vicky Kak Master

                         

                        "wimxa" wrote:

                        vickyk, that would mean that all DataSources would behave like this. The last chapter in the link you gave (Turning off Connection Close Checking) is a setup for the whole CachedConnectionManager MBean. Is there a way to do that on a per-DataSource basis?


                        No, you can configure the EJB container without out the CachedConnectionInterceptor and tie it to specific EJB where you don't want the connections to be automatically closed... This is a *HACK* , refractor the LegacyObject as explained before.
                        I am not telling you to play with CachedConnectionManager.



                        • 9. Re: Keep the connection alive after a management bean method
                          w w Newbie

                           

                          No, you can configure the EJB container without out the CachedConnectionInterceptor and tie it to specific EJB where you don't want the connections to be automatically closed... This is a *HACK* , refractor the LegacyObject as explained before.
                          I am not telling you to play with CachedConnectionManager.

                          It's not named LegacyObject for nothing - if I could refactor it, I would. OK, bad water here, I'll get out of here and try something else. Thanks for the explanation!