Exception when updating an entity after the completion of a transaction which has already updated the same entity.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Exception when updating an entity after the completion of a transaction which has already updated the same entity.

Drayton Brown
Hi all, please can you help me with this, its driving me crazy! 

I'm experiencing the following exception: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

It seems this exception is being thrown on the completion of the 'getNextJobForDevice' method outlined below. From what I can tell it seems that the when the implicit transaction for the 'jobFacadeLocal.print(job, device)' device is committed, we can no longer use any entities that were updated in this transaction. 

I've tried to refresh the object from the database after this transaction. I've tried using the entityManager.refresh() mehtod to do this, but it seems to return the entity without any changes committed from the 'jobFacadeLocal.print(job, device)' method. I don't quite understand this since I'm expecting the changes to have committed as this stage.

I've also tried to remove the implicit transaction on the 'getNextJobForDevice' method, by placing a '@TransactionAttribute(TransactionAttributeType.NEVER)' on it. This however does not help since we get a new exception: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
I tried to counter this exception by calling 'Hibernate.initialize()', but then I got this exception: org.hibernate.HibernateException: collection is not associated with any session

I'd greatly appreciate some help on this, thank you for your time in advance.

Regards
Drayton

@Stateless
@WebService
public class Spooler implements SpoolerLocal {

...

public Job getNextJobForDevice(String deviceName) throws SpoolerException {

...

jobFacadeLocal.print(job, device);

// I have tried refreshing the job entity here, but the refresh returns a job entity as it was before the 'jobFacadeLocal.print(job, device)'

//The code here should only execute if the transaction above has not been rolled back.

jobFacadeLocal.update(job);

...

return job;
}
}


@Stateless
public class JobFacade extends Base<Job> implements JobFacadeLocal {

...
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void print(Job job, Device device) throws BankException, LocationException, JobException, StateException  {

...

jobFacadeLocal.update(job);

...
}
}


Loading...