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]
# 1
The question is, is this possible and is there a design pattern for this?
dubwai at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

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.

bdavisx at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
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.
bdavisx at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
Good idea, I will look into the whether session beans know when they are being destroyed. At least I have somewhere to start from. Thanks.
dubwai at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
> 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.The SessionBean interface has the functiouns ejbCreate() and ejbRemove().
dubwai at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
> The SessionBean interface has the functiouns> ejbCreate() and ejbRemove().Sorry it just has the ejbRemove(). no ejbCreate. I assume you can put code in your constructor.
dubwai at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
> Sorry it just has the ejbRemove(). no ejbCreate. I> assume you can put code in your constructor.Nix that. it does. I guess everyone can tell I'm just starting out with this stuff.
dubwai at 2007-6-29 13:03:57 > top of Java-index,Other Topics,Patterns & OO Design...