Spring Boot事务回滚

时间:2018-05-25 18:23:44

标签: java hibernate spring-boot jpa transactional

我尝试切换到Spring Boot(v2.0.1.RELEASE)和EntityManager。我花了一个星期的时间来处理MySQL数据库回滚异常,但仍然无法弄明白。

@Repository
public class HibernateDaoImp implements Dao {
    @PersistenceContext 
    private EntityManager entityManager;

    public <T extends AbstractEntity> T saveOrUpdate(T entity) {
        if(entity.getId() == null || entity.getId().equals(0)) {
            this.entityManager.persist(entity);
            t = entity;
        } else {
            t = (T) this.entityManager.merge(entity);
        }
        return t;
    } 
}

@Service("userService")
public class UserServiceImp implements UserService {
    @Autowired
    protected Dao dao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {ServiceException.class})
    public User saveUser(User user) throws ServiceException {
        user = this.dao.saveOrUpdate(user);
        throw new ServiceException(500, "internal error");
    }
}

用户仍保存在数据库中。这是loggin:

  

2018-05-25 10:36:46.297 TRACE 25041 --- [nio-8080-exec-4]   .s.t.s.TransactionSynchronizationManager:绑定值   [org.springframework.orm.jpa.EntityManagerHolder@4c6ea9e2]用于密钥   [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@3dd8ed71]   线程[http-nio-8080-exec-4] 2018-05-25 10:36:46.356 DEBUG 25041   --- [nio-8080-exec-4] c.s.knected.controller.UserController:save id = null 2018-05-25 10:36:46.357 TRACE 25041 --- [nio-8080-exec-4]   .s.t.s.TransactionSynchronizationManager:检索值   [org.springframework.orm.jpa.EntityManagerHolder@4c6ea9e2]用于密钥   [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@3dd8ed71]   绑定线程[http-nio-8080-exec-4] 2018-05-25 10:36:46.358 TRACE   25041 --- [nio-8080-exec-4] .s.t.s.TransactionSynchronizationManager:   约束值   [org.springframework.jdbc.datasource.ConnectionHolder@5b4dc571] for   密钥[HikariDataSource(HikariPool-1)]来线程[http-nio-8080-exec-4]   2018-05-25 10:36:46.358 TRACE 25041 --- [nio-8080-exec-4]   .s.t.s.TransactionSynchronizationManager:初始化事务   同步2018-05-25 10:36:46.358 TRACE 25041 ---   [nio-8080-exec-4] o.s.t.i.TransactionInterceptor:获取   交易   [c.s.k.service.UserServiceImp.saveUser] 2018-05-25   10:36:46.358 TRACE 25041 --- [nio-8080-exec-4]   .s.t.s.TransactionSynchronizationManager:检索值   [org.springframework.orm.jpa.EntityManagerHolder@4c6ea9e2]用于密钥   [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@3dd8ed71]   绑定到线程[http-nio-8080-exec-4] Hibernate:       插入       到           用户           (created_by,time_created,deleted,deleted_by,time_deleted,name,time_updated,updated_by,adress,code,email,password,   first_name,lost_login,last_name,mobile,phone,photo_id,   time_registered,user_type)       值           (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'a')2018-05-25 10: 36:46.457 TRACE 25041 --- [nio-8080-exec-4]   o.s.t.i.TransactionInterceptor:完成交易   [c.s.k.service.UserServiceImp.saveInvite]之后   exception:c.s.k.service.ServiceException 2018-05-25   10:36:46.457 TRACE 25041 --- [nio-8080-exec-4]   o.s.t.i.RuleBasedTransactionAttribute:应用规则来确定   是否应该回滚事务   c.s.k.service.ServiceException 2018-05-25   10:36:46.457 TRACE 25041 --- [nio-8080-exec-4]   o.s.t.i.RuleBasedTransactionAttribute:获胜回滚规则是:   RollbackRuleAttribute与模式   [c.s.k.service.ServiceException] 2018-05-25   10:36:46.460 TRACE 25041 --- [nio-8080-exec-4]   .s.t.s.TransactionSynchronizationManager:清算交易   同步2018-05-25 10:36:46.460 TRACE 25041 ---   [nio-8080-exec-4] .s.t.s.TransactionSynchronizationManager:已删除   价值[org.springframework.jdbc.datasource.ConnectionHolder@5b4dc571]   来自线程的密钥[HikariDataSource(HikariPool-1)]   [http-nio-8080-exec-4] 2018-05-25 10:36:46.468 TRACE 25041 ---   [nio-8080-exec-4] .s.t.s.TransactionSynchronizationManager:已删除   值[org.springframework.orm.jpa.EntityManagerHolder@4c6ea9e2]   键   [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@3dd8ed71]   来自线程[http-nio-8080-exec-4]

我还注意到,如果我将@Transactional for Required更改为强制:

@Transactional(propagation = Propagation.MANDATORY, rollbackFor = {ServiceException.class})

我收到以下错误:

  

org.springframework.transaction.IllegalTransactionStateException:没有   为标记为传播的事务找到的现有事务   '强制'

以下是我的pom:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

任何帮助将不胜感激!

-ZJ

这是我在application.properties中的数据源设置:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mytestdb?useSSL=false
spring.datasource.username=abc
spring.datasource.password=abc123

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.maximum-pool-size=50
spring.datasource.hikari.idle-timeout=1000
spring.datasource.hikari.pool-name=knected-pool

#spring.datasource.tomcat.max-wait=20000
#spring.datasource.tomcat.max-active=50
#spring.datasource.tomcat.max-idle=20
#spring.datasource.tomcat.min-idle=15

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false

1 个答案:

答案 0 :(得分:1)

根据@Kayaman的建议缩小到基础连接后,我发现Springboot 2.0 @Transaction doesn't get supported by org.hibernate.dialect.MySQL5Dialect。我更改为MySQL5InnoDBDialect并重新创建表,并且回滚按预期工作!

我应该首先发布我的数据源配置。

非常感谢@Kayaman和@Sundararaj Govindasamy的帮助!