Spring JPA多个数据源

时间:2016-02-15 21:58:37

标签: spring spring-data spring-data-jpa entitymanager transactional

我在这里看过类似的问题,但未能找到真正对我有用的答案。

我有2个不同的数据源,每个数据源都有自己的事务管理器和实体管理器工厂。它们各自在自己的配置类中定义:

@Configuration
@ComponentScan({
"com.sprint.cst.data.v8p",
"com.sprint.v8p.data",
"com.sprint.v8p.data.util",
"com.sprint.v8p.toptower.repositories",
"com.sprint.v8p.toptower.util"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {
    "com.sprint.cst.data.v8p.repository",
    "com.sprint.v8p.repository",
    "com.sprint.eib.data" })
public class JpaConfiguration {

@Value("${cst.db.generateDdl:true}")
boolean generateDdl;
String ddlGenerationStrategy = "create-or-extend-tables";
@Value("${cst.db.showSql:true}")
boolean showSql;

private String[] defaultEntityPackagesToScan =
        new String[]{
        "com.sprint.v8p.domain",
        "com.sprint.v8p.toptower.domain",
        "com.sprint.cst.data.v8p.entity",
        "com.sprint.eib.entity"};

private JpaDialect jpaDialect = new EclipseLinkJpaDialect();


/// Primary JPA
@Primary
@Autowired
@Bean(name = "entityManagerFactory")
@Qualifier("entityManagerFactory")
public LocalContainerEntityManagerFactoryBean defaultEntityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    //em.setPersistenceUnitName(UnitNames.DefaultPersistenceUnit);
    em.setDataSource(dataSource);
    em.setPackagesToScan(defaultEntityPackagesToScan);
    em.setJpaVendorAdapter(vendorAdapter());
    em.setJpaDialect(jpaDialect);
    em.setJpaProperties(defaultJpaProperties());
    return em;
}

@Primary
@Bean(name="transactionManager")
public PlatformTransactionManager defaultTransactionManager(
        EntityManagerFactory emf) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);

    return transactionManager;
}

//'Datasource' Transaction Manager
//leveraged by ice events functionality; batch inserts/updates
@Autowired
@Bean(name="dsTransactionManager")
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(dataSource);
    return dataSourceTransactionManager;
}


/// Other Beans

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

@Bean
public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor(){
    return new PersistenceAnnotationBeanPostProcessor();
}

并且:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = {
        "com.sprint.cst.data.v8p.sp2.repository"},
    entityManagerFactoryRef="sp2EntityManagerFactory",
    transactionManagerRef="sp2TransactionManager")
public class JpaSp2Configuration {

@Value("${cst.db.generateDdl:true}")
boolean generateDdl;
//@Value("${cst.db.eclipselink.ddl-generation:'create-or-extend-tables'}")
String ddlGenerationStrategy = "create-or-extend-tables";
@Value("${cst.db.showSql:true}")
boolean showSql;

private String[] sp2EntityPackagesToScan =
        new String[]{
        "com.sprint.cst.data.v8p.sp2.entity"};

private JpaDialect jpaDialect = new EclipseLinkJpaDialect();

static final String sp2DataSourceJNDIName = "jdbc/cst-ds-sp2";

/// Secondary JPA
@Bean(name= "sp2EntityManagerFactory")
@Qualifier("sp2EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean sp2EntityManagerFactory(@Qualifier("sp2DataSource") DataSource sp2DataSource) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setBeanName("sp2EntityManagerFactory");
    //em.setPersistenceUnitName(UnitNames.Sp2PersistenceUnit);
    em.setDataSource(sp2DataSource);
    em.setPackagesToScan(sp2EntityPackagesToScan);
    em.setJpaVendorAdapter(vendorAdapter());
    em.setJpaDialect(jpaDialect );
    em.setJpaProperties(defaultJpaProperties());
    return em;
}

@Autowired
@Bean(name="sp2TransactionManager")
@Qualifier("sp2TransactionManager")
public PlatformTransactionManager sp2TransactionManager(
        @Qualifier("sp2EntityManagerFactory") LocalContainerEntityManagerFactoryBean sp2EntityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(sp2EntityManagerFactory.getNativeEntityManagerFactory());
    //transactionManager.setPersistenceUnitName(UnitNames.Sp2PersistenceUnit);
    return transactionManager;
}

数据源存在于单独的数据源配置中。 '主要'功能正常工作,'SP2'功能不正常。例如:

@Transactional("sp2TransactionManager")
@Override
public void saveCallHistory(CallHistoryObject callHistoryObject) {
    .
    .
    .

    SolutionInfoEntity solutionInfoResult = solutionInfoRepository.save(callHistoryObject.getSolutionInfoEntity());
    SolutionInfoEntity save = solutionInfoRepository.findOne(callHistoryObject.getSolutionInfoEntity().getSp2Id());

这里的'save'对象确实会返回一个结果,但实际上DB中没有任何内容。有任何想法吗?我觉得我几乎尝试过所有事情。

2 个答案:

答案 0 :(得分:0)

确保您的存储库位于不同的包中(每个dataSource至少1个)。在你有

的配置中
@EnableJpaRepositories(basePackages = {
"com.sprint.cst.data.v8p.repository",
"com.sprint.v8p.repository",
"com.sprint.eib.data" })

每个连接应该只启用那些存储库。

例如,如果dataSource(DS1)在包

上运行
com.spring.ds1.repository

和dataSource(DS2)在包

上运行
com.spring.ds2.repository

JpaRepositories配置看起来像这样

在DS1配置

@EnableJpaRepositories(basePackages = {"com.spring.ds1.repository"})

在DS2配置中

@EnableJpaRepositories(basePackages = {"com.spring.ds2.repository"})

我遇到了与Cassandra类似的问题,并且通过上述配置,我能够解决它。

答案 1 :(得分:0)

我今天遇到了同样的情况,并发现在Spring提供的JPARepository实现(SimpleJAPRepository.java)中定义的方法,将无法在第二个数据库连接中工作。我已经解释了我的发现http://umeshrsharma.blogspot.in/2016/03/spring-data-jpa-with-multiple-database.html。我可能错误或缺少配置。但仍然我的基本JPA功能不起作用,但用户定义功能正在工作。如果有人解决了这个问题,请解释一下。