gpt4 book ai didi

java - 在以下情况下如果发生失败则如何回滚

转载 作者:行者123 更新时间:2023-12-02 15:57:10 25 4
gpt4 key购买 nike

以下是根据一些约束将“书目表”,“学生书目”和“保存”到“学生和书本”的映射的类。

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();
}

}


}

在这里,我的问题是在评估失败的情况下,我期望所有数据都完全回滚,但无法解决,您能告诉我我在哪里出错。

在此先感谢您的帮助!

最佳答案

调用save()方法时,传递failOnError:true的参数,即;保存(failOnError:true)

关于java - 在以下情况下如果发生失败则如何回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33032673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com