users@glassfish.java.net

JMS Topic to MDB to EJB to JPA - Data Integrity/Insertion Problems

From: <glassfish_at_javadesktop.org>
Date: Tue, 08 Apr 2008 13:53:51 PDT

Glassfish V2 JMS/MDB/JPA problem....

I have a JMS Topic setup to receive ObjectMessage's from an external client, with a Message Driven Bean listening on said Topic to pull off any received objects and to send them to their appropriate EJB facade, which in turn inserts the object into the DB via JPA.

The data we are placing on the topic should *never* be identical to anything already inserted into the database, however I still do the following check just in case:

foundContact = em.find(Contact.class, contact.getContactPK());
if(foundContact == null) {
     em.persist(contact);
     em.flush();
}

What I am finding is that when I let the data trickle in, everything works as expected. However, when I let the data flow at full speed, I start getting EntityExists exceptions. The faster I drop data onto the JMS Topic, the more likely my chances of getting an exception are.

Since I know for a fact that every object we are placing onto the topic is unique and not already in the DB, this tells me that somehow multiple instances of the MDB are somehow grabbing the same message off of the Topic. I'm guessing both MDB's are passing the duped object to the EJB for processing. Both EJB's do a find(object.class, objectPK) and get back a null, so both go forward with inserting the object. Since one ultimately has to insert first, the second then fails with the EntityExists. This is NOT how MDB's are intended to work. It seems like a bug, but I have a difficult time believing that something like this would not have been found already.

Since everything works fine if I let the data go in a little more slowly, it almost appears as though it's a threading problem with MDB's within the EJB container of Glassfish.

It's essentially the same thing as this Java 5 EE Tutorial example:
http://java.sun.com/javaee/5/docs/tutorial/doc/bnchf.html
except the MDB passes the object to an EJB, which does the persistence, instead of the MDB doing it itself.

Am I doing anything wrong?
[Message sent by forum member 'jamesdh' (jamesdh)]

http://forums.java.net/jive/thread.jspa?messageID=268250