spring学习系列 --- 事务管理

1、事务管理 ---- 主要是保证事务运行过程中,输入的数据不出现问题

在开发过程中,就是可能会出现程序运行中断,原因可能是程序bug,一块程序想实现一个完整的功能,要是无法全部完成,反倒是会造成错误,所以希望这块代码要么是执行完成,要么就是全部没有执行,即出现执行部分的情况,出现代码回滚。

事务是一系列的动作,它们综合在一起才是一个完整的工作单元,这些动作必须全部完成,如果有一个失败的话,那么事务就会回滚到最开始的状态,仿佛什么都没发生过一样。 

2、事务的4个特性

  • 原子性(Atomicity):事务是一个原子操作,由一系列动作组成。事务的原子性确保动作要么全部完成,要么完全不起作用。
  • 一致性(Consistency):一旦事务完成(不管成功还是失败),系统必须确保它所建模的业务处于一致的状态,而不会是部分完成部分失败。在现实中的数据不应该被破坏。
  • 隔离性(Isolation):可能有许多事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏。
  • 持久性(Durability):一旦事务完成,无论发生什么系统错误,它的结果都不应该受到影响,这样就能从任何系统崩溃中恢复过来。通常情况下,事务的结果被写到持久化存储器中。

3、需要配置事务

xmlns:tx  可以和切面编程 AOP 结合起来,达到好的效果,这样就可以不用将事务的代码封装到业务代码里面去

4、大致需要配置如下

配置数据库、配置jdbc事务管理器、配置事务通知、配置事务切面    ---- 这个是 xml配置申明事务管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


    <!--数据源配置-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- jdbc事务管理器;注入数据源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>  
            <!--<tx:method name="insert*" propagation="REQUIRED" />  -->
            <!--<tx:method name="update*" propagation="REQUIRED" />  -->
            <!--<tx:method name="edit*" propagation="REQUIRED" />  -->
            <!--<tx:method name="save*" propagation="REQUIRED" />  -->
            <!--<tx:method name="add*" propagation="REQUIRED" />  -->
            <!--<tx:method name="new*" propagation="REQUIRED" />  -->
            <!--<tx:method name="set*" propagation="REQUIRED" />  -->
            <!--<tx:method name="remove*" propagation="REQUIRED" />  -->
            <!--<tx:method name="delete*" propagation="REQUIRED" />  -->
            <!--<tx:method name="change*" propagation="REQUIRED" />  -->
            <!--<tx:method name="get*" propagation="REQUIRED" read-only="true" />  -->
            <!--<tx:method name="find*" propagation="REQUIRED" read-only="true" />  -->
            <!--<tx:method name="load*" propagation="REQUIRED" read-only="true" />  -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>  
    </tx:advice>

    
    <!-- 配置事务切面 -->
    <aop:config>
    	<!-- 配置切点 -->
    	<aop:pointcut id="serviceMethod" expression="execution(* com.java1234.service.*.*(..))" />
    	<!-- 配置事务通知 -->
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>
     
	<context:property-placeholder location="jdbc.properties"/>


    <!--配置 jdbcTemplate 模版,这个是可以对 数据库进行操作的工具,类似数据库的查询、插入、删除之类的操作-->
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    	<constructor-arg ref="dataSource"></constructor-arg>
    </bean>
	
	
	<bean id="bankDao" class="com.java1234.dao.impl.BankDaoImpl">
		<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
	</bean> 
	
	<bean id="bankService" class="com.java1234.service.impl.BankServiceImpl">
		<property name="bankDao" ref="bankDao"></property>
	</bean> 
	
</beans>

5、注解式事务管理器    

@Transactional  在类或者方法上,注释一下就可以实现事务管理器的功能
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


    <!--数据源配置-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- jdbc事务管理器;注入数据源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!--&lt;!&ndash; 配置事务通知 &ndash;&gt;-->
    <!--<tx:advice id="txAdvice" transaction-manager="transactionManager">-->
    	<!--<tx:attributes>  -->
            <!--&lt;!&ndash;<tx:method name="insert*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="update*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="edit*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="save*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="add*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="new*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="set*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="remove*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="delete*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="change*" propagation="REQUIRED" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="get*" propagation="REQUIRED" read-only="true" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="find*" propagation="REQUIRED" read-only="true" />  &ndash;&gt;-->
            <!--&lt;!&ndash;<tx:method name="load*" propagation="REQUIRED" read-only="true" />  &ndash;&gt;-->
            <!--<tx:method name="*" propagation="REQUIRED" />-->
        <!--</tx:attributes>  -->
    <!--</tx:advice>-->

    
    <!--&lt;!&ndash; 配置事务切面 &ndash;&gt;-->
    <!--<aop:config>-->
    	<!--&lt;!&ndash; 配置切点 &ndash;&gt;-->
    	<!--<aop:pointcut id="serviceMethod" expression="execution(* com.java1234.service.*.*(..))" />-->
    	<!--&lt;!&ndash; 配置事务通知 &ndash;&gt;-->
    	<!--<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>-->
    <!--</aop:config>-->
     <!---->
	<context:property-placeholder location="jdbc.properties"/>


    <!--配置 jdbcTemplate 模版,这个是可以对 数据库进行操作的工具,类似数据库的查询、插入、删除之类的操作-->
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    	<constructor-arg ref="dataSource"></constructor-arg>
    </bean>
	
	
	<bean id="bankDao" class="com.java1234.dao.impl.BankDaoImpl">
		<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
	</bean> 
	
	<bean id="bankService" class="com.java1234.service.impl.BankServiceImpl">
		<property name="bankDao" ref="bankDao"></property>
	</bean> 
	
</beans>
扫描二维码关注公众号,回复: 3450300 查看本文章

猜你喜欢

转载自blog.csdn.net/hwang4_12/article/details/82951013
今日推荐