spring 配置多个数据源(基于JPA)
来源:互联网 发布:mac怎么转音频格式 编辑:程序博客网 时间:2024/05/20 03:45
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="userService" class="hr.gov.serviceImpl.userServiceImpl"/>
<!-- 第一个数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=round" />
<property name="user" value="" />
<property name="password" value=""/>
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="25000"/>
<property name="acquireIncrement" value="1"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="acquireRetryDelay" value="1000"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="automaticTestTable" value="c3p0TestTable"/>
<property name="idleConnectionTestPeriod" value="18000"/>
<property name="checkoutTimeout" value="3000"/>
</bean>
<bean id="entityManagerFactory" autowire="byName"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="aaaUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 将事务管理加到标有 @Transactional 的类或者方法上 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 第二个数据源-->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bbb?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=round" />
<property name="user" value="" />
<property name="password" value="" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="25000"/>
<property name="acquireIncrement" value="1"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="acquireRetryDelay" value="1000"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="automaticTestTable" value="c3p0TestTable"/>
<property name="idleConnectionTestPeriod" value="18000"/>
<property name="checkoutTimeout" value="3000"/>
</bean>
<bean id="entityManagerFactory2" autowire="byName"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="bbbUnit" />
<property name="dataSource" ref="dataSource2" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager2"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>
<!-- 将事务管理加到标有 @Transactional 的类或者方法上 -->
<tx:annotation-driven transaction-manager="govtransactionManager2" />
</beans>
3. dao注解管理事务
建立两个baseDaoImpl:
public EntityManager entityManager;
private EntityManagerFactory emf;
public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext(unitName="aaaUnit")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@PersistenceUnit(unitName="aaaUnit")
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
baseDaoImpl2:
public EntityManager entityManager;
private EntityManagerFactory emf;
public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext(unitName="bbbUnit")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@PersistenceUnit(unitName="bbbUnit")
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
用两个dao分开链接数据库。
实现类继承basedao或basedao2即可。
补充:service添加事务工厂,则需要改变一些配置。
1.两个数据库对应的事务各添加一句 <qualifier value="aaaEM" />
例如:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<qualifier value="aaaEM" />
</bean>
2. 添加之后,会提示出错,不支持qualifier 。
则将applicationContext.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
。
到此完成。
配置过程中遇到的问题:
配置完成后,启动项目应该达到自动建立数据库的效果。
控制台打印错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
如果认真观察的话,上面应该还会提示在创建哪个bean的时候提示出错。
那么提示错误的bean里面应该有用到EntityManagerFactory。
解决办法:对set方法添加注解,如:
@PersistenceUnit(unitName="aaaUnit")
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
这样来区分数据源。
文章转自:http://blog.csdn.net/al_jin/article/details/50202563
- spring 配置多个数据源(基于JPA)
- spring 配置多个数据源(基于JPA)
- spring 配置多个数据源(基于hibernate)
- Spring3+Jpa 配置多个数据源的解决方案(基于注解)
- Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
- Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
- Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
- Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
- Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
- Spring3+Hibernate3(Jpa) 配置多个数据源的解决方案(基于注解)
- JPA整合Spring配置数据源
- spring+jpa 两个数据源配置
- spring+jpa 两个数据源配置
- spring+jpa 两个数据源配置
- Spring 配置多个数据源
- Spring配置多个数据源
- spring配置多个数据源
- Spring 配置多个数据源
- 向量在游戏开发中的应用(一)
- Date8Util
- Omnibus GitLab 使用说明
- HDU 5536 Chip Factory ACM/ICPC 2015 Changchun(Trie)
- 06jquery filternot has
- spring 配置多个数据源(基于JPA)
- 向量在游戏开发中的应用(二)
- C++ const成员函数
- GEM5 Tutorial
- 使用Node+Koa2+Mysql搭建简易博客
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
- 我的第一篇博客
- select2默认选中
- 出勤记录I (水道DP)