<?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="aaaUnit")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@PersistenceUnit(unitName="aaaUnit")
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;
}
这样来区分数据源。
重启,ok。