3 Replies Latest reply on Aug 23, 2001 2:02 PM by David Jencks

    Stateful or Stateless session bean?

    ocquidant Newbie

      Hello!

      I need to access the database very often via one session bean. Do you think, it is better to have one stateful session bean which keeps the connection to the database for the client or to have one stateless session bean which gets the connection each time.
      Moreover, if i use one stateful session bean, to keep the connections clean, i need to close the connection only in the methods ejbPassivate() and ejbRemoved(). No ?

      Thanks a lot for your advice.

      Nicolas

        • 1. Re: Stateful or Stateless session bean?
          Emmanuel Valentin Newbie

          Hi,

          Statefull beans are used to keep conversationnal state. Use tehm when the state changes of your bean affect subsequent business method invocations, but not when you want to cache system objects. The server keeps caches and pools for that.

          If you keep Connection object open you will run out of resource and you may have difficulties with transactions.

          You can save a reference on the DataSource object in a ejb, this will avoid JNDI lookups. This reference should to be transient, or set to null before passivation. You retrieve it again during activation.

          If there is no conversion between the client and the bean, use a Stateless bean, and cache a DataSource reference.

          Manu

          • 2. Re: Stateful or Stateless session bean?
            ocquidant Newbie

            Ok, that's what i've done (with stateless session bean).
            In the method setSessionContext(), i get the DataSource and the Connection.
            In the method ejbRemove(), i close the connection to the database. Do i need to set the DataSource variable to null?

            thanks a lot

            Nicolas

            • 3. Re: Stateful or Stateless session bean?
              David Jencks Master

              I dont think this is what was advised, and wont work.

              cache the Datasource, and set it to null on passivate,
              but _dont cache the connection_. Get the connection within each transaction or it will not be enrolled in the transaction. Close it before the end of the transaction or it will not be returned to the pool and may mess up transaction handling