• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateless session bean with CMT, sending JMS message on commit

 
Jan Van den bosch
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got a stateless session bean with CMT. One of its methods does two things:

1) it looks up an entity bean to update a row in the DB.

2) it sends a message to a queue to be picked up by a message-drive bean, which will perform a query on the DB, do some calculations and update lots (possibly hundreds of thousands) of rows in the DB.

The calculations and the DB update are to be performed asynchrously since they may take a lot of time, hence the message-driven bean.

The query in the MDB in step 2 relies on the row that was updated by the entity bean in step 1. The problem is, this row isn't updated until the transaction is committed after the session bean's method returns. By then, the MDB could have already stepped in and performed its query and recalculations with the old value, and it does.

I believe stateful session beans can use the SessionSynchronization interface to cope with this kind of problem. How can I handle this with a stateless session bean?
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After doing step 1, try to force database synchronization by calling em.flush().

As far as I know, container managed transactions are deferred and invoked only after the method returns. Which means that altering the state of a managed entity after persist will invoke another update query.

For example,

em.persist(a);
a.setProperty(someValue);

This will produced to queries - One for inserting object a. Another for updating the property of object a. The transactions are deferred and batch to improve peformance.

A workaround is explicitly calling em.flush so that database changes will be synchronized.

I hope this helps.
 
Jan Van den bosch
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using EJB 2.0.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!