Skip to main content

@PreDestroy not implemented?

2 replies [Last post]
sjoekie
Offline
Joined: 2007-09-22

Hello,

I have an EJB that needs some cleaning up before it can be deleted. So I tried adding a method annotated with "@PreDestroy" but supprisingly this method is never called.
So my first question is, is this a known problem? My second question is, is there a workaround?

Just as an example, I created an ejb named testEJB with the following methods:

@PrePersist
public void prepersist(){
System.out.println("prepersist");
}

@PostLoad
public void postload(){
System.out.println("postload");
}

@PreDestroy
public void predestroy(){
System.out.println("predestroy");
}

I then created a servlet with 2 buttons, one "add ejb" button (which creates a new ejb) and one "remove All" (which removes all these test-ejbs. Each time this page is called, it also counted how many ejb's there were. you can find these source-files appended to this message.

After pressing "add ejb" once there appeared on my console:
prepersist
postload

I pressed it again and it said:
prepersist
postload
postload

Finally I pressed "remove All" and my console said:
postload
postload

However, this should have been
postload
postload
predestroy
predestroy
.

This indicates that the @PreDestroy annotation is simply ignored.

As for my system, I'm using GlassFish V2 with NetBeans 6.0.1 on a mac running OSX Leopard.

greetz,
sjoekie

(as I can only attach 2 files, you'll have to create the facades yourself.)
postload[/i]
However, there was no "predestroy" message on my console, and still they were destroyed.

Greetz,
Sjoekie

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mvatkina
Offline
Joined: 2005-04-04

If you are defining JPA callbacks, you need to use @PreRemove or @PostRemove depending on your needs.

Regards,
-marina

sjoekie
Offline
Joined: 2007-09-22

Hehe works like a charm, thanks a lot!