代码如下:
/**
* 内部用户管理器
*/
@Service
public class InnerUserManager {
/**
* 系统内置管理员用户
*/
private static final User SUPER_ADMIN = new User(SUPER_ADMIN_USER_UUID, "super_admin", "超级管理员", UserCategory.SUPER_ADMIN.getId(), null);
private final UserRepository userRepository;
private final PasswordHandler passwordHandler;
private final UserPasswordRepository passwordRepository;
/**
* @param userRepository
* @param passwordHandler
* @param passwordRepository
*/
@Autowired
public InnerUserManager(UserRepository userRepository,
PasswordHandler passwordHandler,
UserPasswordRepository passwordRepository) {
this.userRepository = userRepository;
this.passwordHandler = passwordHandler;
this.passwordRepository = passwordRepository;
}
/**
* 初始化内部用户到数据库
*/
@PostConstruct
@Transactional(rollbackOn = Throwable.class)
private void initInnerUser() {
//如果数据库中未初始化过超级管理员,则初始化
if (InnerUserManager.this.userRepository.findByLoginName(SUPER_ADMIN.getLoginName()) == null) {
// 初始化超级管理员创建时间
SUPER_ADMIN.setCreateTime(System.currentTimeMillis());
User superAdmin = InnerUserManager.this.userRepository.save(SUPER_ADMIN);
// 初始化超级管理员密码
UserPassword password = new UserPassword();
password.setUserId(superAdmin.getId());
password.setPassword(InnerUserManager.this.passwordHandler.toEncryptedPassword(SUPER_ADMIN.getUuid(), SUPER_ADMIN_USER_DEFAULT_PASSWORD));
password.setCreateTime(System.currentTimeMillis());
InnerUserManager.this.passwordRepository.save(password);
throw new UserExistedException("aaa");//此处故意抛出异常
}
}
}
}
在故意抛出异常之后,前面两次的插入并没有回滚!
解决办法:
/**
* 内部用户管理器
*/
@Service
public class InnerUserManager {
/**
* 系统内置管理员用户
*/
private static final User SUPER_ADMIN = new User(SUPER_ADMIN_USER_UUID, "super_admin", "超级管理员", UserCategory.SUPER_ADMIN.getId(), null);
private final UserRepository userRepository;
private final PasswordHandler passwordHandler;
private final UserPasswordRepository passwordRepository;
private final PlatformTransactionManager txManager;
/**
* @param userRepository
* @param passwordHandler
* @param passwordRepository
* @param txManager
*/
@Autowired
public InnerUserManager(UserRepository userRepository,
PasswordHandler passwordHandler,
UserPasswordRepository passwordRepository,
PlatformTransactionManager txManager) {
this.userRepository = userRepository;
this.passwordHandler = passwordHandler;
this.passwordRepository = passwordRepository;
this.txManager = txManager;
}
/**
* 初始化内部用户到数据库
*/
@PostConstruct
private void initInnerUser() {
try {
TransactionTemplate tmpl = new TransactionTemplate(txManager);
tmpl.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
//如果数据库中未初始化过超级管理员,则初始化
if (InnerUserManager.this.userRepository.findByLoginName(SUPER_ADMIN.getLoginName()) == null) {
// 初始化超级管理员创建时间
SUPER_ADMIN.setCreateTime(System.currentTimeMillis());
User superAdmin = InnerUserManager.this.userRepository.save(SUPER_ADMIN);
// 初始化超级管理员密码
UserPassword password = new UserPassword();
password.setUserId(superAdmin.getId());
password.setPassword(InnerUserManager.this.passwordHandler.toEncryptedPassword(SUPER_ADMIN.getUuid(), SUPER_ADMIN_USER_DEFAULT_PASSWORD));
password.setCreateTime(System.currentTimeMillis());
InnerUserManager.this.passwordRepository.save(password);
throw new UserExistedException("aaa");
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}