• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrency problem  RSS feed

 
Josechu Leton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im sending messages from several applications to a JMS queue and then get them with a MDB, then I create an object with the information of every mesage and adds it to the DB using JPA. Before adding a new the object to the DB i need to get one of the attributes of the last object added, perform some operations with it, and add it to the object that is going to be inserted in the database.
The trouble comes when getting the last object form the database, because in the time slot that the method is getting the last obejct of the DB, performs some operations and add the new object, another message comes to the MDB and take the same last object as previous one.
Its kind of hard to explain this, i hope you understand what i mean.
The solution im seeking is to make a transaction that blocks the table (the whole table??) so there are not concurrency problems.
Is there any way of specifying that on a EJB transaction? I`ve cheked some books and it seems that the isolation level of a transaction cannot be set in EJB.
Maybe there is another way to solve this problem...

Thanks for your help
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how changing the transaction isolation level would help you here. You need pessimistic locking on the database table I think. Which database are you using?

 
Josechu Leton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im using MySQL.
As far as I understand, If you set the Isolation Level of the transaction as Serializable it wont let that the table can be read or written till the transaction finishes.
Is that right?
Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I think about it, you could implement pessimistic locking using serlializable transaction isolation, but the more common way would be to aquire a perssimistic lock in your transaction. I think in MySQL you do this by specifying a ttype of locking cursor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!