Hibernate的session是否是同一实例(二)

多数据源的情况,首先是配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd   
	http://www.springframework.org/schema/jee 
	http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
	default-autowire="byName">

	<context:property-placeholder
		location="classpath*:META-INF/fundProfileContext.properties" />

	<context:component-scan base-package="com.jsw.fund" />

	<bean id="basedataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${finabase.jdbc.driver}" />
		<property name="url" value="${finabase.jdbc.url}" />
		<property name="username" value="${finabase.jdbc.user}" />
		<property name="password" value="${finabase.jdbc.password}" />
		<!--initialSize: 初始化连接 -->
		<property name="initialSize" value="5" />
		<!--maxIdle: 最大空闲连接 -->
		<property name="maxIdle" value="10" />
		<!--minIdle: 最小空闲连接 -->
		<property name="minIdle" value="5" />
		<!--maxActive: 最大连接数量 -->
		<property name="maxActive" value="100" />
		<!--removeAbandoned: 是否自动回收超时连接 -->
		<property name="removeAbandoned" value="true" />
		<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
		<property name="removeAbandonedTimeout" value="180" />
		<!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
		<property name="maxWait" value="30000" />
	</bean>
	<bean id="derivateDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${derivate.jdbc.driver}" />
		<property name="url" value="${derivate.jdbc.url}" />
		<property name="username" value="${derivate.jdbc.user}" />
		<property name="password" value="${derivate.jdbc.password}" />
		<!--initialSize: 初始化连接 -->
		<property name="initialSize" value="5" />
		<!--maxIdle: 最大空闲连接 -->
		<property name="maxIdle" value="10" />
		<!--minIdle: 最小空闲连接 -->
		<property name="minIdle" value="5" />
		<!--maxActive: 最大连接数量 -->
		<property name="maxActive" value="100" />
		<!--removeAbandoned: 是否自动回收超时连接 -->
		<property name="removeAbandoned" value="true" />
		<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
		<property name="removeAbandonedTimeout" value="180" />
		<!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
		<property name="maxWait" value="30000" />
	</bean>
	
		<bean id="profileAppDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${profileApp.jdbc.driver}" />
		<property name="url" value="${profileApp.jdbc.url}" />
		<property name="username" value="${profileApp.jdbc.user}" />
		<property name="password" value="${profileApp.jdbc.password}" />
		<!--initialSize: 初始化连接 -->
		<property name="initialSize" value="5" />
		<!--maxIdle: 最大空闲连接 -->
		<property name="maxIdle" value="10" />
		<!--minIdle: 最小空闲连接 -->
		<property name="minIdle" value="5" />
		<!--maxActive: 最大连接数量 -->
		<property name="maxActive" value="100" />
		<!--removeAbandoned: 是否自动回收超时连接 -->
		<property name="removeAbandoned" value="true" />
		<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
		<property name="removeAbandonedTimeout" value="180" />
		<!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
		<property name="maxWait" value="30000" />
	</bean>
	
	<!-- Hibernate配置 -->
	<bean id="baseSessionFactory"
		class="com.jsw.common.spring.support.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="basedataSource" />
		<property name="packagesToScan" value="com.jsw.fund.model.base" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
			</props>
		</property>
		<property name="lobHandler">
			<ref bean="lobHandler"></ref>
		</property>
	</bean>
	
	<bean id="derivateSessionFactory"
		class="com.jsw.common.spring.support.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="derivateDataSource" />
		<property name="packagesToScan" value="com.jsw.fund.model.derivate" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
			</props>
		</property>
		<property name="lobHandler">
			<ref bean="lobHandler"></ref>
		</property>
	</bean>

	<bean id="profileAppSessionFactory"
		class="com.jsw.common.spring.support.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="profileAppDataSource" />
		<property name="packagesToScan" value="com.jsw.fund.profile.model" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
			</props>
		</property>
		<property name="lobHandler">
			<ref bean="lobHandler"></ref>
		</property>
	</bean>

	<bean name="lobHandler"
		class="org.springframework.jdbc.support.lob.DefaultLobHandler"
		lazy-init="true">
	</bean>

	<bean id="baseTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="baseSessionFactory" />
	</bean>

	<bean id="derivateTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="derivateSessionFactory" />
	</bean>
	<bean id="profileAppTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="profileAppSessionFactory" />
	</bean>

	<tx:advice id="baseTxAdvice" transaction-manager="baseTransactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" read-only="true" propagation="REQUIRED" />
			<tx:method name="load*" read-only="true" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" propagation="REQUIRED" />
			<tx:method name="search*" read-only="true" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" read-only="false" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:advisor id="baseAopAdvisor" pointcut="execution(* com.jsw..*ServiceImpl.*(..))"
			advice-ref="baseTxAdvice" />
		<aop:advisor id="baseAopAdvisor2" pointcut="execution(* com.jsw..*Service.*(..))"
			advice-ref="baseTxAdvice" />
		<aop:advisor id="baseAopAdvisor3" pointcut="execution(* com.jsw..*ProcessorImpl.*(..))"
			advice-ref="baseTxAdvice" />
		<aop:advisor id="baseAopAdvisor4" pointcut="execution(* com.jsw..*Shell.*(..))"
			advice-ref="baseTxAdvice" />
		<aop:advisor id="baseAopAdvisor5" pointcut="execution(* com.jsw..*ShellImpl.*(..))"
			advice-ref="baseTxAdvice" />
	</aop:config>
	
	<tx:advice id="derivateTxAdvice" transaction-manager="derivateTransactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" read-only="true" propagation="REQUIRED" />
			<tx:method name="load*" read-only="true" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" propagation="REQUIRED" />
			<tx:method name="search*" read-only="true" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" read-only="false" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:advisor id="derivateAopAdvisor" pointcut="execution(* com.jsw..*ServiceImpl.*(..))"
			advice-ref="derivateTxAdvice" />
		<aop:advisor id="derivateAopAdvisor2" pointcut="execution(* com.jsw..*Service.*(..))"
			advice-ref="derivateTxAdvice" />
		<aop:advisor id="derivateAopAdvisor3" pointcut="execution(* com.jsw..*Processor.*(..))"
			advice-ref="derivateTxAdvice" />
		<aop:advisor id="derivateAopAdvisor4" pointcut="execution(* com.jsw..*ProcessorImpl.*(..))"
			advice-ref="derivateTxAdvice" />
			
		<!--保证在shell中,用到的derivate为同一个session -->
		<aop:advisor id="derivateAopAdvisor5" pointcut="execution(* com.jsw..*Shell.*(..))"
			advice-ref="derivateTxAdvice" />
		<aop:advisor id="derivateAopAdvisor6" pointcut="execution(* com.jsw..*ShellImpl.*(..))"
			advice-ref="derivateTxAdvice" />
	</aop:config>
	
	<tx:advice id="profileAppTxAdvice" transaction-manager="profileAppTransactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" read-only="true" propagation="REQUIRED" />
			<tx:method name="load*" read-only="true" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" propagation="REQUIRED" />
			<tx:method name="search*" read-only="true" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" read-only="false" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:advisor id="profileAppAopAdvisor" pointcut="execution(* com.jsw..*ServiceImpl.*(..))"
			advice-ref="profileAppTxAdvice" />
		<aop:advisor id="profileAppAopAdvisor2" pointcut="execution(* com.jsw..*Service.*(..))"
			advice-ref="profileAppTxAdvice" />
		<aop:advisor id="profileAppAopAdvisor3" pointcut="execution(* com.jsw..*Processor.*(..))"
			advice-ref="profileAppTxAdvice" />
		<aop:advisor id="profileAppAopAdvisor4" pointcut="execution(* com.jsw..*ProcessorImpl.*(..))"
			advice-ref="profileAppTxAdvice" />
		<aop:advisor id="profileAppAopAdvisor5" pointcut="execution(* com.jsw..*Shell.*(..))"
			advice-ref="profileAppTxAdvice" />
		<aop:advisor id="profileAppAopAdvisor6" pointcut="execution(* com.jsw..*ShellImpl.*(..))"
			advice-ref="profileAppTxAdvice" />
	</aop:config>

	<tx:annotation-driven />
	
</beans>

 

 

Test Case:

 

  @Test
  public void testT() throws Exception {
    shell.process();
  }

 

 

process的实现:

  @Override
  public void process() {
    fundProfileService.f1();
    fundProfileService.f2();
    fundCategoryService.f1();
    fundCategoryService.f2();
    fundProfileService.f1();
    fundProfileService.f2();
  }

 

其中fundProfileService使用的数据源是basedataSource,fundCategoryService使用的数据源是derivateDataSource。

 

f1 f2参见上篇日志

http://king-lm.iteye.com/blog/1913556

输出的结果:

 

22580698

22580698

22580698

13180774

13180774

13180774

22580698

22580698

22580698

 

 

若把base的aop-config改为:

	<aop:config>
		<aop:advisor id="baseAopAdvisor" pointcut="execution(* com.jsw..*ServiceImpl.*(..))"
			advice-ref="baseTxAdvice" />
		<aop:advisor id="baseAopAdvisor2" pointcut="execution(* com.jsw..*Service.*(..))"
			advice-ref="baseTxAdvice" />
		<aop:advisor id="baseAopAdvisor3" pointcut="execution(* com.jsw..*ProcessorImpl.*(..))"
			advice-ref="baseTxAdvice" />
	</aop:config>

 

则输出的结果为:

8559295

12230572

12230572

4115754

4115754

4115754

12062111

18151786

18151786

看到derivateDataSource还是比较整齐的,baseDataSource每次调用都会发生变化,然而base的transactionManager的事务在service上,所以一个service中调用2此的输出结果是一样的。
结论:
多数据源的事务管理,各顾各的,互不干涉。在同一个事务中共用同一session。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值