[英]How to rollback in case of failure in grails in below case
以下是根據一些約束將“書目表”,“學生表”和“保存”到“學生書”的映射的類。
class SaveMyDataService {
@ Transactional(propagation = Propagation.REQUIRES_NEW)
def saveBooks(List < Book > bookList) {
/*
some operation on bookList and then saving each Book Object individually
*/
}
@ Transactional(propagation = Propagation.REQUIRES_NEW)
def saveStudent(List < Student > studentList) {
/*
some operation on studentList and then saving each Student Object individually
*/
}
@ Transactional(propagation = Propagation.REQUIRES_NEW)
def saveStudentBookMapping() {
List < Book > bookList = Book.getAll();
List < Student > studentList = Studnet.getAll();
for (int i = 0; i < studnetList.size(); i++) {
StudentBookMapping obj = new StudentBookMapping();
/*
after some operation we map the Book & Student and save the StudentBookMapping Object
*/
obj.save();
}
}
}
該服務類調用SaveMyDataService和其他服務的方法來獲取數據,然后在可能發生異常的情況下進行一些評估,在這種情況下,我希望回滾所有數據,例如Books,Students及其映射,但無法解決。
class FetchAllNewBooksAndStudent{
def saveMyDataService;
def xmlParsingSerivice;
def evaluationService;
getStudentAndBooksData()
{
try{
/*
Some IO operations to get the Student & Book List from XML
*/
List<Student> studList = xmlParsing.getStudList();
List<Book> bookList = xmlParsing.getBookList();
// here we call the save book service
saveMyDataService.saveBooks(bookList);
// here we call the save Student service
saveMyDataService.saveStudent(studList);
// here we call the mapping service to map the Student & Books
saveMyDataService.saveStudentBookMapping();
/*
after this I do some Evaluation operation but here might be chances of getting exception in such a case I want to rollback all the above entries made like books, student & their mapping but its not working out
*/
evaluationService.evaluate();
}
catch(Exception e)
{
log.error e.getMessage();
}
}
}
該作業每隔4小時運行一次,以檢查Student / Books / StudentBookMapping的新數據,並調用fetchAllNewBooksAndStudent類的getStudentAndBooksData服務
class RegularBookStudentCheckJob{
def fetchAllNewBooksAndStudent;
static triggers = {
simple repeatInterval: Long.parseLong(ConfigHolder.config.REGULAR_BOOK_STUDENT_CHECK_JOB_REPEAT_INTERVAL), // here value is 4hrs
startDelay : 60000
}
def execute(){
if(String.valueOf(ConfigHolder.config.RUN_BOOK_STUDENT_CHECK_JOB).equalsIgnoreCase("true"))
{
fetchAllNewBooksAndStudent.getStudentAndBooksData();
}
}
}
在這里,我的問題是在評估失敗的情況下,我期望所有數據都完全回滾,但仍無法解決,請讓我知道我在哪里出錯。
在此先感謝您的幫助!
在調用save()方法時,傳遞failOnError:true的參數,即; 保存(failOnError:true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.