users@glassfish.java.net

Re: why does stateful ejb throw exception for calling super.finalize();

From: Steven Siebert <smsiebe_at_gmail.com>
Date: Fri, 25 Feb 2011 16:58:59 -0500

Hi Gary,

You're not permitted to use finalize for the same reasons you are not to use
constructors to create an EJB instance - the EJB container manages the
lifecycle of your EJB instances. This is necessary for many reasons -
allowing the EJB container to accurately manage the performance of your
application (ie to grow the instance pool when under stress) is often cited,
but it's also necessary for other EJB "magic" such as context and dependency
injection, container managed transactions, etc.

Since you are using a session bean, you can use the @PostConstruct
annotation to designate a callback method, which will be
called immediately after instantiation. Likewise, you can annotate a method
with @PreDestoy which will be called before the EJB is removed and
(eventually) GC'ed.

That said...the specification indicates that a Stateful Session Bean will be
created for each client...and the client will use this same EJB to maintain
state. In other words, if a new stateful session bean was made for each
call...you wouldn't maintain state between calls. I'm not sure if you'll
see anything special in your testing =)

I hope this helps,

S

On Fri, Feb 25, 2011 at 3:21 PM, emiddio-frontier <emiddio_at_frontier.com>wrote:

> I wanted to track the creation and deletion of EJB instances -- motivated
> by seeing so many instances created for a simple JSF2 web page -- when i
> thought only 1 would have been needed;
>
> each web page click invokes the bean's constructor 3 times for a page with
> 3 input fields; -- i added a no-arg constructor to the bean to track
> instantiations in the beans class with static volaitile int fields.
>
> and i thought that since finalize is in every class -- inherited from
> Object that it would not be possible to get an exception overriding
> finalize.
>
> Now i trying to understand WHY this does not work -- planning to step thru
> gfv301 source code for some insights;
>
> I did see the specs do say to not override/implement finalize --but they do
> not say why.
>
> I am learning and at this point guessing is has something to do with Proxy
>
> thanks
>
> gary
>
>
> ----- Original Message -----
> *From:* Steven Siebert <smsiebe_at_gmail.com>
> *To:* marina.vatkina_at_oracle.com
> *Cc:* emiddio-frontier <emiddio_at_frontier.com> ; users_at_glassfish.java.net
> *Sent:* Friday, February 25, 2011 4:51 AM
> *Subject:* Re: why does stateful ejb throw exception for calling
> super.finalize();
>
> Gary,
>
> What are you trying to accomplish? It's likely you can accomplish what you
> are looking for by using the session beans lifecycle callbacks.
>
> S
>
> On Thu, Feb 24, 2011 at 10:47 PM, Marina Vatkina <
> marina.vatkina_at_oracle.com> wrote:
>
>> No, it's not ok to have finalaize method in the bean class.
>>
>> -marina
>>
>> emiddio-frontier wrote:
>>
>>> So does this mean its okay to have finalaize method but dont call
>>> super.finalize(); ???
>>>
>>> I will try to find the spec section you mention.
>>>
>>> thanks
>>>
>>> gary
>>>
>>