如果在以下情况下grails失败的情况下如何回滚

时间:2015-10-09 07:43:25

标签: java 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&amp;的方法。获取数据的其他服务,然后进行一些评估 在这种情况下可能会出现异常的情况我希望能够回滚所有数据,即书籍,学生和他们的映射但它没有成功。

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

    }


}

这里我的问题是在评估失败的情况下我希望完全回滚所有数据,但是它没有成功,请你告诉我哪里出错了。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

相关问题