I am using a stateless EJB class to update a persistence entity located in a database. A method within the EJB calls an implementation class where the work is done. I believe what is causing the issue is that an entity called Foo
, has a oneToMany relationship with an entity Bar
. Things are done, and the Session is updated with Foo
which 'cascades' to Bar
. When a StaleObjectStateException
occurs, the transaction is not being fully rolled back which is causing errors for obvious reasons.
EJB:
private Session getSession() throws BusinessException {
if( this.sess == null ) {
ServiceLocator locator = new ServiceLocator();
SessionFactory sf = locator.getHibernateSessionFactory();
this.sess = sf.openSession();
}
return this.sess;
}
private ProductionOrderImpl getImpl() throws BusinessException {
if( this.impl == null ) {
this.impl = new ProductionOrderImpl( getSession() );
}
return this.impl;
}
public void cutoffOrders( ) throws Exception {
Transaction tx = null;
try {
tx = getSession().beginTransaction();
getImpl().cutOffFoos(fooTime);
tx.commit();
} catch (StaleObjectStateException e1){
if (tx != null) tx.rollback();
logger.error( "Failed to cutoff order : " + e1 );
throw new Exception( LocaleMgr.getMessage());
}
finally {
// reset implementation object, close session,
// and reset session object
impl = null;
sess.close();
sess = null;
}
}
Implementation:
public ProductionOrderImpl(Session sess) {
this.sess = sess;
}
public void cutoffFoos( Timestamp fooTime) throws Exception {
... Code that gets fooList ...
if( fooList != null ) {
for( Foo foo: fooList ) {
for( Bar bar : foo.getBarList() ) {
... Code that does things with existing Barlist ...
if( ... ) {
... Code that makes new Bar object ...
foo.getBarList().add(bar2);
}
}
sess.update( foo );
}
}
}
Relevant Foo code:
@OneToMany(cascade=CascadeType.ALL, mappedBy="foo")
@OrderBy("startTime DESC")
Set<Bar> barList;
So basically, when the Transaction tries to rollback, the Bar parts that were changed get rolled back, but the new Bar (bar2 in code) records remain..
Any guidance would be appreciated. Like I said I believe the error here has to do with the sess.update(foo)
; possibly something to do with autocommit
, but by default it should be off.
I believe that what is happening, is that the Session.Update(foo) is in turn creating two separate transactions. Specifically, the Foo
is updated (SQL UPDATE), but the Bar
is saved (SQL INSERT). Since the transaction context would only really see the SQL UPDATE, that is all it reverses. Will have to look into this more..
I have tried changing the Session.FlushMode
to COMMIT
but it still doesn't seem to fix the issue. It does however, partially fix the problem.. It will rollback the entries properly except for the particular entry which causes the StaleObjectStateException. That particular entry is actually deleted right out of the database...
sess
(I assume some kind of persistence Session) ? If you are using EJBs, you should really be using Container managed transactions and EntityManager instead of custom tx and session. With regard to your problem, I guess thatsess
object in the EJB is not the same assess
in you impl class. – yntelectual Nov 13 '15 at 11:42sess
is passed into the impl via the constructor. You might be right. I've fixed my issue, but I'll look into what you say, because I think it would be a cleaner fix. – ballBreaker Nov 13 '15 at 14:08