Commit data in the middle of Transaction inside the Container


Many times I was dealing an issue inside the Application server’s container(Jboss, Weblogic).

I had the need to commit data(Logs, Database) in middle of Transaction.

For example let’s say I want to update some records in the Database and then do some logic on the UPDATED records within the same transaction.

This wont be possible until the current Transaction will finish it’s work.

So the walk through is starting  new Transaction within the current Transaction.

When the container is already performing a transaction, then starting a new transaction will result in the original being suspended. When the new transaction has committed, the original transaction will be resumed.

I am using EJB3.0. The annotation for starting new Transaction within running transaction is

protected void doSomeDatabaseUpdate(...)
   //Some logic that must be committed before the main transaction finish

The result:
The method doSomeDatabaseUpdate will be invoked inside new transaction
and when finished the data will be committed.
Afterwards we can continue with our logic on the updated
data within the main transaction.

Related Articles: