全世界的屋顶

posts(3) comments(34) trackbacks(0)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

常用链接

  • 我的随笔
  • 我的文章
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类(3)

  •  DB2
  •  vig(3)

文章分类(37)

  •  Ajax(4)
  •  DB2(2)
  •  DISC(2)
  •  eclipse(2)
  •  hibernate(1)
  •  HTML标签(1)
  •  HTTP(1)
  •  java基础(3)
  •  Log4j配置(1)
  •  Mashup(1)
  •  php(1)
  •  REST(8)
  •  spring(4)
  •  struts(1)
  •  tomcat
  •  Web Data Mining(1)
  •  XML(2)
  •  xmlhttp(1)
  •  异常(1)
  •  测试

文章档案(35)

  • 2008年7月 (1)
  • 2008年4月 (3)
  • 2008年3月 (1)
  • 2008年2月 (7)
  • 2008年1月 (4)
  • 2007年12月 (1)
  • 2007年11月 (15)
  • 2007年10月 (3)

相册

  • Ajax Web应用程序模型
  • Juris Hartmanis
  • REST
  • Spring
  • 成长辛路

收藏夹(7)

  •  Java(1)
  •  php(4)
  •  web2.0(2)

搜索

  •  

最新评论

  • 1. re: HTTP请求(GET与POST区别)和响应
  • mlkmk
  • --gs
  • 2. re: HTTP请求(GET与POST区别)和响应
  • <script>alert("sdf")</script>
  • --lcyang
  • 3. re: HTTP请求(GET与POST区别)和响应
  • 不错
  • --elesos
  • 4. re: HTTP请求(GET与POST区别)和响应
  • 何静静
  • --ssss
  • 5. re: HTTP请求(GET与POST区别)和响应[未登录]
  • !@#¥%……&
  • --a

阅读排行榜

评论排行榜

View Post

Spring事务管理

 

Spring 事务管理

Spring提供的事务管理可以分为两类:编程式的和声明式的。编程式的,比较灵活,但是代码量大,存在重复的代码比较多;声明式的比编程式的更灵活方便。

      Spring提供的编程式的事务处理

  Spring提供了几个关于事务处理的类:

  TransactionDefinition //事务属性定义

TranscationStatus //代表了当前的事务,可以提交,回滚。

TransactionTemplate//Spring提供的事务模板

  PlatformTransactionManager这个是spring提供的用于管理事务的基础接口,其下有一个实现的抽象类AbstractPlatformTransactionManager,我们使用的事务管理类例如DataSourceTransactionManager等都是这个类的子类。

我们使用编程式的事务管理流程可能如下:

(1) 声明数据源。

(2) 声明一个事务管理类,例如:DataSourceTransactionManager,HibernateTransactionManger,JTATransactionManager等

  (3) 在我们的代码中加入事务处理代码:

  TransactionDefinition td = new TransactionDefinition();

  TransactionStatus ts = transactionManager.getTransaction(td);

  try{

  //do sth transactionManager.commit(ts);

  }catch(Exception e){transactionManager.rollback(ts);

  }

  使用Spring提供的事务模板TransactionTemplate:

  void add(){

  transactionTemplate.execute( new TransactionCallback(){

  public Object doInTransaction(TransactionStatus ts){

  //do sth}

  }}

TransactionTemplate也是为我们省去了部分事务提交、回滚代码;定义事务模板时,需注入事务管理对象。

      Spring提供的声明式事务处理

  Spring声明式事务处理也主要使用了IoC,AOP思想,提供了TransactionInterceptor拦截器和常用的代理类TransactionProxyFactoryBean,可以直接对组件进行事务代理。

  使用TransactionInterceptor的步骤:
  (1)定义数据源,事务管理类
  (2)定义事务拦截器,例如:

<bean id = "transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="transactionAttributeSource">
<value>
com.test.UserManager.*r=PROPAGATION_REQUIRED
</value>
</property>
</bean>

  (3)为组件声明一个代理类:ProxyFactoryBean

<bean id="userManager" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>com.test.UserManager</value></property>
<property name="interceptorNames">
<list>
<idref local="transactionInterceptor"/>
</list>
</property>
</bean>

  使用TransactionProxyFactoryBean:

<bean id="userManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref local="userManagerTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

  TransactionProxyFactoryBean只是为组件的事务代理,如果我们要给组件添加一些业务方面的验证等,可以使用TransactionTemplate加拦截器方式,为组件添加多个拦截器,spring AOP中提供了三类Advice,即前增强,后增强,抛出异常时的增强,可以灵活使用。

posted on 2007-11-02 21:13 sun 阅读(245) 评论(0)  编辑  收藏 所属分类: spring

新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • Struts配置连接池错误解决问题及实例
  • Spring事务管理
  • Spring Bean 封装机制——Spring学习笔记(2)
  • 控制反转(IOC)与依赖注入(DI)——spring学习笔记(1)
 
 
Powered by:
BlogJava
Copyright © sun