spring3.0事务的多数据源的annotation-driven用法

一.annotation-driven如何正确使用事务管理器
(本文仅基于3.0+版本作为测试)
假定spring 容器中定义了两个事务管理器: transactionManagerX, transactionManagerY,分管两个数据源datasourceX和datasourceY.
<tx:annotation-driven transaction-manager="transactionManagerX" />
<tx:annotation-driven transaction-manager="transactionManagerY" />
(spring容器中的定义顺序如上)
有如下应用代码:
public interface TestEntityService {
public void methodX();
public void methodY();
}
 
接口实现类1
public class  TestEntityServiceImpl implements TestEntityService {
@Resource
private TestEntityDao testEntityDao;//实际操作的是 datasourceX.
@Transactional
public void methodX() {
testEntityDao.xxx();
testEntityDao.zzz();
}
public void methodY() {
}
}
 
接口实现类2
public class  AnotherTestEntityServiceImpl implements TestEntityService {
@Resource
private TestEntityDao anOtherTestEntityDao;//实际操作的是 datasourceY.
@Transactional
public void methodX() {
testEntityDao.mmm();
testEntityDao.nnn();
}
public void methodY() {
}
}
假设方法methodX需要事务控制的,通常我们是直接在方法上添加@Transactional标注,
但是好像spring3.0(具体版本没弄清)之前的Transactional标注不支持区分使用哪个事务管理器。3.0之后的版本Transactional增加了个string类型的value属性来特殊指定加以区分。
例如@Transactional(" aaaaa"),即显示的要求spring用id=" aaaaa"的事务管理器来管理事务。该属性亦可省略(省略的话用容器中缺省的transactionManager)
对于该属性的用法做了如下测试来
methodX()事务生效测试结果
@Transactional
("transactionManagerX")
 
@Transactional
("transactionManagerY")
 
@Transactional
("transactionManagerZ")
 transactionManagerZ为未定义过的
@Transactional  
TestEntityServiceImpl(实际使用datasourceX)
Y
N
Y
Y
AnotherTestEntityServiceImpl (实际使用datasourceY)
N
Y
N
N
如果调换两个事务管理器在容器中的定义顺序,如
<tx:annotation-driven transaction-manager="transactionManagerY" />
<tx:annotation-driven transaction-manager="transactionManagerX" />
得到的结果
methodX()事务生效测试结果
@Transactional
("transactionManagerX")
 
@Transactional
("transactionManagerY")
 
@Transactional
("transactionManagerZ")
 transactionManagerZ为未定义过的
@Transactional  
TestEntityServiceImpl(实际使用datasourceX)
Y
N
N
N
AnotherTestEntityServiceImpl (实际使用datasourceY)
N
Y
Y
Y
 
分析结果(其实源码就可以反应出):容器指定一个默认的事务管理器
1.当在@Transactional(" xxx")中正确指定了需要使用的事务管理器时,事务控制正常。
2.如果@Transactional指定了未定义过的事务管理器,spring以缺省默认的事务管理器来处理。(如果程序正好使用的是缺省事务管理器同一个数据源,事务控制将生效)。
3.如果@Transactional不指定事务管理器,使用缺省。
4.如果@Transactional指定了不匹配的事务管理器(实际用到的数据源和指定的事务管理器控制的数据源不一致),事务控制将失效.
注:spring容器缺省事务管理器:以加载顺序,首先加载的作为缺省。例如
如果
<tx:annotation-driven transaction-manager="transactionManagerX" />
<tx:annotation-driven transaction-manager="transactionManagerY" />
定义在同一个文件中,则第一个transactionManagerX作为缺省。
定义在不同文件,则按文件的加载顺序,首先加载的作为缺省。
 
建议:实际代码中需要用到@Transactional时,即使默认只有一个transactionManager,@Transactional也将其标明。以提高新增数据源后代码可读性,另外防止定义多个数据源后,以前缺省的不被spring默认为缺省了(比如哪天上线新定义了一个数据源,刚好新定义的transactionManager被先加载了,那就悲剧了。)
 
二.bean的配置使用
容器中加了<tx:annotation-driven >(需要增加一些xsd)之后,需要事务控制的的service,不需要再具体的bean上做其他的配置,例如用代理包装。直接配置即可
<bean id="testEntityService" class="com.xxx.impl.TestEntityServiceImpl"/>
spring将由JdkDynamicAopProxy 生成代理过的类提供使用。
这种用法的效果和下面配置使用效果一样。都是由JdkDynamicAopProxy 生成代理对象提供使用。
我觉得区别是下面的方法在事务控制的代码可读性上不好,因为哪个方法需要事务控制和控制粒度都在配置文件中,和代码分开了。
<bean id="testEntityService3" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager" ref="transactionManagerX" />
      <property name="target">
         <bean class="com.xxxx.impl.TestEntityServiceImpl" />
      </property>
      <property name="proxyInterfaces" value="com.xxxx.TestEntityService"/>
      <property name="transactionAttributes">
         <props>
           <prop key="*">PROPAGATION_REQUIRED</prop>
         </props>
      </property>
</bean>
 
方法的可见度和 @Transactional
@Transactional 注解应该只被应用到 public 可见度的方法上。 如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。
@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。然而,请注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据,能够被可以识别 @Transactional 注解和上述的配置适当的具有事务行为的beans所使用。上面的例子中,其实正是 <tx:annotation-driven/>元素的出现 开启 了事务行为。
Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。你当然可以在接口上使用 @Transactional 注解,但是这将只能当你设置了基于接口的代理时它才生效。因为注解是 不能继承 的。
 
实际开发中,多半喜欢将持久化操作的代码集中抽出为另一个方法(因为不想事务被无关的业务代码托的持续太长),然后在抽取出来的方法上加上@Transactional,这样的结果是被抽离出的代码即使加了事务标记,也根本起不到事务控制的效果(不管是private和public)。
例如:
public class TestEntityServiceImpl implements TestEntityService {
@Resource
private TestEntityDao testEntityDao;//实际操作的是datasourceX.
@Transactional
public void methodX() {
testEntityDao.xxx();
testEntityDao.zzz();
}
public void methodY() {
methodX() 
}
}
如果执行TestEntityService.methodY();事务是不生效的。只有TestEntityService.methodX();才生效。
从spring实现这些的原理(动态代理和aop)上来看,只拦截外部调用,方法的内部调用通常是不被aop支持的。
从网上扒到一篇文章,可以解决这个问题。 http://blog.csdn.net/quzishen/archive/2010/08/11/5803721.aspx


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: bonecp.driverClass=org.gjt.mm.mysql.Driver bonecp.jdbcUrl=jdbc:mysql://localhost/manytomany?useUnicode=true&characterEncoding=UTF-8 bonecp.username=root bonecp.password=2008 #分区数量 bonecp.partitionCount = 1 #每个分区含有的最小连接数 bonecp.minConnectionsPerPartition = 1 #每个分区含有的最大连接数 bonecp.maxConnectionsPerPartition = 2 #每次新增连接的数量 bonecp.acquireIncrement = 1 #连接池阀值,当 可用连接/最大连接 < 连接阀值 时,创建新的连接 bonecp.poolAvailabilityThreshold = 20 #连接超时时间阀值,获取连接时,超出阀值时间,则获取失败,毫秒为单位 bonecp.connectionTimeout = 10000 #连接池助手线程数量,可设置为0,该参数会降低运行速度,但程序有大量连接时,有助于提升高并发程序的性能 bonecp.releaseHelperThreads = 0 #语句助手线程数,可设置为0,该参数会降低运行速度,但程序有大量的查询语句时,有助于提升高并发程序的性能 bonecp.statementReleaseHelperThreads = 0 #测试连接有效性的间隔时间,单位分钟 bonecp.idleConnectionTestPeriod = 60 #连接的空闲存活时间,当连接空闲时间大于该阀值时,清除该连接 bonecp.idleMaxAge = 240 #语句缓存个数,默认是0 bonecp.statementsCacheSize = 5 Spring中的配置信息 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <aop:config proxy-target-class="true"/> <tx:annotation-driven transaction-manager="txManager"/> <!-- 采用单数据源事务控制方式,通过注解来定义事务--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:proxool.properties</value> </property> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"> <property name="driver"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="driverUrl"> <value>jdbc:mysql://localhost/manytomany?useUnicode=true&characterEncoding=UTF-8</value> </property> <property name="user"> <value>root</value> </property> <property name="password"> <value>2008</value> </property> <property name="alias"> <value>Db_name</value> </property> <property name="houseKeepingSleepTime"> <value>90000</value> </property> <property name="prototypeCount"> <value>50</value> </property> <property name="maximumConnectionCount"> <value>50</value> </property> <property name="minimumConnectionCount"> <value>2</value> </property> <property name="trace"> <value>true</value> </property> <property name="verbose"> <value>true</value> </property> </bean> </beans>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值