0002.Spring data jpa 测试时事务回滚了,但是数据还是插入到了表中

问题简介

使用Spring data jpa + mysql对Dao层的save()方法进行单元测试时,期望在测试完后进行回滚操作,结果未回滚,去表里面查看,数据却成功插入。而Log日志显示已经进行了回滚操作。日志如下:

2017-05-03 01:44:00 [DEBUG] org.hibernate.engine.transaction.internal.TransactionImpl | rolling back

问题出现时配置:

spring-context.xml




    
    
        
        
            
                classpath*:*.properties
            
        
    

    
    
    
    


spring-jdbc.xml




    
    

    
    

    
    
        
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
        
        
        
        
            
        
        
        
        
            
                ${hibernate.dialect}
                ${hibernate.hbm2ddl.auto}
                ${hibernate.show_sql}
                ${hibernate.format_sql}
                ${hibernate.use_sql_comments}
                org.hibernate.cfg.ImprovedNamingStrategy
            
        
    


jdbc.properties

# +++++++++++++++++++++
# jdbc properties
# +++++++++++++++++++++
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/auth_db?createDatabaseIfNotExist=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=123456

# +++++++++++++++++++++
# hibernate properties
# +++++++++++++++++++++
# 方言
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.use_sql_comments=false

# +++++++++++++++++++++
# druid properties
# +++++++++++++++++++++
# 配置初始化连接池大小、最大并发数量、最小空闲数量
druid.initialSize=1
druid.maxActive=20
druid.minIdle=1

# 配置获取连接等待超时的时间
druid.maxWait=60000

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
druid.timeBetweenEvictionRunsMillis=60000

# 配置一个连接在池中最小生存的时间,单位是毫秒
druid.minEvictableIdleTimeMillis=300000

druid.validationQuery=SELECT 'x'
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false

# 打开PSCache,并且指定每个连接上PSCache的大小
# 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false
druid.poolPreparedStatements=false
druid.maxPoolPreparedStatementPerConnectionSize=20

测试 AuthGroupRepositoryTest.java

package com.airkisser.auth.dao;

import com.airkisser.auth.dao.auth.AuthGroupRepository;
import com.airkisser.auth.model.auth.AuthGroup;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import java.util.Date;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@ContextConfiguration(locations = {"classpath:spring-context.xml"})
public class AuthGroupRepositoryTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private AuthGroupRepository authGroupRepository;

    @Test
    public void testAdd() {
        AuthGroup group = new AuthGroup();
        group.setGroupName("TestAdmin");
        group.setDescription("Desc");
        group.setCreateDate(new Date());
        authGroupRepository.save(group);
        AuthGroup g = authGroupRepository.findOne(group.getId());
        assertNotNull(g);
        assertEquals(group.getGroupName(), g.getGroupName());
    }

}

解决问题

将hibernate.dialect配置成hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect,指定MYSQL建表的时候使用InnoDB引擎(支持事务安全),使用MySQL5InnoDBDialect方言在生成表结构的时候可以指定表的"ENGINE=InnoDB",而如果使用方言MySQL5Dialect,则在生成表结构的时候默认使用的是 ENGINE=MyISAM,该引擎不支持事务,即使程序中使用事务,也不起作用

你可能感兴趣的:(0002.Spring data jpa 测试时事务回滚了,但是数据还是插入到了表中)