1 Reply Latest reply on Oct 11, 2003 4:13 AM by amol

    How to keep a Connection to a database with a Stateful Sessi

    berni

      With a stateful session bean, I open a connction to a database, but after making a query (SELECT ...) with a first method, I obtain this message "Closing a statement you left open, please do your own housekeeping".
      After that, I can't access to the resultset with another method from the same session bean because the connection has been closed.

      Between the two calls (executeQuery() in the first one and resultset.next() in the second one),
      I just check a boolean parameter for using differently the resultset.

      Why has JBoss closed the connection between the two method calls ?
      Is there some parameters to change in a *.xml file that can help to keep the connection ?
      How JBoss manage objects that are not serializable like java.sql.Connection ?


      Please, help

      PS : I work with jboss-3.2.1_tomcat-4.1.24.
      I think my java code is ok, because if I use the resultset.next() directly with the first method, there is no problem.
      And the session bean seems correctly deployed :
      In my ejb-jar.xml I have this two lines :
      <session-type>Stateful</session-type>
      <transaction-type>Container</transaction-type>
      (and the dtd is : <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>)

      In the log, there are this lines :
      09:25:56,796 INFO [EjbModule] Starting
      09:25:56,796 INFO [StatefulSessionContainer] Starting
      09:25:56,812 INFO [StatefulSessionInstancePool] Starting
      09:25:56,812 INFO [StatefulSessionInstancePool] Started
      09:25:56,812 INFO [StatefulSessionFilePersistenceManager] Starting
      09:25:56,828 INFO [StatefulSessionFilePersistenceManager] Started
      09:25:56,828 INFO [StatefulSessionContainer] Started
      09:25:56,828 INFO [EjbModule] Started
      09:25:56,828 INFO [EJBDeployer] Deployed: file:/C:/JBoss/jboss-3.2.1_tomcat-4.1
      .24/server/default/tmp/deploy/server/default/deploy/services_jboss.ear/40.
      services_jboss.ear-contents/tools_SourceData.jar

      In the jmx-console, the session bean appears in a "StatefulSessionContainer".

        • 1. Re: How to keep a Connection to a database with a Stateful S

          Hi ! ,


          First of all , let me explain the motivation behind statefullsession bean.

          statefullsession beans are used to maintain the conversational state of the client. This means they are attached to a particular client and saves information about him, so that client need not resend the information about him , during each business method invokation.

          But for the performance optimization , your bean container (vendor dependent) may use the technique called Instance pooling. In instance pooling , the container creates a set of bean instances and reuse them to service the client request. But the instance pooling is transparent to the client.

          Now the question is how the container does this.

          For the container to achieve this it uses callback methods namely ejbActivate() and ejbPassivate() method.

          Container calls ejbPassivate() method just before serializing your bean object to some persistent store so that it can be activated again when the client corressponding to this bean object sends request.
          By calling ejbPassivate() container giives you a chance to close the resources that can not be serialized.

          Now , when the client corressponding to above bean
          calls any method , the container first of all deserializes the bean (hence it's state is restored), then it calls bean's ejbActivate() method.

          again, by calling ejbActivate() container gives you a chane to open the resources that you need in your business methods.

          Hope this will help you in redesigning your business logic.