Spring JPA 多数据源
Spring JPA Multiple Datasources
我在这里看到过类似的问题,但未能找到真正适合我的答案。
我有 2 个不同的数据源,每个都有自己的事务管理器和实体管理器工厂。它们每个都在自己的配置中定义 class:
@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;
}
数据源存在于单独的数据源配置中。 'primary' 函数运行良好,'SP2' 函数运行不正常。例如:
@Transactional("sp2TransactionManager")
@Override
public void saveCallHistory(CallHistoryObject callHistoryObject) {
.
.
.
SolutionInfoEntity solutionInfoResult = solutionInfoRepository.save(callHistoryObject.getSolutionInfoEntity());
SolutionInfoEntity save = solutionInfoRepository.findOne(callHistoryObject.getSolutionInfoEntity().getSp2Id());
这里的'save'对象实际上做了return一个结果,但实际上没有任何东西被持久化到数据库中。有任何想法吗?我觉得我几乎什么都试过了。
确保您的存储库位于不同的包中(每个数据源至少 1 个)。在您拥有的配置中
@EnableJpaRepositories(basePackages = {
"com.sprint.cst.data.v8p.repository",
"com.sprint.v8p.repository",
"com.sprint.eib.data" })
每个连接应该只启用那些存储库。
例如,如果数据源 (DS1) 对包进行操作
com.spring.ds1.repository
并且数据源 (DS2) 对包进行操作
com.spring.ds2.repository
JpaRepositories 配置如下所示
在 DS1 配置中
@EnableJpaRepositories(basePackages = {"com.spring.ds1.repository"})
在 DS2 配置中
@EnableJpaRepositories(basePackages = {"com.spring.ds2.repository"})
我在使用 Cassandra 时遇到了类似的问题,通过上述配置,我能够解决它。
我今天 运行 进入同样的情况,发现 spring 提供的 JPARepository 实现 (SimpleJAPRepository.java) 中定义的方法在第二个数据库连接中不起作用。我已经解释了我的发现 http://umeshrsharma.blogspot.in/2016/03/spring-data-jpa-with-multiple-database.html。我的配置可能有误或缺失。但我的基本 JPA 功能仍然无法正常工作,但用户定义功能可以正常工作。如果有人解决了这个问题,请解释一下。
我在这里看到过类似的问题,但未能找到真正适合我的答案。
我有 2 个不同的数据源,每个都有自己的事务管理器和实体管理器工厂。它们每个都在自己的配置中定义 class:
@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;
}
数据源存在于单独的数据源配置中。 'primary' 函数运行良好,'SP2' 函数运行不正常。例如:
@Transactional("sp2TransactionManager")
@Override
public void saveCallHistory(CallHistoryObject callHistoryObject) {
.
.
.
SolutionInfoEntity solutionInfoResult = solutionInfoRepository.save(callHistoryObject.getSolutionInfoEntity());
SolutionInfoEntity save = solutionInfoRepository.findOne(callHistoryObject.getSolutionInfoEntity().getSp2Id());
这里的'save'对象实际上做了return一个结果,但实际上没有任何东西被持久化到数据库中。有任何想法吗?我觉得我几乎什么都试过了。
确保您的存储库位于不同的包中(每个数据源至少 1 个)。在您拥有的配置中
@EnableJpaRepositories(basePackages = {
"com.sprint.cst.data.v8p.repository",
"com.sprint.v8p.repository",
"com.sprint.eib.data" })
每个连接应该只启用那些存储库。
例如,如果数据源 (DS1) 对包进行操作
com.spring.ds1.repository
并且数据源 (DS2) 对包进行操作
com.spring.ds2.repository
JpaRepositories 配置如下所示
在 DS1 配置中
@EnableJpaRepositories(basePackages = {"com.spring.ds1.repository"})
在 DS2 配置中
@EnableJpaRepositories(basePackages = {"com.spring.ds2.repository"})
我在使用 Cassandra 时遇到了类似的问题,通过上述配置,我能够解决它。
我今天 运行 进入同样的情况,发现 spring 提供的 JPARepository 实现 (SimpleJAPRepository.java) 中定义的方法在第二个数据库连接中不起作用。我已经解释了我的发现 http://umeshrsharma.blogspot.in/2016/03/spring-data-jpa-with-multiple-database.html。我的配置可能有误或缺失。但我的基本 JPA 功能仍然无法正常工作,但用户定义功能可以正常工作。如果有人解决了这个问题,请解释一下。