我正在使用Spring 3.1.4.RELEASE
,Hibernate 4.1.0.Final
,JPA 2
,JBoss 7.1.3.AS
和MySQL 5.5.37
。有时当我们尝试提交事务时,它会失败,尽管令人讨厌的是回滚失败,因此系统中的这种锁永远不会消失并导致其他事情失败。回滚异常堆栈跟踪是
05:00:07,751 ERROR [org.mainco.subco.myproject.service.OrganizationServiceImpl] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4) Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) [spring-orm-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:387) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) [spring-tx-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at com.sun.proxy.$Proxy120.save(Unknown Source)
at org.mainco.subco.myproject.service.OrganizationServiceImpl.updateAllOrgRecords(OrganizationServiceImpl.java:282) [pd-60.0.0.jar:]
at org.mainco.subco.myproject.service.OrganizationServiceImpl.updateAllDistricts(OrganizationServiceImpl.java:219) [pd-60.0.0.jar:]
at org.mainco.subco.myproject.service.OrganizationServiceImpl.updateOrgInfo(OrganizationServiceImpl.java:190) [pd-60.0.0.jar:]
at org.mainco.subco.myproject.quartz.ProcessOrgsWorker.work(ProcessOrgsWorker.java:35) [classes:]
at org.mainco.subco.myproject.quartz.ProcessOrgsJob.execute(ProcessOrgsJob.java:17) [classes:]
at org.quartz.core.JobRunShell.run(JobRunShell.java:216) [quartz-1.8.5.jar:]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) [quartz-1.8.5.jar:]
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:90) [hibernate-entitymanager-4.1.0.Final.jar:4.1.0.Final]
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512) [spring-orm-3.1.4.RELEASE.jar:3.1.4.RELEASE]
... 14 more
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [org.mainco.subco.organization.domain.Organization] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=eodbId, rootBeanClass=class org.mainco.subco.organization.domain.Organization, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:184) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1127) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:325) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) [hibernate-core-4.1.0.Final.jar:4.1.0.Final]
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73) [hibernate-entitymanager-4.1.0.Final.jar:4.1.0.Final]
... 15 more
我的问题是如何阻止RollBackExceptions?下面是我在Spring中的配置
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="org.myproject.subco" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
</bean>
<util:map id="jpaPropertyMap">
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.hbm2ddl.auto" value="validate" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<entry key="hibernate.cache.use_second_level_cache" value="true" />
<entry key="hibernate.cache.use_query_cache" value="false" />
<entry key="hibernate.generate_statistics" value="false" />
<entry key="hibernate.connection.zeroDateTimeBehavior" value="convertToNull" />
</util:map>
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jee:jndi-lookup jndi-name="java:jboss/datasources/MySqlDS" id="dataSource" expected-type="javax.sql.DataSource"/>
注意:我的问题不是如何解决我的交易异常。我想构建足够灵活的东西,如果存在提交问题,事情可以正常回滚。