在我的服务类中,当抛出异常时,回滚不起作用,出了什么问题?
UserTest类
@Test
public void checkCreatingAdminUser() throws AppException {
String name = "Admin 1";
String username = "admin1";
String password = "Adminuser1";
Assert.assertTrue(AppUtils.matchesPasswordMask(password));
Boolean active = true;
String email = "admin1@ad.ru";
try {
Assert.assertNull(userService.findByUsername(username));
User user = userService.createAdminUser(name, username, password, active, email);
} catch (Exception e) {
// TEST FAILES HERE!
Assert.assertNull(userService.findByUsername(username));
}
}
UserService类
public interface UserService {
User createAdminUser(String name, String username, String password, Boolean active, String email) throws AppException;
User findByUsername(String username);
}
实施服务类
@Override
public User createAdminUser(String name, String username, String password, Boolean active, String email) throws AppException {
if (findByUsername(username) != null) {
throw new AppException(AppError.USER_EXISTS_BY_USERNAME);
}
try {
return createAdminUserTr(name, username, password, active, email);
} catch (Exception e) {
logger.error("Error on create admin group", e);
throw new AppException(AppError.DB_INTERNAL_ERROR);
}
}
@Transactional
public User createAdminUserTr(String name, String username, String password, Boolean active, String email) {
User user = new User();
user.setName(name);
user.setUsername(username);
user.setPassword(hashPassword(password));
user.setActive(active);
user.setEmail(email);
user.setCreateDate(new Date());
userRepository.save(user);
throw new RuntimeException("check rollback test");
}
我认为问题出在我的配置中,但我没有找到它
<?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:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.hsqldb.jdbcDriver"/>
<property name="jdbcUrl" value="jdbc:hsqldb:mem:mymemdb"/>
<property name="user" value="sa"/>
<property name="password" value=""/>
<property name="acquireIncrement" value="1"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="maxPoolSize" value="3"/>
<property name="maxStatements" value="0"/>
<property name="minPoolSize" value="1"/>
<property name="autoCommitOnClose" value="false"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="MAR-TEST-PU"/>
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="my.app.models"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<jpa:repositories
base-package="my.app.repositories"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"
/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<context:annotation-config/>
<context:component-scan base-package="my.app"/>
</beans>
感谢您的帮助!