春季启动:两个entityManager使用相同的主数据库

时间:2019-03-12 07:34:13

标签: spring-boot jpa

我正在开发一个需要从两个不同数据库进行事务处理的应用程序。我已经配置了两个entitymanager,并将一个设置为@Primary。 不知何故,这两个事务都击中了@primary数据库,这对我来说很奇怪。 您能建议我的代码有什么错误吗?

主要数据库配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages =    {"com.nbn.firstdb"},entityManagerFactoryRef = "devEntityManager", transactionManagerRef = "devTransactionManager")
public class DevDatabaseConfiguration {

@Autowired
private Environment environment;

@Primary
@Bean(name = "devTransactionManager")
public PlatformTransactionManager anotherTransactionManager(
        @Qualifier("devEntityManager") EntityManagerFactory devEntityManagerFactory) {
    return new JpaTransactionManager(devEntityManagerFactory);
}
@Primary
@Bean(name = "devEntityManager")
public LocalContainerEntityManagerFactoryBean userEntityManager(EntityManagerFactoryBuilder builder,
        @Qualifier("devDataSource") DataSource ds) {
    // Use these properties to let spring work on batch insertion
    Map<String, Object> jpaProperties = new HashMap<>();
    jpaProperties.put("hibernate.jdbc.batch_size", 500);
    jpaProperties.put("hibernate.order_inserts", true);
    jpaProperties.put("hibernate.order_updates", true);

    return builder.dataSource(ds).packages("com.nbn.firstdb").persistenceUnit("devPersistence").properties(jpaProperties)
            .build();
}
@Primary
@Bean(name = "devDataSource")
public DataSource devDataSource() {

    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName(environment.getRequiredProperty("dev.datasource.type"));
    ds.setUrl(environment.getRequiredProperty("dev.datasource.url"));
    ds.setUsername(environment.getRequiredProperty("dev.datasource.username"));
    ds.setPassword(environment.getRequiredProperty("dev.datasource.password"));
    return ds;
}

辅助数据库配置

@Configuration
@EnableJpaRepositories(basePackages = {
    "com.nbn.seconddb" }, entityManagerFactoryRef = "prodEntityManager", transactionManagerRef = "prodTransactionManager")
public class DatabaseConfiguration {

@Autowired
private Environment environment;

@Bean(name = "prodDataSource")
public DataSource dataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName(environment.getRequiredProperty("spring.datasource.driver-class-name"));
    ds.setUrl(environment.getRequiredProperty("spring.datasource.url"));
    ds.setUsername(environment.getRequiredProperty("spring.datasource.username"));
    ds.setPassword(environment.getRequiredProperty("spring.datasource.password"));
    return ds;
}

@Bean(name = "prodTransactionManager")
public PlatformTransactionManager anotherTransactionManager(
        @Qualifier("prodEntityManager") EntityManagerFactory prodEntityManagerFactory) {
    return new JpaTransactionManager(prodEntityManagerFactory);
}

@Bean(name = "prodEntityManager")
public LocalContainerEntityManagerFactoryBean userEntityManager(EntityManagerFactoryBuilder builder,
        @Qualifier("prodDataSource") DataSource ds) {
    // Use these properties to let spring work on batch insertion
    Map<String, Object> jpaProperties = new HashMap<>();
    jpaProperties.put("hibernate.jdbc.batch_size", 500);
    jpaProperties.put("hibernate.order_inserts", true);
    jpaProperties.put("hibernate.order_updates", true);

    return builder.dataSource(ds).packages("com.nbn.seconddb").persistenceUnit("prodPersistence")
            .properties(jpaProperties).build();
}

使用第一个数据库服务的服务:

@Service
public class DevDatabaseService {

@PersistenceContext(name = "devEntityManager")
private EntityManager entityManager;

使用辅助数据库服务的服务:

@Service
public class DatabaseService {

@PersistenceContext(name = "prodEntityManager")
private EntityManager entityManager;

@SuppressWarnings("unchecked")
public void data() {

    System.out.println("prodEntityManager");

这两个服务都命中相同的数据库,即用主要

注释

0 个答案:

没有答案