以下是基於某些約束將圖書清單,學生列表&保存爲學生&圖書的類的類。如何在以下情況下在grails中發生故障時進行回滾
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的方法&其他服務來獲得數據之後,它做一些評價 那裏可能是我期待回滾所有的這種情況下獲得異常的可能性數據即書籍,學生&他們的映射,但它沒有工作。
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個小時運行後,檢查學生/書籍/ 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();
}
}
}
這裏我的問題是評估失敗的情況下我期望完全回滾所有數據,但它不能解決問題,請讓我知道我犯了什麼錯誤。
在此先感謝您的幫助!
要回滾事務,您需要拋出運行時異常http://stackoverflow.com/questions/2979786/rolling-back-a-transaction-in-a-grails-service – Vahid