星空网 > 软件开发 > ASP.net

声明式事务管理详解

 

  spring的事务处理分为两种:

    1、编程式事务:在程序中控制事务开始,执行和提交;(不建议使用,所以这里我就不说明太多)    

    2、声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码;(建议使用)
        我对”声明式“的理解是这样的:Spring配置文件中定义好了这样一个规则,
      这个规则可以指定对哪些类的哪些方法在执行的时候添加事务控制,并配置好了事务的相关执行属性,
      就是在这些类的这些方法执行的时候隐式地添加事务开始、执行、提交或回滚的代码(当然我们看不到)
      声明式事务又分了两种写法:
        2.1、        2.2、注解
   下面我们首先讲解下在<? ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/aop" ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/jee" ="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理 --> <!-- <context:component-scan>: 有一个use-default-filters属性,该属性默认为true, 这就意味着会扫描指定包下的全部的标有注解的类,并注册成bean. 可以发现这种扫描的粒度有点太大,如果你只想扫描指定包下面的Controller, 该怎么办?此时子标签<context:incluce-filter>就起到了勇武之地。如下所示 <context:component-scan base-package="news" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> 如果use-dafault-filters在上面并没有指定,默认就为true, 也就意味着你现在加<context:exclude-filter/>跟没加是一样的 所有你要记住,你若想要用到<context:component-scan>的子标签, 必须要把use-dafault-filters的值改为false 当然还有一个是与之相反的而已这里就不啰嗦了 上面这一对解释换成一句话就是: Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描 <context:component-scan>的base-package属性作用:设置要被扫描的包 --> <!-- (本案例不用到,只是用了一个全盘扫描,以上内容只是为了让大家了解它) --> <context:component-scan base-package="news.."/> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- <tx:annotation-driven transaction-manager="transactionManager"/> --> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <!-- 每300秒检查所有连接池中的空闲连接 --> <property name="idleConnectionTestPeriod" value="300"/> <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 --> <property name="maxIdleTime" value="900"/> <!-- 最大连接数 --> <property name="maxPoolSize" value="2"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.connection.autocommit">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingResources"> <list> <value>news/entity/News.hbm.class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!-- 创建事务管理器, 管理sessionFactory(因为所有的session都是从sessionFactory获取的) --> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置通知, 那些方法需要切入什么类型的事务 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置切面表达式, 并且让 tx与切面表达式合二为一 --> <aop:config> <!-- 表达式, 定义哪个包的哪些类需要切入事务,但是此处并且没有制定类中哪些方法,需要切入什么样 事务 --> <aop:pointcut expression="execution(* news.service.*.*(..))" id="pointcut" /> <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/> </aop:config></beans>

   做到这里

    声明式事务管理详解

    如果没有这些包的朋友们可以去一下网址下载:http://pan.baidu.com/s/1eSlyY1G

 注意:
在以下情况中spring的事务管理会失效: ● private 方法无法添加事务管理. ● final 方法无法添加事务管理. ● static 方法无法添加事务管理. ● 当绕过代理对象, 直接调用添加事务管理的方法时, 事务管理将无法生效.


 



原标题:声明式事务管理详解

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流