UnexpectedRollbackException - a full scenario analysis

后端 未结 3 934
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 10:21

All I know about this exception is from Spring\'s documentation and some forum posts with frostrated developers pasting huge stack traces, and no replies.

From Sprin

3条回答
  •  借酒劲吻你
    2020-12-05 11:05

    Well I can tell you how to reproduce the UnexpectedRollbackException. I was working on my project, and I got this UnexpectedRollbackException in following situation. I am having controller, service and dao layers in my project. What I did is in my service layer class,

    class SomeServiceClass {
        void outerTransaction() {
            // some lines of code
            innerTransaction();
            //AbstractPlatformTransactionManager tries to commit but UnexpectedRollbackException is thrown, not here but in controller layer class that uses SomeServiceClass.
        }
    
        void innerTransaction() {
            try {
                // someDaoMethod or someDaoOperation that throws exception
            } catch(Exception e) {
                // when exception is caught, transaction is rolled back, outer transaction does not know about it.
                // I got this point where inner transaction gets rolled back when I set HibernateTransactionManager.setFailEarlyOnGlobalRollbackOnly(true)
                // FYI : use of following second dao access is wrong, 
                try {
                    // again some dao operation
                } catch(Exception e1) {
                    throw e2;
                }
            }
        }
    }
    

提交回复
热议问题