【发布时间】:2016-01-07 01:44:00
【问题描述】:
以下是根据一些约束保存图书列表、学生列表和学生和图书映射的类。
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 小时运行一次,检查 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();
}
}
}
我的问题是在评估失败的情况下,我希望所有数据都完全回滚,但它无法解决,请告诉我哪里出错了。
提前感谢您的帮助!
【问题讨论】:
-
要回滚事务,您需要抛出运行时异常stackoverflow.com/questions/2979786/…