the never ending flow of optimistic locking questions
beligum Jun 19, 2007 6:42 AMHi all,
This question must be getting boresome to some of you experts here,
but please help an intermediate developer along with some answers to a few simple questions regarding the subject.
I use the @Version annotation in my EJB3 entity bean (Seam, JTA), and, while a user edits the bean-contents, it's possible the info is updated through a JMS-callback from an external source.
Now, I would like to select a few (modified-behind-the-scenes) fields from the database, and let them precede the user-input whatsoever.
So, when I call my entityManager.flush() method (Seam-managed em), it throws a StaleObjectStateException, because of the JMS-update. In this post (http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3919517#3919517), Gavin points out that it's possible to present the user with the changes made in the DB, by starting up a new context/transaction. He even points out how to do it, but in his own expert-style, the explanation is still quite cryptic to me.
Can anyone elaborate on the:
To "get" a new PC/txn to work with, call a session bean in the context of a REQUIRES_NEW transaction (if it is stateful, it needs to be a newly instantiated SFSB).
This is my relevant code (a conversation-scoped SFSB in a long-running conversation/transaction):
public String updateEntity() { ... try { entityManager.merge(getSelectedInode()); entityManager.flush(); return "success"; } catch (OptimisticLockException e) { VersionConflictManager conflictManager = (VersionConflictManager)Component.getInstance("versionConflictManager"); File dbFile = (File)conflictManager.getConflictingDbObject(File.class, getSelectedInode().getFileSystemEntity().getId()); System.out.println("test"); } }
And here's my VersionConflictManager
@Stateful @Scope(CONVERSATION) @Name("versionConflictManager") public class VersionConflictManager implements Serializable { //-----VARIABLES----- @In(create=true) EntityManager entityManager; //-----CONSTRUCTORS----- public VersionConflictManager() { } //-----GETTERS/SETTERS------ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public Object getConflictingDbObject(Class entityClass, int id) { return entityManager.find(entityClass, id); } @Remove @Destroy public void destroy() { } }
Apparently, I'm missing something, because I get a StaleObjectStateException when I try to instantiate the VersionConflictManager in the catch-code.
Any help is appreciated,
bram