问题简介
使用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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--PropertyConfigurer-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:*.properties</value>
</list>
</property>
</bean>
<import resource="classpath:spring-jdbc.xml"/>
<!--<import resource="classpath:spring-aop.xml"/>-->
<!--<import resource="classpath:spring-shiro.xml"/>-->
<!--<import resource="classpath:spring-mvc.xml"/>-->
</beans>
spring-jdbc.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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 扫描DAO interface包,自动完成DAO注入 添加事务管理 -->
<jpa:repositories base-package="com.airkisser.auth.dao" transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" repository-impl-postfix="Impl"/>
<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 事务管理器 使用JPA事务管理-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid.initialSize}"/>
<property name="minIdle" value="${druid.minIdle}"/>
<property name="maxActive" value="${druid.maxActive}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${druid.validationQuery}"/>
<property name="testWhileIdle" value="${druid.testWhileIdle}"/>
<property name="testOnBorrow" value="${druid.testOnBorrow}"/>
<property name="testOnReturn" value="${druid.testOnReturn}"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}"/>
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${druid.maxPoolPreparedStatementPerConnectionSize}"/>
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定Jpa持久化实现厂商类,这里以Hibernate为例 -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<!-- 指定自动扫描的注解实体类包 -->
<property name="packagesToScan" value="com.airkisser.auth.model"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
</bean>
</beans>
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,该引擎不支持事务,即使程序中使用事务,也不起作用
网友评论