Home How can I isolate a single delete(..) from being rolled back during runtime exception in grails 2.5.6?
Reply: 1

How can I isolate a single delete(..) from being rolled back during runtime exception in grails 2.5.6?

Bere
1#
Bere Published in 2018-02-14 08:37:31Z

I am working on a legacy grails 2.5.6 project.

As part of the logic, an exception(type of RuntimeException) is thrown when an orphaned record is detected. While doing so, the orphaned record has to be deleted. This logic is deep inside layers of service method calls which are in a big transactional block (by default).

Here is a sample code:

...
financialTransactionItem.delete(flush:true); /* flush: true is for a reason here */
throw new ServiceException(...);
...

But, obviously, the delete is rolled back when the ServiceException(derived from RuntimeException) is thrown and the orphaned record (financialTransactionItem) stays there.

The rollback is still necessary, as there are other DB transactions that must be discarded.

My question is: How can I do both? i.e. Rollback everything except the specified delete operation?

Bere
2#
Bere Reply to 2018-02-15 10:16:53Z

I enclosed the delete statement inside a withNewTransaction(...) block and the rollback didn't affect the delete operation.

FinancialTransactionItem.withNewTransaction {TransactionStatus status ->

    FinancialTransactionItem obsoleteFinancialTransactionItem = FinancialTransactionItem.get(ftiItemId);

    if(obsoleteFinancialTransactionItem){

        obsoleteFinancialTransactionItem.delete(flush: true);
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299544 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO