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'm using container managed transactions)

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 the ((Session)entityManager.getDelegate()).refresh(t); approach and still no luck!

I'd greatly appreciate some help on this since I've been trying to find a solution for a while now with not luck. Even a link to point me in the right direction would be greatly appreciated at this time.

Thank you for your time in advance.

Regards
Drayton

@Stateless
@WebService
public class Spooler implements SpoolerLocal {

...

public Job getNextJobForDevice(String deviceName) throws SpoolerException {

...

List<Job> jobs = jobFacadeLocal.findPendingByStockTypes(device.getStockTypes());

for (Job job : jobs) {

...

jobFacadeLocal.print(job, device);

//The changes made in the 'jobFacadeLocal.print(job, device)' are visible, but the entity version number has not changed (even though it has been updated in the db)

// I have tried refreshing the job entity here, but the refresh returns a job entity as it was before the 'jobFacadeLocal.print(job, device)' (still with the old version number).
// As I intend to use the updated values of the 'job' entity here, this will not do.

//The code here should only execute if the transaction above has not been rolled back. (Thus the REQUIRES_NEW annotation on the method)

...

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); //This just passes the 'job' entity to entityManager.merge(job);

...
}
}