DB constraint violation not throwing Exception in Hibernate

前端 未结 2 744
攒了一身酷
攒了一身酷 2021-01-17 02:36

I have the following code:

try {
    userDAO1.save(userRecord);
    userDAO2.save(userRecord);
}
catch(DataIntegrityViolationException e) {
    throw new App         


        
2条回答
  •  无人共我
    2021-01-17 03:22

    What made you to believe that DataIntegrityViolationException was not thrown while the statement userDAO1.save() was executed? Also, why do you believe that the statement userDAO2.save() was executed as well?

    If the above opinions were made upon observations of code execution progression in an IDE debug console such as that of Eclipse then the interpretations might be wrong.

    Please try to observe the results by punching-in some debug statements like the ones below, and executing the code. This may help you to find out the root cause of the failure -

    try {
        userDAO1.save(userRecord);
        System.out.println("-- After userDAO1.save(userRecord) --");
        userDAO2.save(userRecord);
        System.out.println("-- After userDAO2.save(userRecord) --");
    } catch(DataIntegrityViolationException e) {
        e.printStackTrace();
        throw new ApplicationException("Contraint violated")
    }
    

提交回复
热议问题