(交易没有回滚)Spring-data,JTA,JPA,Wildfly10

时间:2017-01-17 23:54:17

标签: hibernate jpa spring-data-jpa wildfly jta

我有一段时间没遇到这个问题,无法弄清楚我做错了什么。以下是上下文:

  • Spring 4.3.4.RELEASE
  • Spring-data-jpa 1.10.6.RELEASE
  • Hibernate 5.2.5.Final
  • 我使用wildfly 10,从maven插件开始
  • 为JNDI正确查找配置的数据源
  • 在接口上使用带有@Repository的spring-data
  • 创建了entityManagerFactory作为spring bean(LocalContainerEntityManagerFactoryBean)
  • 至于交易经理,我试图同时使用' tx:jta-transaction-manager'和spring bean(JtaTransactionManager)
  • 配置了persistence.xml
  • 启用' tx:注释驱动'

所以,我得到了一个@Service类(SistemaBOImpl),它注入了一个@Repository(SistemaDAO)和一个用于事务行为的测试方法:

    @Service
@Transactional(propagation = Propagation.SUPPORTS)
public class SistemaBOImpl extends AbstractBO<Sistema, Long> implements SistemaBO {

    @Autowired
    public SistemaBOImpl(SistemaDAO sistemaDAO) {
        super(sistemaDAO);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { BusinessException.class })
    public void testTrans() throws BusinessException {
        try {
            final Sistema s = findOne(1L);
            s.setCodUsuAlt(SimpleDateFormat.getInstance().format(new Date()));
            s.setDatAlt(new Date());
            save(s); //1st save

            save(new Sistema()); //2nd save
        } catch (Exception e) {
            throw new RuntimeException(e);
            // throw new BusinessException(e);
        }
    }
}

DAO

@Repository
@Transactional
public interface SistemaDAO extends DAO<Sistema, Long>, JpaRepository<Sistema, Long> {}

问题是: 虽然由于数据库的限制,第二次保存不会完成(我是故意这样做的,第一次保存被提交,数据被更改。 我无法弄清楚为什么它不会全部回滚。 我做错了什么?!?

以下是我的其他配置文件:

的applicationContext.xml

<beans ... ommited namespace hell>
<context:annotation-config />

<context:component-scan base-package="br.com.myco" />

<tx:jta-transaction-manager />

<tx:annotation-driven transaction-manager="transactionManager" />

<jpa:repositories base-package="br.com.myco" />

<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/Myco_DB" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jtaDataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" >
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
            <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="tof"/>
    <property name="packagesToScan" value="br.com.myco.tof"/>
</bean>

的persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
<persistence-unit name="myco" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/Myco_DB</jta-data-source>
    <class>br.com.abril.tof.domain.Sistema</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
    </properties>
</persistence-unit>

的web.xml

<web-app version="3.1" ...>

<display-name>MyCO</display-name>
<description>MyCO</description>

<!-- Carrega Spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>myco</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myco</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<persistence-unit-ref>
    <persistence-unit-ref-name>jpa/EntityManager</persistence-unit-ref-name>
    <persistence-unit-name>myco</persistence-unit-name>
</persistence-unit-ref>

<session-config>
    <session-timeout>60</session-timeout>
</session-config>
</web-app>

第二次保存的日志/堆栈跟踪

Caused by: org.hibernate.HibernateException: Could not apply work
at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.doTheWorkInNewTransaction(JtaIsolationDelegate.java:112)
at org.hibernate.resource.transaction.backend.jta.internal.JtaIsolationDelegate.delegateWork(JtaIsolationDelegate.java:65)
at org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:412)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
at com.sun.proxy.$Proxy63.persist(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy63.persist(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:506)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
**at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)**
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 65 more

2 个答案:

答案 0 :(得分:0)

检查您使用的MySQL引擎是否是InnoDB。如果您使用的是MyISAM,则不支持交易。

答案 1 :(得分:0)

得到了人们!

经过大量调试和搜索事务配置的缺陷后,我最终看到我的所有业务都不是代理。好吧,那说我认为应该是春天配置我的豆子问题,为了上帝的缘故:),是它。 我得到了这个spring-mvc配置错误(WEB-INF / myco-servlet.xml):

<context:component-scan base-package="br.com.myco" />
<mvc:annotation-driven />

请参阅 base-package ?它指向与我的applicationContext.xml中相同的包:

<context:component-scan base-package="br.com.myco" />

我只需将myco-servlet.xml中的配置更改为主mvc包:

<context:component-scan base-package="br.com.myco.service" />

工作得很好!

对此question on stackoverflow的赞誉也是对此问题的启示。