- 数据源配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byName">
<bean id="dataSource-hr" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> <property name="filters" value="stat" /> <property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" /> <property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="50" />
</bean> <!-- Hibernate Jpa SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource-hr" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">200</prop>
<prop key="hibernate.jdbc.batch_size">200</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.xx.xx.pojo</value>
</list>
</property>
</bean>
</beans>数据源1配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byName">
<bean id="dataSource-pku" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.pku.url}" />
<property name="username" value="${jdbc.pku.username}" />
<property name="password" value="${jdbc.pku.password}" /> <property name="filters" value="stat" /> <property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" /> <property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="50" />
</bean> <!-- Hibernate Jpa SessionFactory -->
<bean id="sessionFactory-pku"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource-pku" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">200</prop>
<prop key="hibernate.jdbc.batch_size">200</prop>
<prop key="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 -->
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
</props>
</property> <property name="packagesToScan">
<list>
<value>com.xx.xx.pojo</value>
</list>
</property>
</bean>
</beans>数据源2配置
- 事务的配置
<?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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-autowire="byName">
<!-- 事务管理器配置, Hibernate多数据源事务 -->
<bean id="transactionManager-hr"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <bean id="transactionManager-pku"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory-pku" />
</bean> <!-- 1.使用annotation定义事务 -->
<!-- proxy-target-class ture:动态代理(需cglib类库的支持) false:接口(Spring默认方式) -->
<tx:annotation-driven transaction-manager="transactionManager-pku"
proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager-hr"
proxy-target-class="true" />
<!-- proxy-target-class true:CGLIB代理 false:JDK代理 -->
<aop:aspectj-autoproxy expose-proxy="true"
proxy-target-class="true" /> <!-- 2.声明式事务配置 -->
<aop:config>
<aop:pointcut id="serviceMethods-pku"
expression="execution(* com.wisdombud.career.business.*.*(..)) || execution(* com.wisdombud.career.business.impl.*.*(..))" />
<aop:pointcut id="serviceMethods-hr"
expression="execution(* com.wisdombud.wisdomhr.srv.*.*(..)) || execution(* com.wisdombud.wisdomhr.srv.impl.*.*(..))
||execution(* com.wisdombud.wisdomhr.common.srv.*.*(..)) ||execution(* com.wisdombud.common.srv.*.*(..))" /> <aop:advisor advice-ref="txAdvice-pku" pointcut-ref="serviceMethods-pku" />
<aop:advisor advice-ref="txAdvice-hr" pointcut-ref="serviceMethods-hr" />
</aop:config> <!-- 通知 默认事务管理器:transaction-manager="transactionManager" -->
<tx:advice id="txAdvice-hr" transaction-manager="transactionManager-hr">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="page*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="plot*" read-only="true" />
<tx:method name="login*" propagation="REQUIRED" read-only="true" />
<tx:method name="set*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="start*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="process*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="audit*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="add*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="save*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="register*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="del*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="change*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 通知 默认事务管理器:transaction-manager="transactionManager" -->
<tx:advice id="txAdvice-pku" transaction-manager="transactionManager-pku">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="page*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="plot*" read-only="true" />
<tx:method name="login*" propagation="REQUIRED" read-only="true" />
<tx:method name="set*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="start*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="process*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="audit*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="add*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="save*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="register*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="del*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="change*" propagation="REQUIRED"
rollback-for="common.toolkit.java.exception" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>事务配置
- 部分类的spring bean配置
<bean id="pku-dic-Dao" class="com.wisdombud.career.dao.impl.DicCommonDaoImpl">
<property name="sessionFactory" ref="sessionFactory-pku" />
</bean>类配置
- 说明与坑
- 多数据源主要是数据库有多个,而业务上往往决定了需要同时进行两个数据库的操作,因此在配置的时候需要考虑清楚事务一致性需要在什么级别配置;
- spring集成hibernate进行事务配置的时候,离不开aop,而aop其实是方法级别的配置,针对某些方法进行切面。如果要切面的类继承了另外一个类,而且父类与子类不在一个包里面,那么在执行父类方法的时候需要aop的话,aop的整体配置pointcut的时候,需要将包含父类的规则包含进来,否则spring不会生成其动态代理。
相关文章
- Java 学习笔记14:Spring 数据库数据源DBCP配置说明
- spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置
- java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
- JAVA spring hibernate 多数据源配置记录
- Mongodb集群搭建及spring和java连接配置记录
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源方法
- Spring使用Hibernate和再加SpringData时配置的差别基于Java配置
- Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)
- spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池