Last one out turn off the light
I am trying to implement a strategy where the last session bean to be accessing an entity bean removes that entity bean before going out of scope.
Essentially a session bean looks for a particular entity bean and if it isn't there creates it. As each session bean is about to be destroyed I want it to either try to remove the entity bean or see if any other session beans are accessiong that entity bean. I want the last session bean to remove the entity bean as it is being destroyed.
[507 byte] By [
dubwai] at [2007-9-26 4:04:57]

I'm not a session bean expert, but I have some ideas that might get you started.
The only requirement is that the session beans somehow know when they are created and destroyed. Is this possible? If yes, then my idea will work.
My basic idea is to implement a reference counter for the entity bean.
You will either need a static field that is used as a reference counter in the entity bean (don't know if that is allowed under the spec) or some kind of singleton that has the field.
When the entity bean is first created (by the first session bean), set the reference counter to 1. Any time another session bean is created, increment the counter.
Whenever a session bean is destroyed, decrement the counter. When the counter reaches zero, you should be able to remove the entity bean safely.
Of course, none of this will work unless the session beans receive come kind of notification when they are created / destroyed.
Bill Davis.
As far as there being a "named" pattern for this, I don't know. I'm sure it probably should be a pattern, since it probably happens quite often.It may not be a J2EE pattern though, it seems like this kind of pattern would be useful in situations other than just J2EE.