本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:210
评论数量:205
留言数量:-19
访问次数:924557
建立时间:2007年5月10日




[Spring]spring 事务管理探讨
文章收藏,  网上资源,  软件技术,  电脑与网络

李小白 发表于 2009/6/5 9:56:40

声明式的事务管理(Declarative transaction management): <1>事务配置方式: Java代码 500)this.width=500'> <!-- dataSource for MySQL -->    <bean id="dataSource"           class="org.apache.commons.dbcp.BasicDataSource"           destroy-method="close">            <property name="driverClassName"               value="com.mysql.jdbc.Driver" />            <property name="url"               value="jdbc:mysql://localhost:3306/springapp" />            <property name="username" value="root" />            <property name="password" value="root" />    </bean>     <!-- dataSource for MySQL --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/springapp" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> Java代码 500)this.width=500'> <!-- Hibernate SessionFactory for MySQL -->    <bean id="sessionFactory"       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">            <property name="dataSource" ref="dataSource" />            <property name="mappingDirectoryLocations">                <list>                    <value>classpath:/</value>                </list>            </property>            <property name="hibernateProperties">                <props>                    <prop key="hibernate.dialect">                        org.hibernate.dialect.MySQLDialect                    </prop>                    <prop key="hibernate.show_sql">true</prop>                    <prop key="hibernate.jdbc.fetch_size">10</prop>                    <prop key="hibernate.jdbc.batch_size">50</prop>                </props>            </property>    </bean>  <!-- Hibernate SessionFactory for MySQL --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingDirectoryLocations"> <list> <value>classpath:/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">10</prop> <prop key="hibernate.jdbc.batch_size">50</prop> </props> </property> </bean> Java代码 500)this.width=500'> <!—define transactionManager -->    <bean id="transactionManager"     class="org.springframework.orm.hibernate3.HibernateTransactionManager">      <property name="sessionFactory">       <ref local="sessionFactory" />      </property>    </bean>  <!—define transactionManager --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> Java代码 500)this.width=500'> <!—business layer -->    <bean id="userManageService"     class="com.test.service.impl.userManageServiceImpl">      <property name="userLoginDao">       <ref bean="userLoginDao" />      </property>    …    </bean>  <!—business layer --> <bean id="userManageService" class="com.test.service.impl.userManageServiceImpl"> <property name="userLoginDao"> <ref bean="userLoginDao" /> </property> … </bean>第一种:使用TransactionProxyFactoryBean,配置声明式事务的方法如下。 (1)表比较少的情况: Java代码 500)this.width=500'> <bean id="userManagerServiceProxy"     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">      <!-- 配置事务管理器 -->      <property name="transactionManager">       <ref bean="transactionManager" />      </property>      <!-- 此属性指定目标类本身是否是代理的对象,如果目标类没有实现任何类,就设为true代表自己 -->      <property name="proxyTargetClass">       <value>false</value>      </property>      <property name="proxyInterfaces">       <value> com.test.service.userManageService</value>      </property>      <!-- 目标bean -->      <property name="target">       <ref bean="userManageService"/>      </property>      <!-- 配置事务属性 -->    <property name="transactionAttributes">       <props>    <prop key="delete*">PROPAGATION_REQUIRED</prop>    <prop key="add*">PROPAGATION_REQUIRED</prop>    <prop key="update*">PROPAGATION_REQUIRED</prop>    <prop key="save*">PROPAGATION_REQUIRED</prop>    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>    </props>    </property>    </bean>  <bean id="userManagerServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 配置事务管理器 --> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 此属性指定目标类本身是否是代理的对象,如果目标类没有实现任何类,就设为true代表自己 --> <property name="proxyTargetClass"> <value>false</value> </property> <property name="proxyInterfaces"> <value> com.test.service.userManageService</value> </property> <!-- 目标bean --> <property name="target"> <ref bean="userManageService"/> </property> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>(2)利用继承的思想简化配置,适合相对比较多的模块时使用。 Java代码 500)this.width=500'>  <bean id="transactionBase"     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"     lazy-init="true" abstract="true">      <!-- 配置事务管理器 -->      <property name="transactionManager">       <ref bean="transactionManager" />      </property>      <!-- 配置事务属性 -->      <property name="transactionAttributes">       <props>    <prop key="delete*">PROPAGATION_REQUIRED</prop>    <prop key="add*">PROPAGATION_REQUIRED</prop>    <prop key="update*">PROPAGATION_REQUIRED</prop>    <prop key="save*">PROPAGATION_REQUIRED</prop>    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>    </props>    </property>    </bean>   <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 配置事务管理器 --> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>而具体的模块可以简单的这样配置。只要指明它的parent(父类)就可以了。父类一般把abstract="true",因为在容器加载的时候不需要初始化,等到用的时候再有它的子类调用的时候,再去初始化。 Java代码 500)this.width=500'> <bean id="userManageServiceProxy" parent="transactionBase" >      <property name="target">      <ref bean="userManageService"/>      </property>    </bean>  <bean id="userManageServiceProxy" parent="transactionBase" > <property name="target"> <ref bean="userManageService"/> </property> </bean>第二种:自动创建事务代理的方式。主要利用BeanNameAutoProxyCreator自动创建事务代理 Java代码 500)this.width=500'>   <!--利用了拦截器的原理。-->      <bean id="transactionInterceptor"     class="org.springframework.transaction.interceptor.TransactionInterceptor">      <property name="transactionManager">       <ref bean="transactionManager" />      </property>      <!-- 配置事务属性 -->      <property name="transactionAttributes">       <props>    <prop key="delete*">PROPAGATION_REQUIRED</prop>    <prop key="add*">PROPAGATION_REQUIRED</prop>    <prop key="update*">PROPAGATION_REQUIRED</prop>    <prop key="save*">PROPAGATION_REQUIRED</prop>    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>    </props>    </property>    </bean>    <bean id="serviceProxy "     class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">     <property name="beanNames">    <list>    <value>userManageService</value>    </list>    <!--或者直接用 <value>*Service</value>-->    </property>      <property name="interceptorNames">       <list>    <value>transactionInterceptor</value>    </list>    </property>    </bean>   <!--利用了拦截器的原理。--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="serviceProxy " class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>userManageService</value> </list> <!--或者直接用 <value>*Service</value>--> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean><2>事务中异常 Java代码 500)this.width=500'> <!-- 配置事务属性 -->    <property name="transactionAttributes">       <props>    <!--指定了 "PROPAGATION_REQUIRED",表示在当前的事务中执行操作,如果事务不存在就建立一个新的-->    <prop key="delete*">PROPAGATION_REQUIRED,-ProgramException</prop>    <prop key="add*">PROPAGATION_REQUIRED,-ProgramException</prop>    <prop key="update*">PROPAGATION_REQUIRED, -ProgramException</prop>    <prop key="save*">PROPAGATION_REQUIRED, -ProgramException</prop>    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>    </props>    </property>  <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <!--指定了 "PROPAGATION_REQUIRED",表示在当前的事务中执行操作,如果事务不存在就建立一个新的--> <prop key="delete*">PROPAGATION_REQUIRED,-ProgramException</prop> <prop key="add*">PROPAGATION_REQUIRED,-ProgramException</prop> <prop key="update*">PROPAGATION_REQUIRED, -ProgramException</prop> <prop key="save*">PROPAGATION_REQUIRED, -ProgramException</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property>Spring中对异常的回滚,默认是在抛出运行时异常(RuntimeException)时才回滚,对非运行时异常不回滚。如果使用-Exception,意思是对所有的异常异常都回滚。Exception前面加上 "-" 时,表示发生指定异常时撤消操作(rollback),如果前面加上 "+",表示发生异常时立即提交(commit)。 要想用Spring的事务管理机制,就需要把数据库的连接交给Spring来管理,(JDBC,SESSION道理一样),如果使用Hibernate框架,要把Session交给Spring管理。在整个Service方法调用中,虽然Sevice调用了多个Dao,但是整个过程中Session只有一个。也就是说你对数据库的DML操作,都会先保存在这个Session中,包括update,insert,delete。当发生异常(这个异常可以是数据库的,也可以是程序的),Spring会把这个Session中对应的DML操作回滚。 <3>事务的属性 (1) 传播行为 PROPAGATION_MANDATORY: 方法必须在一个现存的事务中进行,否则丢出异常 PROPAGATION_NESTED: 在一个嵌入的事务中进行,如果不是,则同PROPAGATION_REQUIRED PROPAGATION_NEVER: 指出不应在事务中进行,如果有就丢出异常 PROPAGATION_NOT_SUPPORTED: 指出不应在事务中进行,如果有就暂停现存的事务 PROPAGATION_REQUIRED: 在当前的事务中进行,如果没有就建立一个新的事务 PROPAGATION_REQUIRES_NEW: 建立一个新的事务,如果现存一个事务就暂停它 PROPAGATION_SUPPORTS: 支持现在的事务,如果没有就以非事务的方式执行 (2) 隔离层级 ISOLATION_DEFAULT: 使用底层数据库预设的隔离层级 ISOLATION_READ_COMMITTED: 允许事务读取其他并行的事务已经送出(Commit)的数据字段,可以防止Dirty read问题 ISOLATION_READ_UNCOMMITTED: 允许事务读取其他并行的事务还没送出的数据,会发生Dirty、Nonrepeatable、Phantom read等问题 ISOLATION_REPEATABLE_READ: 要求多次读取的数据必须相同,除非事务本身更新数据,可防止Dirty、Nonrepeatable read问题 ISOLATION_SERIALIZABLE: 完整的隔离层级,可防止Dirty、Nonrepeatable、Phantom read等问题,会锁定对应的数据表格,因而有效率问题 (3) 只读提示(Read-only hints) 如果事务只进行读取的动作,则可以利用底层数据库在只读操作时发生的一些最佳化动作,由于这个动作利用到数据库在只读的事务操作最佳化,因而必须在事务中才有效,也就是说要搭配传播行为PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED来设置。 (4)事务超时期间(The transaction timeout period) 有的事务操作可能延续很长一段的时间,事务本身可能关联到数据表的锁定,因而长时间的事务操作会有效率上的问题,对于过长的事务操作,考虑Roll back事务并要求重新操作,而不是无限时的等待事务完成。 可以设置事务超时期间,计时是从事务开始时,所以这个设置必须搭配传播行为PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED来设置。 事务的超时属性以timeout_为前缀和一个整型数字定义,例如: <prop key="query*">PROPAGATION_REGUIRED,timeout_5,readOnly</prop>


阅读全文(1327) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.844 second(s), page refreshed 144814822 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号