configure Spring+Hibernate+JPA for work with two databases
来源:互联网 发布:c 调用tensorflow 编辑:程序博客网 时间:2024/06/05 05:04
第一方案
my datasource-context.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util">
<!--
Data Source config
-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${local.jdbc.driver}" p:url="${local.jdbc.url}"
p:username="${local.jdbc.username}" p:password="${local.jdbc.password}">
</bean>
<bean id="dataSourceRemote" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${remote.jdbc.driver}"
p:url="${remote.jdbc.url}" p:username="${remote.jdbc.username}"
p:password="${remote.jdbc.password}" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entity-manager-factory-ref="entityManagerFactory" />
<!--
JPA config
-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list value-type="java.lang.String">
<value>classpath*:config/persistence.local.xml</value>
<value>classpath*:config/persistence.remote.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="localDataSource" value-ref="dataSource" />
<entry key="remoteDataSource" value-ref="dataSourceRemote" />
</map>
</property>
<property name="defaultDataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true" p:generateDdl="true">
</bean>
</property>
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
each persistence.xml contains one unit, like this:
<persistence-unit name="remote" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
<property name="hibernate.dialect" value="${remote.hibernate.dialect}" />
<property name="hibernate.hbm2ddl.auto" value="${remote.hibernate.hbm2ddl.auto}" />
</properties>
</persistence-unit>
1.
In your Spring context:
<context:annotation-config />
2.
In your Spring-managed DAOs (note the unitName property):
@PersistenceContext(unitName = "pu1"`) protected EntityManager entityManager;
This way a properly intantiated EntityManager corresponding to the persistence unit named "pu1" would be injected in the corresponding DAOs.
第二方案
If you follow this tutorial, http://javacodegeeks.blogspot.com/2010/05/jboss-42x-spring-3-jpa-hibernate.html you can make the following changes to access two different databases:
1. persistence.xml, define a second pesristence-unit for your second database.
2. spring.xml, define a second entityManagerFactory bean under a different name, lets say "entityManagerFactoryDB2" and configure it to use the persistent unit for the second database.
3.
for every DAO you want to access the second database include the following :
@Autowired
private EntityManagerFactory entityManagerFactoryDB2;
@PostConstruct
public void init() {
super.setEntityManagerFactory(entityManagerFactoryDB2);
}
Thats all!
On spring service classes, use the DAOs as usual!
- configure Spring+Hibernate+JPA for work with two databases
- Spring JPA – Multiple Databases
- Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos
- Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos
- Flex 2.0 @ Work in combination with Spring and Hibernate
- Spring Data repositories with multiple databases
- SPRING JPA 与 Hibernate JPA
- struts2+spring+jpa(hibernate)
- jpa+spring+hibernate
- 11.Spring + Hibernate + JPA
- Spring集成JPA(Hibernate)
- Spring + JPA + Hibernate配置
- Spring Boot JPA-Hibernate
- spring+hibernate+jpa配置
- hibernate(jpa)+spring
- spring + jpa + hibernate
- configure Spring Data JPA by using XML
- How to configure IPMI to work with Oracle RAC
- [YC] Symbian S60位图处理那些事
- Symbian/S60:删除指针后,在何种情形下才需要置空(ptr = NULL)
- Symbian周期函数(CPeriodic)(
- [代码]使用LINQ的查询结果填充XML树(LINQ to XML)
- Symbian - CEikRichTextEditor 陷阱
- configure Spring+Hibernate+JPA for work with two databases
- Symbian S60系列应用的全屏处理
- How to use CPeriodic for a steady stream of ticks
- [代码]使用函数构造创建具有属性的元素(LINQ to XML)
- 麦考林
- JS 打开新窗口没有工具栏 地址栏,同时关闭当前窗口
- [代码]创建XDocument对象(LINQ to XML)
- zz iccr做覆盖率分析
- mysql表类型MyISAM和InnoDB区别