Spring3+Jpa 配置多个数据源的解决方案(基于注解)

补充说明:

不同的应用场景,解决方案也不同。

例如下文的配置实例,是创建了两个事务管理器。

如果项目要求必须只能存在一个事务管理,解决方案可参考:http://today.java.net/pub/a/today/2006/08/31/jotm-transactions-in-spring-and-hibernate.html



----------------配置文件

-------------------------------------META-INF/persistence.xml-------------------------------------------------------------

01 xml version="1.0" encoding="UTF-8"?>
02 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
03              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
05              version="1.0">
06  
07     
08     <persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">
09         <provider>org.hibernate.ejb.HibernatePersistenceprovider>
10         <non-jta-data-source>mysqlDataSourcenon-jta-data-source>
11        
12         <class>com.sunhope.model.core.ExternalEnvironmentclass>  
13         
14         <properties>
15             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
16             <property name="hibernate.archive.autodetection" value="false"/>
17             <property name="hibernate.transaction.auto_close_session" value="false"/>
18             <property name="hibernate.hbm2ddl.auto" value="update" />
19         properties>
20     persistence-unit>
21     
22     <persistence-unit name="postgre" transaction-type="RESOURCE_LOCAL">
23         <provider>org.hibernate.ejb.HibernatePersistenceprovider>
24         
25         <non-jta-data-source>postgreDataSourcenon-jta-data-source>
26  
27         <class>com.sunhope.model.baseinfo.BdStorageCompanyclass>
28         <class>com.sunhope.model.baseinfo.BdHouseTypeclass>
29         <class>com.sunhope.model.baseinfo.BdStorageclass>
30         <class>com.sunhope.model.core.StoreEnvironmentclass>
31         
32         <properties>
33             <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
34             <property name="hibernate.archive.autodetection" value="false"/>
35             <property name="hibernate.transaction.auto_close_session" value="false"/>
36             <property name="hibernate.hbm2ddl.auto" value="update" />
37         properties>
38     persistence-unit>
39     
40 persistence>

-------------------------------------applicationContext.xml-------------------------------------------------------------

001 xml version="1.0" encoding="UTF-8"?>
002 <beans xmlns="http://www.springframework.org/schema/beans"
003     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
004     xmlns:tx="http://www.springframework.org/schema/tx"
005     xsi:schemaLocation="http://www.springframework.org/schema/beans
006             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
007             http://www.springframework.org/schema/tx
008             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
009              
010     <bean id="defaultPersistenceUnitManager"
011         class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
012         <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
013         
014         <property name="dataSourceLookup">
015             <bean
016                 class="org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup" />
017         property>
018     bean>
019      
020     
021     <tx:annotation-driven transaction-manager="postgreEntityManagerFactory"/>
022      
023     <bean id="postgreJpaVendor"
024         class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
025          <property name="showSql" value="true" />
026          <property name="generateDdl" value="true" />
027     bean>      
028      
029     <bean id="postgreDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
030        <property name="driverClassName" value="org.postgresql.Driver"/>
031        <property name="url" value="..."/>
032        <property name="username" value="..."/>
033        <property name="password" value="..."/>
034         <property name="initialSize" value="1"/>
035         <property name="maxActive" value="100"/>
036         <property name="maxIdle" value="8"/>
037         <property name="minIdle" value="1"/>
038     bean>
039      
040     <bean id="postgreEntityManagerFactory"
041         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
042         <property name="persistenceUnitManager" ref="defaultPersistenceUnitManager" />
043         <property name="persistenceUnitName" value="postgre" />
044         <property name="jpaVendorAdapter" ref="postgreJpaVendor" />
045         <property name="loadTimeWeaver">
046             <bean
047                 class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
048         property>
049         <property name="jpaDialect">
050             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
051             bean>
052         property>
053     bean>      
054          
055     <bean id="postgreTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
056         <property name="entityManagerFactory" ref="postgreEntityManagerFactory" />
057         <qualifier value="postgreEM" />
058         <property name="jpaDialect">
059             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
060         property>
061     bean>
062      
063      
064     <tx:annotation-driven transaction-manager="mysqlEntityManagerFactory"/>
065      
066     <bean id="mysqlJpaVendor"
067         class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
068          <property name="showSql" value="true" />
069          <property name="generateDdl" value="true" />
070     bean>
071      
072     <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
073        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
074        <property name="url" value="..."/>
075        <property name="username" value="..."/>
076        <property name="password" value="..."/>
077         <property name="initialSize" value="1"/>
078         <property name="maxActive" value="100"/>
079         <property name="maxIdle" value="8"/>
080         <property name="minIdle" value="1"/>
081     bean>
082      
083     <bean id="mysqlEntityManagerFactory"
084         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
085         <property name="persistenceUnitManager" ref="defaultPersistenceUnitManager" />
086         <property name="persistenceUnitName" value="mysql" />
087         <property name="jpaVendorAdapter" ref="mysqlJpaVendor" />
088         <property name="loadTimeWeaver">
089             <bean
090                 class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
091         property>
092         <property name="jpaDialect">
093             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
094             bean>
095         property>
096     bean>
097      
098     <bean id="mysqlTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
099         <property name="entityManagerFactory" ref="mysqlEntityManagerFactory" />
100         <qualifier value="mysqlEM" />
101         <property name="jpaDialect">
102             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
103         property>
104     bean>
105      
106 beans>

---------------------注解

-------------------------------------DAO-------------------------------------------------------------

------------MySqlBaseDaoImpl.java

1 @Transactional(value="mysqlEM")
2 public class MySqlBaseDaoImpl implements IMySqlBaseDao{
3     @PersistenceContext(unitName="mysql")
4     protected EntityManager em;
5      
6     //......
7 }

------------PostgreSqlBaseDaoImpl.java

1 @Transactional(value="postgreEM")
2 public class PostgreSqlBaseDaoImpl implements IPostgreSqlBaseDao{
3     @PersistenceContext(unitName="postgre")
4     protected EntityManager em;
5  
6     //.......
7 }

-------------------------------------Service-------------------------------------------------------------

---------------ExternalEnvironmentServiceImpl.java

1 @Service
2 @Transactional(value="mysqlEM")
3 public class ExternalEnvironmentServiceImpl extends
4 MySqlBaseDaoImpl implements IExternalEnvironmentService {
5  
6 }

--------------------BdStorageCompanyServiceImpl.java

1 @Service
2 @Transactional(value="postgreEM")
3 public class BdStorageCompanyServiceImpl extends PostgreSqlBaseDaoImpl
4         implements IBdStorageCompanyService {
5  
6 }

-------------------------------------Model(Entity)-------------------------------------------------------------

-------------ExternalEnvironment.java

1 @Entity(name = "TBL_EXTERNALENVIRONMENT")
2 public class ExternalEnvironment extends BaseModel{
3  
4     //......
5  
6 }

-----------------BdStorageCompany.java

1 @Entity(name = "TBL_BDSTORAGECOMPANY")
2 public class BdStorageCompany extends BaseModel {
3     
4    //.......
5  
6 }

你可能感兴趣的:(Web)