aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How to update an entity when the persistence context is extended? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to update an entity when the persistence context is extended?" Watch "How to update an entity when the persistence context is extended?" New topic
Author

How to update an entity when the persistence context is extended?

Jaaouane Aymen
Greenhorn

Joined: Sep 22, 2009
Posts: 29
Hello,
I have an EJB which represents my DAO layer,
To update an entity, we do em.merge(user) if the scope of the persistence context is a an EJB method,
but when the persistence context is extended, i do not find the way to update the entity. It is not legal to
write a merge because the entity is not detached (PersistenceContextType.EXTENDED).
I do for that an em.flush() but it is not very correct because this operation will flush all the entity manager
and i want to do an update for only user object.


is there a correct statement to replace this em.flush?
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Jaaouane,

if your entity is attached and the changes are made within a transactional method, then the entity manager will perform a flush automatically when the transaction commits. So usually there is no need for flushing manually.




SCJP 5 (98%) - SCBCD 5 (98%)
Jaaouane Aymen
Greenhorn

Joined: Sep 22, 2009
Posts: 29
Hi Ralph,
thank you for your help,

this update method of EJB is called from a seam component

public void save() {
accountAction.update(selectedUser);
StatusMessages.instance().addFromResourceBundle(Severity.INFO, "fr.aptus.siaptus.delete.error.objectModified", "Utilisateur");
}

The problem is that when an exception occurs, the message of success is also displayed because the transaction is committed after the end of the save method and not in the middle of the save method, (the transaction is not committed after the update method which is an EJB method).
so, why the transaction is committed after the end of the save method and not after the end of the update method.As i read, the life cycle of a transaction is the EJB method. Is the life cycle of transaction has changed because i am using seam?
can you explain for me when a transaction is started and when a transaction is committed when we are using seam with EJB.
but when i put em.flush in the EJB, the transaction is committed after the call of update.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
The problem is that when an exception occurs, the message of success is also displayed because the transaction is committed after the end of the save method and not in the middle of the save method, (the transaction is not committed after the update method which is an EJB method).
OK, I see.

Transactions in EJB are JTA based (exception: entity transactions, but this doesn't matter here) and @TransactionAttribute is used to configure in which transaction scope an ejb business method is performed.

Default is REQUIRED: In case that the ejb method is called from within a JTA transaction, this means that the ejb method is executed in the scope of this transaction, too. In this scenario the transaction still lasts when the ejb method has finished.

I don't know about seam's transaction management. But according to your observations it looks to me like the following: The save method is performed within a JTA transaction (started by seam) which calls the ejb method update (that uses the transaction attribute REQUIRED). When save has finished, the transaction completes and the persintence context is flushed.

So in your situation it seems best to me to use em.flush in the update method as indicated in your code. 'Cause if synchronization fails an exception is thrown and you can show up a corresponding status message.

Technically it would also be possible to perform update in a new transaction by using transaction attribute REQUIRES_NEW. Then this new transaction would complete when update ends. But starting and completing transactions is time consuming and from a functional point of view it seems reasonable to me that save und update belong to the same transaction.

but when i put em.flush in the EJB, the transaction is committed after the call of update.
flush doesn't complete the transaction but synchronizes the persistence context with the db.

I do for that an em.flush() but it is not very correct because this operation will flush all the entity manager and i want to do an update for only user object.
To my knowledge it's in fact not possible to flush a particalur entity. But usually the persistence context knows which of its entities have changed. So if the persistence context contains 1000 entities and two have changed then only two will be flushed. Using appropriate logging settings you can see the sql operations in the log file. At least using em.flush does the same as if the flush is performed automatically when the transaction commits.
 
Consider Paul's rocket mass heater.
 
subject: How to update an entity when the persistence context is extended?