1、Spring20简明手册系列之五 事务管理Spring2.0简明手册(系列之五 事务管理) 2008-07-16 21:26:45标签:Spring 简明 手册 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 1.声明式事务管理 Spring2.0及以后的版本中声明式事务的配置与之前的版本有相当大的不同。主要差异在于不再需要配置TransactionProxyFactoryBean了。1.1基于XML Schema/我们想做成事务性的服务接口package x.y.service;public interface FooService
2、Foo getFoo(String fooName); Foo getFoo(String fooName, String barName); void insertFoo(Foo foo); void updateFoo(Foo foo);/上述接口的一个实现package x.y.service;public class DefaultFooService implements FooService public Foo getFoo(String fooName) throw new UnsupportedOperationException(); public Foo getFoo(S
3、tring fooName, String barName) throw new UnsupportedOperationException(); public void insertFoo(Foo foo) throw new UnsupportedOperationException(); public void updateFoo(Foo foo) throw new UnsupportedOperationException(); classpath:jdbc-config.properties 更进一步:参照Spring2.0参考手册-9.5.5. 有关的设置1.2使用 Transa
4、ctional /the service class that we want to make transactional Transactional(readOnly = true) public class DefaultFooService implements FooService public Foo getFoo(String fooName) / do something / these settings have precedence for this method Transactional(readOnly = false, propagation = Propagatio
5、n.REQUIRES_NEW) public void updateFoo(Foo foo) / do something classpath:jdbc-config.properties 更进一步:参照Spring2.0参考手册-9.5.6.1. Transactional 有关的设置1.3插入事务操作 1.3.1使用 Transactional Transactional(readOnly = true) public class DefaultFooService implements FooService public Foo getFoo(String fooName) / do s
6、omething / these settings have precedence for this method Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void updateFoo(Foo foo) / do something package x.y;import org.aspectj.lang.ProceedingJoinPoint;import org.springframework.util.StopWatch;import org.springframework
7、.core.Ordered;public class SimpleProfiler implements Ordered private int order; / allows us to control the ordering of advice public int getOrder() return this.order; public void setOrder(int order) this.order = order; / this method is the around advice public Object profile(ProceedingJoinPoint call) throws Throwable Object returnValue; StopWatch clock = new StopWatch(getClass().getName(); try clock.start(call.toShortString(); returnValue = call.proceed(); finally clock.stop(); System.out.println(clock.prettyPrint(); return returnValue; !- thi
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2