Java 在Springboot中,当我使用EntityManager合并方法时,服务记录并没有提交到数据库中
这是我的db数据源配置类(我在几个数据库上有连接,所以我有自定义的db配置) 因此,paymentData被存储。我可以在输出中看到结果,但它并没有提交,数据库中也并没有记录。Select语句运行良好,因为它不需要提交。我知道存在一些事务和自动提交问题,但我找不到正确的方法来启用自动提交或手动处理事务Java 在Springboot中,当我使用EntityManager合并方法时,服务记录并没有提交到数据库中,java,spring,jpa,autocommit,Java,Spring,Jpa,Autocommit,这是我的db数据源配置类(我在几个数据库上有连接,所以我有自定义的db配置) 因此,paymentData被存储。我可以在输出中看到结果,但它并没有提交,数据库中也并没有记录。Select语句运行良好,因为它不需要提交。我知道存在一些事务和自动提交问题,但我找不到正确的方法来启用自动提交或手动处理事务 @Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = "com.test.p
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.test.package_name", entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager")
public class DBConfiguration {
@Bean(name = "dataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource ojdbcDataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "db")
@Primary
@Bean(name = "entityManager")
public LocalContainerEntityManagerFactoryBean ojdbcEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(ojdbcDataSource()).persistenceUnit("db").properties(jpaProperties())
.packages("com.test.package_name").build();
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(ojdbcEntityManagerFactory(builder).getObject());
tm.setDataSource(ojdbcDataSource());
return tm;
}
private Map<String, Object> jpaProperties() {
Map<String, Object> props = new HashMap<>();
props.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
props.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
return props;
}
}
@Service
class SomeClass{
@PersistenceContext(unitName = "db", type = PersistenceContextType.EXTENDED)
private EntityManager em;
@Transactional
protected void savePaymentData(PaymentInput input) {
PaymentData paymentData = new PaymentData();
paymentData.setVar1("test1");
paymentData.setVar2("test2");
paymentData = em.merge(paymentData);
System.out.println(paymentData);
}
}