试图找到我的代码出错的问题,直到我转移到JTAtransactionManager它有问题在数据库中保存记录但是获取记录工作正常,下面是我的示例TransactionConfig类和服务类方法。
@Configuration
@ComponentScan
@EnableTransactionManagement
public class TransactionConfig {
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
//userTransactionImp.setTransactionTimeout(10000);
return userTransactionImp;
}
@Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
public TransactionManager atomikosTransactionManager() throws Throwable {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
return userTransactionManager;
}
@Bean(name = "transactionManager")
@DependsOn({ "userTransaction", "atomikosTransactionManager" })
public PlatformTransactionManager transactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
TransactionManager atomikosTransactionManager = atomikosTransactionManager();
return new JtaTransactionManager(userTransaction, atomikosTransactionManager);
}
}
---Employee Service Class Method---
@Transactional
public void appExample() {
try {
Employee emp = new Employee();
emp.setFirstName("Veer");
emp.setLastName("kumar");
empRepo.save(emp);
} catch (Exception e) {
log.error(e);
}
}
答案 0 :(得分:0)
我认为问题在于empRepo.save()
方法调用。当您使用@Transactional进行事务管理时,此调用不会对数据库进行任何更改。
请尝试使用empRepo.saveAndFlush()
,这会立即将数据刷新到数据库中。您可以参考答案Difference between save and saveAndFlush in Spring data jpa