2012-10-09 39 views
1

當我們嘗試保存對象和子對象時,我們遇到了一些奇怪的行爲和與事務不一致的地方。不幸的是,返回給我們的錯誤根本沒有幫助我們。我想知道是否有辦法獲得有關此例外的更多信息。我們正在分析我們的Model類和JPA註釋以確保所有內容都正確配置。這是目前正在從一個jUnit測試案例運行。在交易中保存對象時是否需要特別考慮?我們不想做任何特別的事情。我們正在創建一個Parent對象以及多個子對象,並試圖將其保存到數據庫中。當我們只是得到一個通用的「無法提交JPA」錯誤時,調試這個問題就很困難。任何幫助深表感謝。檢索Spring JPA事務異常的更多錯誤信息

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction rolled back because transaction was set to RollbackOnly. 
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:476) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:374) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
at $Proxy16.createIncident(Unknown Source) 
at eg.test.IncidentTests.testCreateIncident(IncidentTests.java:135) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: javax.persistence.RollbackException: Transaction rolled back because transaction was set to RollbackOnly. 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:92) 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63) 
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467) 
+0

你能顯示一些代碼片段嗎?也許你的豆的一部分? DAO?和你的單元測試。你在單元測試中有正確的註釋嗎? @RunWith(SpringJUnit4ClassRunner.class) loader = AnnotationConfigContextLoader.class) – sbzoom

回答

1

您可以嘗試啓用sql trace並觀察執行的原始SQL。

+0

在我的特殊情況下,檢查RDBMS日誌也很有幫助。查詢本身看起來很無辜,但日誌顯示它確實違反了一些限制。 –

0

我想你捕捉並忽略導致回滾某處的異常。檢查事務邊界內的代碼是否有被忽略的異常。

另一種可能性是事務明確標記爲回滾 - 僅通過調用EntityTransactionTransactionStatus上的setRollbackOnly()

+0

我將驗證交易是否被標記。令我們困惑的是,它可以節省基本的型號更新。看起來,當你開始添加子對象時,事情開始失敗。另一件事,我們的邏輯中沒有例外。這是在春天開始發揮魔力的時候爆炸了。我們啓用了SQL日誌記錄功能,但不顯示任何錯誤信息。我們在發生異常之前看到的最後兩條SQL語句是,它正在爲序列中的下一個值(在Oracle DB上運行)爲它試圖創建的兩個對象獲取下一個值。 –