Message driven beans(MDB) and transaction attributes

"Message-driven beans may declare only the NOT_SUPPORTED or REQUIRED transaction attribute. The other transaction attributes don't make sense in message-driven beans because they apply to client-initiated transactions."

Why ?

MDB are called asynchronously. Therefore NEVER, SUPPORTS, REQUIRES_NEW transaction attributes are not make sense, because there isn't pre-existing transaction context for MDB.

NEVER throws Exception if there is pre-existing transaction context, but that don't ever occur.

If there is pre-existing transaction context, SUPPORTS will run in that transaction context. (But, there is NO such in MDB!)

REQUIRES_NEW always run in new transaction context, but this behavior is always the same to REQUIRED.

REQUIRED Vs. REQUIRES_NEW

why are we allowed to use a REQUIRED , but not a REQUIRES_NEW?

When we specify theh RequiresNew, it comes with an implicit understanding that if the client has a transaction, it needs to be suspended. i.e. the existence of a client is almost always assumed in the case of REQUIRES_NEW. Its as if we are saying- hey client, just take a break, and dont disturb me while i am acting on my world dominance plan. Ill inform you to do your boring stuff when i am done!

Whereas, in the case of REQUIRED, all that we are trying to tell the runtime environment is that my MDB should have a transaction associated with it. There is no assumption of the existence of a client. Hence, Required seems like a more direct way of telling the container to start a transaction than REQUIRES_NEW.

Comments

Popular posts from this blog

-- How to Schedule a Task --

How to Connect DataBase to Java Application -JDBC

-- Are you a Android Developer? --