Spring的事务管理难点剖析(6):特殊方法成漏网之鱼


   因为Spring事务管理是基于接口代理或动态字节码技术,经过AOP实施事务加强的。虽然Spring还支持AspectJ LTW在类加载期实施加强,但这种方法不多使用,因此咱们不予关注。
    对于基于接口动态代理的AOP事务加强来讲,因为接口的方法都必然是public的,这就要求实现类的实现方法也必须是public的(不能是 protected、private等),同时不能使用static的修饰符。因此,能够实施接口动态代理的方法只能是使用“public”或 “public final”修饰符的方法,其余方法不可能被动态代理,相应的也就不能实施AOP加强,换句话说,即不能进行Spring事务加强了。
    基于CGLib字节码动态代理的方案是经过扩展被加强类,动态建立其子类的方式进行AOP加强植入的。因为使用final、static、private 修饰符的方法都不能被子类覆盖,相应的,这些方法将没法实施AOP加强。因此方法签名必须特别注意这些修饰符的使用,以避免使方法不当心成为事务管理的漏网 之鱼。

事务加强遗漏实例

   本节中,咱们经过具体的实例说明基于CGLib字节码动态代理没法享受Spring AOP事务加强的特殊方法。 java

package com.baobaotao.special; import org.springframework.stereotype.Service; @Service("userService") public class UserService { //① private方法因访问权限的限制,没法被子类覆盖 private void method1() { System.out.println("method1"); } //② final方法没法被子类覆盖 public final void method2() { System.out.println("method2"); } //③ static是类级别的方法,没法被子类覆盖 public static void method3() { System.out.println("method3"); } //④ public方法能够被子类覆盖,所以能够被动态字节码加强 public void method4() { System.out.println("method4"); } }

   Spring经过CGLib动态代理技术对UserService Bean实施AOP事务加强的关键配置,具体以下所示: mysql

… <aop:config proxy-target-class="true"><!-- ①显式使用CGLib动态代理 --> <!-- ②但愿对UserService全部方法实施事务加强 --> <aop:pointcut id="serviceJdbcMethod" expression="execution(* com.baobaotao.special.UserService.*(..))"/> <aop:advisor pointcut-ref="serviceJdbcMethod" advice-ref="jdbcAdvice" order="0"/> </aop:config> <tx:advice id="jdbcAdvice" transaction-manager="jdbcManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> …

在①处,咱们经过proxy-target-class="true"显式使用CGLib动态代理技术,在②处经过AspjectJ切点表达式表达UserService全部的方法,但愿对UserService全部方法都实施Spring AOP事务加强。
   在UserService添加一个可执行的方法,以下所示:spring

package com.baobaotao.special; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Service; @Service("userService") public class UserService { … public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("user/special/applicationContext.xml"); UserService service = (UserService) ctx.getBean("userService"); System.out.println("before method1"); service.method1(); System.out.println("after method1"); System.out.println("before method2"); service.method2(); System.out.println("after method2"); System.out.println("before method3"); service.method3(); System.out.println("after method3"); System.out.println("before method4"); service.method4(); System.out.println("after method4"); } }


   在运行UserService以前,将Log4J日志级别设置为DEBUG,运行以上代码查看输出日志,以下所示:sql

引用

①未启用事务
before method1
in method1
after method1

②未启用事务
before method2
in method2
after method2

③未启用事务
before method3
in method3
after method3

④启用事务
before method4
Creating new transaction with name [com.baobaotao.special.UserService.method4]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT

in method4
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver]
Releasing JDBC Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver] after transaction
Returning JDBC Connection to DataSource
after method4

⑤未用事务
before method5
in method5
after method5

⑥启用事务
before method6
Creating new transaction with name [com.baobaotao.special.UserService.method6]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT

in method6
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver]

after method6


  观察以上输出日志,很容易发现method1~method3及method5这4个方法都没有被实施Spring的事务加强,而method4和method6被实施了事务加强。这个结果验证了咱们前面的论述。
  咱们经过下表描述哪些特殊方法将成为Spring AOP事务加强的漏网之鱼。
序  号         
  动态代理策略                                          
不能被事务加强的方法
1 基于接口的动态代理 除public外的其余全部的方法,此外public static也不能被加强
2 基于CGLib的动态代理 private、static、final的方法


   不过,须要特别指出的是,这些不能被Spring事务加强的特殊方法并不是就不工做在事务环境下。只要它们被外层的事务方法调用了,因为Spring事务管 理的传播级别,内部方法也能够工做在外部方法所启动的事务上下文中。咱们说,这些方法不能被Spring进行AOP事务加强,是指这些方法不能启动事务, 可是外层方法的事务上下文依旧能够顺利地传播到这些方法中。
   这些不能被Spring事务加强的方法和可被Spring事务加强的方法惟一的区别在于“是否能够主动启动一个新事务”:前者不能然后者能够。对于事务传 播行为来讲,两者是彻底相同的,前者也和后者同样不会形成数据链接的泄漏问题。换句话说,若是这些“特殊方法”被无事务上下文的方法调用,则它们就工做在 无事务上下文中;反之,若是被具备事务上下文的方法调用,则它们就工做在事务上下文中。
   对于private的方法,因为最终都会被public方法封装后再开放给外部调用,而public方法是能够被事务加强的,因此基本上没有什么问题。在 实际开发中,最容易形成隐患的是基于CGLib的动态代理时的“public static”和“public final”这两种特殊方法。缘由是它们自己是public的,所以能够直接被外部类(如Web层的Controller类)调用,只要调用者没有事务上 下文,这些特殊方法也就以无事务的方式运做。

注:以上内容摘自《Spring 3.x企业应用开发实战》
相关文章
相关标签/搜索