【问题标题】:Spring JPA Transaction Manager does not rollbackSpring JPA 事务管理器不回滚
【发布时间】:2018-12-30 22:44:08
【问题描述】:

我最近从 Hibernate 转到了 JPA。现在我注意到我的代码中有两件事。

  1. 我的服务中的方法不再需要@Transactional 来执行对数据库的插入。

  2. 即使有任何异常,事务也永远不会回滚。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.myapp.service, com.myapp.batch,com.myapp.auth" />
    <!-- Configure the data source bean -->
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">  
      <property name="dataSourceProperties" >
        <props>
            <prop key="url">jdbc:mysql://localhost:3306/app?zeroDateTimeBehavior=convertToNull</prop>
            <prop key="user">user</prop>
            <prop key="password">pass</prop>
        </props>
      </property> 
      <property name="dataSourceClassName"   
                value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />  
    </bean>  

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">  
          <constructor-arg ref="hikariConfig" />  
    </bean>
    <!-- Create default configuration for Hibernate -->
    <bean id="hibernateJpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    <!-- Configure the entity manager factory bean -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.myapp.persist" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.c3p0.timeout">100</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean class="org.springframework.jdbc.datasource.init.DataSourceInitializer"
        depends-on="entityManagerFactory">
        <property name="dataSource" ref="dataSource" />
        <property name="databasePopulator">
            <bean
                class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
                <property name="scripts" value="classpath:initial_data.sql" />
                <property name="continueOnError" value="true" />
            </bean>
        </property>
    </bean>
    <jpa:repositories base-package="com.myapp.repositories"
        entity-manager-factory-ref="entityManagerFactory" />
    <!-- Enable annotation driven transaction management -->
    <tx:annotation-driven />
    <task:annotation-driven />
</beans>

NetworkService.java

@Service
public class AdminUserManagementService {

    @Autowired
    UserRepository userRepository;

    @Autowired
    UserService userService;

    @Autowired
    RequestRegisterHistoryRepository requestRegisterHistoryRepository;

    @Autowired

    @Transactional(rollbackFor=Exception.class)
    public User saveUser(UserBO userBO){
        RequestRegisterHistory requestRegisterHistory = new RequestRegisterHistory();
        User user = new User();
        // ..
        requestRegisterHistoryRepository.save(requestRegisterHistory);
        userRepository.save(user);
        return user;
    }       
}

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="sec://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <context:component-scan base-package="com.myapp.controller" />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

    <mvc:resources mapping="/resources/**" location="resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->


    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
        <beans:property name="order" value="1" />
    </beans:bean>

<beans:bean id="contentNegotiationManager"
             class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <beans:property name="favorPathExtension" value="false" />
    <beans:property name="favorParameter" value="true" />
    <beans:property name="parameterName" value="mediaType" />
    <beans:property name="ignoreAcceptHeader" value="true"/>
    <beans:property name="useJaf" value="false"/>
    <beans:property name="defaultContentType" value="application/json" />

    <beans:property name="mediaTypes">
        <beans:map>
            <beans:entry key="json" value="application/json" />
            <beans:entry key="xml" value="application/xml" />
       </beans:map>
    </beans:property>
</beans:bean>

    <mvc:interceptors>
        <beans:bean
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <beans:property name="paramName" value="lang" />
        </beans:bean>
        <beans:bean class="com.myapp.component.AppInterceptor" />
    </mvc:interceptors>

    <beans:bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
            <beans:property name="supportedMediaTypes" value="image/jpeg" />
    </beans:bean>

    <beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

         <!-- setting maximum upload size -->
        <beans:property name="maxUploadSize" value="99999999999" />
    </beans:bean>
</beans:beans>

我正在使用Spring Version : 4.1.4MySQL Table : MyISAM

堆栈跟踪

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:248)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:214)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy37.save(Unknown Source)
    at com.myapp.service.AdminUserManagementService.saveUser(AdminUserManagementService.java:460)
    at com.myapp.service.AdminUserManagementService$$FastClassBySpringCGLIB$$f21244b7.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)

我该如何配置,所以我需要@Transactional 注释并且任何事务都会在异常期间回滚?

【问题讨论】:

  • 阅读docs.spring.io/spring-data/jpa/docs/1.7.2.RELEASE/reference/…。 Spring-data-jpa 存储库是事务性的。如果您希望事务跨越服务方法调用,只需将该服务方法设置为事务性即可。
  • 当我添加它仍然不会回滚。知道为什么吗?
  • 你在使用 MySQL 你有事务表类型吗?您正在使用组件扫描您是否也有一个具有相同组件扫描的DispatcherServlet?另外,您使用的是哪个 Spring 版本,您的服务是实现接口还是普通类?
  • 您提到存在约束冲突,但对象正在保存。哪个对象。违反什么约束。你能提供更多细节吗?默认情况下,您从哪里调用 saveUser() 作为 Spring 代理类。因此,如果您从同一个类中调用此方法,则事务注释将不起作用,而如果您从另一个类中调用此方法,则事务注释将起作用。您能否详细说明“违反命中约束但仍保存对象”?
  • @Deinum,使用的表是 MyISAM。 DispatcherServlet 仅存在于 web.xml 中。春季版本是 4.1.4。我的服务是一个普通的类。

标签: spring jpa


【解决方案1】:

问题在于MyISAM 的表引擎。将引擎更改为InnoDB 后,事务能够回滚。

hibernate.dialect 设置为org.hibernate.dialect.MySQLInnoDBDialect 确保将来创建带有InnoDB 的表。

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 2013-04-19
    • 1970-01-01
    • 2013-05-08
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多