我有一个指向两个不同数据库的应用程序,我已经按照此处发布的示例创建了两个JPAConfig(在2处使用JAVA进行JPA配置)
http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/#noxml
在我的持久层中,我使用anotations和代码就像这个persistor接口
public interface EmployeePersistor {
public void create(EmployeeEntity employee);
}
实施
@Component
public class EnvelopeStatusPersistorImpl implements EnvelopeStatusPersistor {
@PersistenceContext(unitName = "db2EntityManagerFactory")
EntityManager db2EM;
@Override
@Transactional("db2TransactionManager")
public void create(EmployeeEntity employee) {
db2EM.persist(employee);
db2EM.flush();
}
}
这适用于db2事务,但是当我尝试使用db1执行类似的方法时,查询仍在针对db2运行
@PersistenceContext(unitName = "db1EntityManagerFactory")
EntityManager db1EM;
@Override
@Transactional("db1TransactionManager")
public void create(EmployeeEntity employee) {
db1EM.persist(employee);
db1EM.flush();
}
下面列出的是仅用于db1的JPAConfig文件
@Configuration
@PropertySource("classpath:application.${target_env}.properties")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.application", entityManagerFactoryRef = "db1EntityManagerFactory", transactionManagerRef = "db1TransactionManager")
public class CRMJPAConfig {
@Value("${db1.url}")
private String db1URL;
@Value("${db1.username}")
private String db1User;
@Value("${db1.password}")
private String db1Password;
@Value("${db1.driver}")
private String db1DriverClass;
@Value("${db1.scan}")
private String db1PackageScan;
@Value("${db1.dialect}")
private String db1HibernateDialect;
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { db1PackageScan });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
@Primary
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(db1DriverClass);
dataSource.setUrl(db1URL);
dataSource.setUsername(db1User);
dataSource.setPassword(db1Password);
return dataSource;
}
@Bean
@Primary
public PlatformTransactionManager db1TransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(db1EntityManagerFactory()
.getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", db1HibernateDialect);
return properties;
}
}
答案 0 :(得分:0)
这应该可以解决问题...
在applicationcontext-jdbc.xml或类似用途的配置文件中 你应该有类似的情况
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:orcl="http://www.springframework.org/schema/data/orcl"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/data/orcl
http://www.springframework.org/schema/data/orcl/spring-data-orcl-1.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Used to enable transaction annotation on the DBItaServiceImpl class -->
<!-- <bean id="cerempService" class="eu.europa.acer.aris.ceremp.service.impl.CerempServiceImpk" /> -->
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven />
<!-- Caching configuration -->
<cache:annotation-driven />
<!-- ehcache Cache Manager -->
<bean class="org.springframework.cache.ehcache.EhCacheCacheManager" id="cacheManager" p:cacheManager-ref="ehcache"/>
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" p:shared="true" />
<aop:aspectj-autoproxy/>
<!-- AOP used to debug spring-jpa -->
<bean class="org.springframework.aop.interceptor.CustomizableTraceInterceptor"
id="customizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])" />
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]" />
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor"
pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))" />
</aop:config>
<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="eu.europa.acer.aris.ceremp.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
<jpa:repositories base-package="eu.europa.acer.aris.ceremp.iamrepository" entity-manager-factory-ref="entityManagerFactory2" transaction-manager-ref="transactionManager2"/>
<!-- <util:properties id="hibernatePropertiesProps">
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key = "hibernate.show_sql">true </prop>
<prop key="org.hibernate.envers.auditTablePrefix">AUD_</prop>
<prop key="org.hibernate.envers.auditTableSuffix"></prop>
<prop key="org.hibernate.envers.store_data_at_delete">true</prop>
</util:properties> -->
<util:properties id="hibernatePropertiesProps">
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="log4j.logger.org.hibernate.SQL">ALL</prop>
<prop key="log4j.logger.org.hibernate.type">ALL</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="org.hibernate.envers.auditTablePrefix">AUD_</prop>
<prop key="org.hibernate.envers.auditTableSuffix"></prop>
<prop key="org.hibernate.envers.store_data_at_delete">true</prop>
</util:properties>
<!-- flyway part -->
<beans profile="ci,local,dev,qa,test,smarts,prototype,pilot,testframework,testframework-lutech">
<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate" depends-on="dataSource">
<property name="dataSource" ref="dataSource"/>
<property name="disableInitCheck" value="true" />
<property name="encoding" value="UTF-8"></property>
<property name="placeholders" ref="flyway.prop"/>
</bean>
</beans>
<beans profile="staging,preprod,prod">
<bean id="flyway" class="com.googlecode.flyway.core.Flyway">
<property name="placeholders" ref="flyway.prop"/>
</bean>
</beans>
<!-- Declare a JPA entityManagerFactory -->
<beans profile="ci,local,dev,qa,test,smarts,prototype,pilot,testframework,testframework-lutech">
<bean id="entityManagerFactory" depends-on="flyway"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"
p:jpaVendorAdapter-ref="hibernateVendor"
p:jpaProperties-ref="hibernatePropertiesProps"
p:dataSource-ref="dataSource"
p:persistenceUnitName="persistenceUnitDCI"
p:packagesToScan= "eu.europa.acer.aris.ceremp.domain,eu.europa.acer.aris.ceremp.auditing,eu.europa.acer.aris.ceremp.domain.readonly" >
<qualifier value ="emfDCI" />
</bean>
<bean id="entityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"
p:jpaVendorAdapter-ref="hibernateVendor"
p:jpaProperties-ref="hibernatePropertiesProps"
p:dataSource-ref="dataAltSource"
p:persistenceUnitName="persistenceUnitIAM"
p:packagesToScan= "eu.europa.acer.aris.ceremp.domain,eu.europa.acer.aris.ceremp.auditing,eu.europa.acer.aris.ceremp.domain.readonly" >
<qualifier value ="emfIAM" />
</bean>
</beans>
<beans profile="staging,preprod,prod">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"
p:jpaVendorAdapter-ref="hibernateVendor"
p:jpaProperties-ref="hibernatePropertiesProps"
p:dataSource-ref="dataSource"
p:persistenceUnitName="persistenceUnitDCI"
p:packagesToScan= "eu.europa.acer.aris.ceremp.domain,eu.europa.acer.aris.ceremp.auditing,eu.europa.acer.aris.ceremp.domain.readonly" >
<qualifier value ="emfDCI" />
</bean>
<bean id="entityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"
p:jpaVendorAdapter-ref="hibernateVendor"
p:jpaProperties-ref="hibernatePropertiesProps"
p:dataSource-ref="dataAltSource"
p:persistenceUnitName="persistenceUnitIAM"
p:packagesToScan= "eu.europa.acer.aris.ceremp.domain,eu.europa.acer.aris.ceremp.auditing,eu.europa.acer.aris.ceremp.domain.readonly" >
<qualifier value ="emfIAM" />
</bean>
</beans>
<beans>
<!-- Specify our ORM vendor -->
<bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="false"/>
<!-- Declare a transaction manager-->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"
p:dataSource-ref="dataSource"/>
<!-- Declare a transaction manager-->
<bean id="transactionManager2"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory2"
p:dataSource-ref="dataAltSource"/>
</beans>
<!-- Define the oracle pooling datasource of spring-data which use the class oracle.jdbc.pool.OracleDataSource -->
<beans profile="local">
<orcl:pooling-datasource id="dataSource"
url="jdbc:oracle:thin:@db00-ccea.labucs.int:1521:CCEA" username="${db.credential.username}" password="${db.credential.password}"/>
<util:properties id="flyway.prop" location="classpath:config_local.properties"/>
<orcl:pooling-datasource id="dataAltSource"
url="jdbc:oracle:thin:@db00-ccea.labucs.int:1521:CCEA" username="${db2.credential.username}" password="${db2.credential.password}"/>
<util:properties id="flyway.prop" location="classpath:config_local.properties"/>
</beans>
</beans>
现在......你应该有一个该对象的Repository&amp; RepositoryImpl类......在repositoryimpl类的引用中有这样的东西:
package eu.europa.acer.aris.ceremp.iamrepository.impl;
public class UserRepositoryImpl implements UserRepositoryCustom
{
@Qualifier("emfIAM")
@Autowired
private EntityManagerFactory em;
}
基本上你必须自动装配一个entitymanagerfactory,并通过限定符指定你为特定实体配置的qualifer对于你想要用于该特定对象的连接:) 你还有其他选择,但这是最简单的选择,尽管你也可以考虑另一个,比如定义一个属性来定义哪个entitymanager将用于处理同一个类的两个对象,例如你有一个客户与类别黄金的客户存在于一个模式和类别银的客户在一个不同的模式... ...
希望这会有所帮助:)
答案 1 :(得分:0)
修复了问题,在第一个文件中将数据源更改为db1DataSource,在第二个文件中将数据源更改为db2DataSource