Grails - Saving multiple object, Rollback all object if one fails to save

落花浮王杯 提交于 2019-11-28 02:17:34

问题


I need to save multiple object at once, and rollback all if one object fails to save. For example :

class Transaction {
  Item item;
}

class Item {
  date lastTransaction;
}

If I create new Transaction, I need to change lastTransaction value and save the item.
If I failed to save the item, I need to rollback the Transaction (vice versa).

Any ideas?


回答1:


Yuck. Don't throw exceptions to roll back transactions. You're incurring a pretty high cost to take advantage of a side effect where the transaction manager, assuming that a runtime exception means that you're not in control, automatically rolls back the transaction for you to keep you from doing more damage. It's a bit like being lonely and hitting yourself in the head repeatedly with a hammer so some EMTs and perhaps a nurse or a doctor will spend some time with you.

It's pretty easy to roll back a transaction, but unfortunately Grails doesn't expose any of this:

import org.springframework.transaction.interceptor.TransactionAspectSupport

class FooService {

   def someMethod(...) {

      boolean somethingBadHappened = ...
      if (somethingBadHappened) {
         // roll back
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
      }

      // ok, proceed
      ...
   }
}

And later you can check if the current transaction was rolled back with

TransactionAspectSupport.currentTransactionStatus().isRollbackOnly()

Note that this won't work in a controller since the transaction will have ended by that point.




回答2:


Create a service method and put your persistence logic in there. When you call save, if you specify failOnError: true then if there are validation problems an exception will be thrown and your transaction will be rolled back.

def myServiceMethod(...some parameters...) {
    ....
    Item item = new Item(lastTransaction: new Date())
    item.save(failOnError: true)
    Transaction transaction = new Transaction(item: item)
    transaction.save(failOnError: true)
    ...
    return transaction
}

Also, have a read through of the services section of the Grails docs.



来源:https://stackoverflow.com/questions/21138173/grails-saving-multiple-object-rollback-all-object-if-one-fails-to-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!