SSM框架-事务管理

本文介绍了事务的概念,包括原子性、一致性和隔离性等特性。详细讲述了Spring框架提供的事务管理方式,包括XML配置和注解方式,并解析了配置文件中的相关术语。还展示了如何在类或方法中使用@Transactional注解来实现事务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是事务?

.事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消。也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做。

.事务的结束有两种,当事务中的所以步骤全部成功执行时,事务提交。如果其中一个步骤失败,将发生回滚操作,撤消撤消之前到事务开始时的所以操作。

2.事务的四大特性

1)    原子型 (一个事务中的多个操作要么都成功要么都失败)

2)    一致性 (例如存钱操作,存之前和存之前的钱数应该是一致的)

3)    隔离性 (事务与事务应该是相互隔离的)

4)    持久性 (事务一旦提交,数据要持久保存)


Spring提供了两种事务管理方式, 编程式事务和声明式事务。

    编程式事务指的是通过编码方式实现事务;

    声明式事务基于 AOP,将具体业务逻辑与事务处理解耦。声明式事务管理使业务代码逻辑不受污染

3.在spring配置文件中的配置(基于XML方式)
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
       
    <!-- 自动扫描该包 -->
    <context:component-scan base-package="com.jt" />
    <!-- 启用mvc注解 -->
    <mvc:annotation-driven />
    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html"></property>
    </bean>  
    <!-- 整合DRUID -->
    <util:properties id="cfg" location="classpath:config.properties"/>
    <!--配置DruidDataSource连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close" init-method="init" lazy-init="true">
		<property name="driverClassName" value="#{cfg.driver}" />
		<property name="url" value="#{cfg.url}" />
		<property name="username" value="#{cfg.username}" />
		<property name="password" value="#{cfg.password}" />
    </bean>
    <!-- 整合mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	   <property name="dataSource" ref="dataSource" />
	   <property name="configLocation" value="classpath:mybatis-configs.xml"></property>
	   <!-- 自动扫描mapping.xml文件 -->
	   <property name="mapperLocations" >
			<list><value>classpath:mapper/*.xml</value></list>
	   </property>
	</bean>
    <!-- 扫描DAO接口所在包,Spring会自动查找其下的dao
                  接口,然后为接口创建代理对象-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage"
		          value="com.jt.**.dao"/>
	</bean>
	
	<!-- 配置spring事务管理 -->
	<!--1)定义事务管理对象(可以将此对象理解为切面) -->
	<bean id="txManager"
	      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	      <property name="dataSource" ref="dataSource"/>
	</bean>
    <!-- 2)配置事务策略 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
       <tx:attributes>
       	   <!--定义查询方法都是只读的 -->
           <tx:method name="find*" read-only="true" isolation="READ_COMMITTED"/>
           <!-- 主库执行操作,事务传播行为定义为默认行为 -->
           <tx:method name="save*" propagation="REQUIRED"/>
           <tx:method name="update*" propagation="REQUIRED"/>
           <tx:method name="delete*" propagation="REQUIRED"/>
           <tx:method name="valid*" propagation="REQUIRED"/>
           <!--其他方法使用默认事务策略 -->
           <tx:method name="*"/>
       </tx:attributes>
    </tx:advice>
    <!-- 3)配置事务应用(在哪些业务对象的哪些方法上使用事务) -->
    <aop:config>
    	<!-- 定义切面,所有的service的所有方法 -->
       <aop:pointcut expression="execution(* com.jt.sys.service..*.*(..))"
                     id="txPointcut"/>
        <!-- 应用事务策略到Service切面 -->
       <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>          
    </aop:config>

    
	<!--=============================下面是shiro多realm认证=============================-->
    <!-- 配置shiro框架-->
	<!-- 配置realm对象(将给spring管理) -->
	<bean id="userRealm" 
	        class="com.jt.sys.service.realm.ShiroUserRealm">
	    <!-- 凭证匹配器(密码加密) -->
	    <property name="credentialsMatcher">
    		<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    			<property name="hashAlgorithmName" value="MD5"/>
    			<!-- <property name="hashIterations" value="1024"/> -->
    		</bean>
    	</property>
	</bean>
	<!-- 配置CacheManager对象(不是必须的,主要是为了提高性能,可以对认证信息以及授权信息进行缓存) -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one.  If not, a new one
             will be creaed with a default config:
             <property name="cacheManager" ref="ehCacheManager"/> -->
        <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want
             a specific Ehcache configuration to be used, specify that here.  If you don't, a default
             will be used.: -->
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
    </bean>
	<!-- 配置securityManager对象(此对象时shiro框架核心) -->
	<bean id="securityManager" 
	   class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
	    <property name="realm" ref="userRealm"/> 
	    <property name="cacheManager" ref="cacheManager"/>  
	</bean>
	<!-- 配置ShiroFilter(通过此filter的配置实现对请求资源的过滤,哪些请求要放行,哪些要认证)-->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
         <!-- shiro的核心安全接口 -->
         <property name="securityManager" ref="securityManager"/>
         <!-- 要求登录时的连接 -->
         <property name="loginUrl" value="/loginUI.do"></property>
         <!-- 登录成功后要跳转的连接(此处已经在登录中处理了) -->
         <!-- <property name="successUrl" value="/index.jsp"></property> -->
         <!-- 访问未对其授权的资源时,要跳转的连接 
         <property name="unauthorizedUrl" value="/default.html"></property>-->
         <!-- shiro连接约束配置 -->
         <property name="filterChainDefinitions">
             <value>
                 <!-- 对静态资源设置允许匿名访问 -->
                 /bower_components/** = anon
                 /build/** = anon
                 /dist/** = anon
                 /plugins/** = anon
                 /doLogin.do = anon
                 <!-- 退出 -->
                 /doLogout.do = logout  <!-- 会调用Subject的logout方法,此方法会将session清空 -->
                 <!-- 剩余其他路径,必须认证通过才可以访问 -->
                 /** = authc
             </value>
         </property>
     </bean>
     
    <!--Shiro生命周期处理器-->
	<bean id="lifecycleBeanPostProcessor" 
	class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
	
	<!--启用shiro注解权限检查(@RequestPermissions)-->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
	    depends-on="lifecycleBeanPostProcessor"/>
	<bean  class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
	    <property name="securityManager" ref="securityManager"/>
	</bean>
	<aop:aspectj-autoproxy/>
</beans>
4.基于注解方式实现
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
</bean>
<!--设置注解驱动的事务管理  -->
<tx:annotation-driven transaction-manager="txManager"/>

5.配置中的单词解释

name	当在配置文件中有多个 TransactionManager , 可以用该属性指定选择哪个事务管理器。
propagation	事务的传播行为,默认值为 REQUIRED。
isolation	事务的隔离度,默认值采用 DEFAULT。
timeout	事务的超时时间,默认值为-1。如果超过该时间限制但事务还没有完成,则自动回滚事务。
read-only	指定事务是否为只读事务,默认值为 false;为了忽略那些不需要事务的方法,比如读取数据,可以设置 read-only 为 true。
rollback-for	用于指定能够触发事务回滚的异常类型,如果有多个异常类型需要指定,各类型之间可以通过逗号分隔。
no-rollback- for	抛出 no-rollback-for 指定的异常类型,不回滚事务。
6.在类或方法中使用@Transaction注解应用事务。
@Transactional注解可以用在方法上也可以添加到类级别上。当把@Transactional 注解放在类级别时,表示所有该类的公共方法都配置相同的事务属性信息。见清单 2,EmployeeService 的所有方法都支持事务并且是只读。当类级别配置了@Transactional,方法级别也配置了@Transactional,应用程序会以方法级别的事务属性信息来管理事务,换言之,方法级别的事务属性信息会覆盖类级别的相关配置信息。

基础配置资源,完美分开
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值