Hello,
I'd like to achieve something like this:
So I've tried:Code:public interface RequestCallback { public void onRequest( Request request ); } @Transactional public void processPendingRequests() { requestDao.iteratePending( new RequestCallback() { public void onRequest( Request request ) { requestDao.lock( request ); process( request ); // I would like a commit here! } } }
did not work. So I went to:Code:@Transactional public void processPendingRequests() { requestDao.iteratePending( new RequestCallback() { public void onRequest( Request request ) { TransactionTemplate tt = new TransactionTemplate( transactionManager ) // should be created elsewhere I know; tt.execute( new TransactionCallbackWithoutResult() ) { @Override protected void doInTransactionWithoutResult( TransactionStatus transactionstatus ) { requestDao.lock( request ); process( request ); } } // commit does not happen here at all } } } // commit happens here
Now it works but with every tt.execute I get:Code:@Transactional public void processPendingRequests() { requestDao.iteratePending( new RequestCallback() { public void onRequest( Request request ) { TransactionTemplate tt = new TransactionTemplate( transactionManager ) // should be created elsewhere I know; tt.setPropagationBehavior( TransactionDefinition.PROPAGATION_REQUIRES_NEW ); // <--------- opens new hibernate session :( tt.execute( new TransactionCallbackWithoutResult() ) { @Override protected void doInTransactionWithoutResult( TransactionStatus transactionstatus ) { requestDao.lock( request ); process( request ); } } // commit does happen here } } }
- main transaction suspended
- new hibernate session created
- new transaction created
- request processed
- transaction commited
- session closed
- main transaction resumed
I have also tried to manage transactions completely by hand (without use of transaction template and @Transactional interface). Second call to
transactionManager.commit( transactionStatus ) raised an exception: Transaction is already completed.
There is totally no problem to do this in pure hibernate. I am lost in Spring's transaction framework.


Reply With Quote
