﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-warmcool-文章分类-spring</title><link>http://www.blogjava.net/warmcool/category/8000.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 03:37:34 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 03:37:34 GMT</pubDate><ttl>60</ttl><item><title>spring提供的事务管理</title><link>http://www.blogjava.net/warmcool/articles/33145.html</link><dc:creator>warmcool's blog</dc:creator><author>warmcool's blog</author><pubDate>Thu, 02 Mar 2006 02:58:00 GMT</pubDate><guid>http://www.blogjava.net/warmcool/articles/33145.html</guid><wfw:comment>http://www.blogjava.net/warmcool/comments/33145.html</wfw:comment><comments>http://www.blogjava.net/warmcool/articles/33145.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/warmcool/comments/commentRss/33145.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/warmcool/services/trackbacks/33145.html</trackback:ping><description><![CDATA[<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; spring提供的事务管理可以分为两类：编程式的和声明式的。编程式的，比较灵活，但是代码量大，存在重复的代码比较多；声明式的比编程式的更灵活。</DIV>
<DIV>&nbsp;</DIV>
<DIV>传统使用JDBC的事务管理</DIV>
<DIV>================&nbsp;</DIV>
<DIV>以往使用JDBC进行数据操作，使用DataSource,从数据源中得到Connection,我们知道数据源是线程安全的，而连接不是线程安全的，所以对每个请求都是从数据源中重新取出一个连接。一般的数据源由容器进行管理，包括连接池。例如TOMCAT，WEBSPHERE，WEBLOGIC等这些J2EE商业容器都提供了这个功能。</DIV>
<DIV>&nbsp;</DIV>
<DIV>以往的我们使用JDBC在写代码时，事务管理可能会是这样：</DIV>
<DIV>Connection conn = null;</DIV>
<DIV>try</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; conn&nbsp; = DBConnectionFactory.getConnection;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; conn.setAutoCommit(false);</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; //do something</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; conn.commit(); //commit transcation</DIV>
<DIV>}</DIV>
<DIV>catch(Exception e)</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;&nbsp; conn.rollback();</DIV>
<DIV>&nbsp;&nbsp;&nbsp; //do sth</DIV>
<DIV>}</DIV>
<DIV>finally</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; try</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; {</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn.close();</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; }</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; catch(SQLException se){ //do sth.}</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; //close ResultSet,PreparedStatement,Connection 
<DIV>&nbsp;&nbsp;&nbsp; //notice:Maybe ocurr Exception when u close rs,pstmt,conn</DIV>
<DIV>}</DIV></DIV>
<DIV>&nbsp;</DIV>
<DIV>按照以往的思路来写代码，代码量比较长，而且容易疏忽，忘掉一些try/catch，引发一些异常无法catch，虽然有时候我们会写DBTool类，来关闭这些资源，并且保证在关闭这些资源时，不向外抛异常。</DIV>
<DIV>&nbsp;</DIV>
<DIV>spring提供的编程式的事务处理</DIV>
<DIV>&nbsp;==================</DIV>
<DIV>spring提供了几个关于事务处理的类：&nbsp;TransactionDefinition //事务属性定义</DIV>
<DIV>TranscationStatus //代表了当前的事务，可以提交，回滚。</DIV>
<DIV>PlatformTransactionManager这个是spring提供的用于管理事务的基础接口，其下有一个实现的抽象类AbstractPlatformTransactionManager,我们使用的事务管理类例如DataSourceTransactionManager等都是这个类的子类。</DIV>
<DIV>&nbsp;</DIV>
<DIV>我们使用编程式的事务管理流程可能如下：</DIV>
<DIV>1 声明数据源</DIV>
<DIV>2 声明一个事务管理类，例如DataSourceTransactionManager,HibernateTransactionManger,JTATransactionManager等</DIV>
<DIV>3 在我们的代码中加入事务处理代码：</DIV>
<DIV>TransactionDefinition td = new TransactionDefinition();</DIV>
<DIV>TransactionStatus ts = transactionManager.getTransaction(td);</DIV>
<DIV>try</DIV>
<DIV>{ //do sth</DIV>
<DIV>&nbsp; transactionManager.commit(ts);</DIV>
<DIV>}</DIV>
<DIV>catch(Exception e){transactionManager.rollback(ts);}</DIV>
<DIV>&nbsp;</DIV>
<DIV>使用spring提供的事务模板TransactionTemplate</DIV>
<DIV>void add()</DIV>
<DIV>{</DIV>
<DIV>&nbsp;&nbsp;&nbsp; transactionTemplate.execute( new TransactionCallback(){</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pulic Object doInTransaction(TransactionStatus ts)</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { //do sth}</DIV>
<DIV>&nbsp;&nbsp;&nbsp; }</DIV>
<DIV>}</DIV>
<DIV>TransactionTemplate也是为我们省去了部分事务提交、回滚代码；定义事务模板时，需注入事务管理对象.</DIV>
<DIV>&nbsp;</DIV>
<DIV>spring声明式事务处理</DIV>
<DIV>=============</DIV>
<DIV>spring声明式事务处理也主要使用了ioc,aop思想，提供了TransactionInterceptor拦截器和常用的代理类TransactionProxyFactoryBean,可以直接对组件进行事务代理。</DIV>
<DIV>&nbsp;</DIV>
<DIV>使用TransactionInterceptor步骤</DIV>
<DIV>1.定义数据源，事务管理类</DIV>
<DIV>2.定义事务拦截器,such as:</DIV>
<DIV>&lt;bean id = "transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;</DIV>
<DIV>&nbsp; &lt;property name="transactionManager"&gt;&lt;ref bean="transactionManager"/&gt;&lt;/property&gt;<BR>&nbsp;&nbsp;&lt;property name="transactionAttributeSource"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;value&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; com.test.UserManager.*r=PROPAGATION_REQUIRED<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/value&gt;<BR>&nbsp;&nbsp;&lt;/property&gt;</DIV>
<DIV>&lt;/bean&gt;</DIV>
<DIV>3.为组件声明一个代理类：ProxyFactoryBean</DIV>
<DIV>&lt;bean id="userManager" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="proxyInterfaces"&gt;&lt;value&gt;com.test.UserManager&lt;/value&gt;&lt;/property&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name="interceptorNames"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;list&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;idref local="transactionInterceptor"/&gt;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/list&gt;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/property&gt;</DIV>
<DIV>&nbsp;&lt;/bean&gt;</DIV>
<DIV>&nbsp;</DIV>
<DIV>使用TransactionProxyFactoryBean:</DIV>
<DIV>&lt;bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;<BR>&nbsp;&nbsp;&lt;property name="transactionManager"&gt;&lt;ref bean="transactionManager"/&gt;&lt;/property&gt;<BR>&nbsp;&nbsp;&lt;property name="target"&gt;&lt;ref local="userManagerTarget"/&gt;&lt;/property&gt;<BR>&nbsp;&nbsp;&lt;property name="transactionAttributes"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;props&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop key="insert*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop key="update*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop key="*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/props&gt;<BR>&nbsp;&nbsp;&lt;/property&gt;<BR>&nbsp;&lt;/bean&gt;</DIV>
<DIV>&nbsp;</DIV>TransactionProxyFactoryBean只是为组件的事务代理，如果我们要给组件添加一些业务方面的验证等，可以使用TransactionTemplate加拦截器方式，为组件添加多个拦截器，spring AOP中提供了三类Advice,即前增强，后增强，抛出异常时的增强，可以灵活使用。<img src ="http://www.blogjava.net/warmcool/aggbug/33145.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/warmcool/" target="_blank">warmcool's blog</a> 2006-03-02 10:58 <a href="http://www.blogjava.net/warmcool/articles/33145.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>