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.
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
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