i using stateless ejb class update persistence entity located in database. method within ejb calls implementation class work done. believe causing issue entity called foo, has onetomany relationship entity bar. things done, , session updated foo 'cascades' bar. when staleobjectstateexception occurs, transaction not being rolled causing errors 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 cutoff order : " + e1 ); throw new exception( localemgr.getmessage()); } { // reset implementation object, close session, // , 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 gets foolist ... if( foolist != null ) { for( foo foo: foolist ) { for( bar bar : foo.getbarlist() ) { ... code things existing barlist ... if( ... ) { ... code 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 transaction tries rollback, bar parts changed rolled back, new bar (bar2 in code) records remain..
any guidance appreciated. said believe error here has sess.update(foo); possibly autocommit, default should off.
i believe happening, session.update(foo) in turn creating 2 separate transactions. specifically, foo updated (sql update), bar saved (sql insert). since transaction context see sql update, reverses. have more..
i have tried changing session.flushmode commit still doesn't seem fix issue. however, partially fix problem.. rollback entries except particular entry causes staleobjectstateexception. particular entry deleted right out of database...
well managed fix issue.. wait accept in case else posts better, more... bounty worthy.
basically, changing flushmode manual, , manually flushing throughout, can catch staleobjectexception earlier, backs code out sooner. still have artifacts of partially rolled-back records.. however, method runs every 2 minutes on schedule, during second pass fixes of issues.
i changed ejb have following:
public void cutofforders( ) throws exception { transaction tx = null; try { tx = getsession().begintransaction(); getsession().setflushmode(flushmode.manual); getimpl().cutofffoos(footime); getsession().flush(); tx.commit(); } catch (staleobjectstateexception e1){ if (tx != null) tx.rollback(); logger.error( "failed cutoff order : " + e1 ); throw new exception( localemgr.getmessage()); } { // reset implementation object, close session, // , reset session object impl = null; sess.close(); sess = null; } } then implementation code have following:
public void cutofffoos( timestamp footime) throws exception { ... code gets foolist ... if( foolist != null ) { for( foo foo: foolist ) { for( bar bar : foo.getbarlist() ) { ... code things existing barlist ... sess.flush(); if( ... ) { ... code makes new bar object ... foo.getbarlist().add(bar2); } } sess.flush(); sess.update( foo ); } } }
Comments
Post a Comment