码迷,mamicode.com
首页 > 编程语言 > 详细

spring 事务

时间:2017-10-23 21:48:39      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:连接   text   val   common   nbsp   xmlns   特定   ssi   point   

spring 事务管理有两种方式

1.声明式事务管理,在要管理的方法上添加@Transactional  写在service 的实现类或者dao层都可以

2.配置式事务管理,在要管理的方法前后织入事务通知

 

spring 配置文件 spring_coer.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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop   
                         http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
                         http://www.springframework.org/schema/tx 
                         http://www.springframework.org/schema/tx/spring-tx.xsd">
                         <!--Spring 整合mybaits-->
<!-- 加载配置文件 -->
        <bean id="loadProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:db.properties</value>
                    </list>
                </property>
        </bean>

    <!--配置注解扫描 use-default-filters 是扫描包括component 下的子注解 @service 等 -->
    <context:component-scan base-package="com.ssh"></context:component-scan>
    <!--sping 整合hibernate -->

  
    <!--定义basicDataSource数据源 -->
    <!-- org.apache.commons.dbcp2.datasources -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!-- 指定连接数据库的驱动 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <!-- 指定数据库所用的url -->
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/ssm"></property>
        <!--指定连接的用户名 -->
        <property name="user" value="root"></property>
        <!-- 指定连接的密码 -->
        <property name="password" value="123456"></property>
        <!-- 定义hibernate 的sessionFactory -->
    </bean>
 <!--                              方式一   声明式事务管理                                                    -->
    <tx:annotation-driven transaction-manager="transactionManager"/>



<!-- spring 整合mybaits --> <!-- sprig 托管sqlSessionFactoryBean 作用:创建会话连接--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 加载mybaties 配置文件 --> <property name="configLocation" value="classpath:mybatisConfig.xml"/> <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件--> <!-- <property name="mapperLocations" value="classpath:com/ssh/sqlmap/*Mapper.xml" /> --> </bean> <!-- spring 注入模板工具类--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> </bean>

<!--                                              方式二,配置事务管理                                                                               -->
     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


<!-- 定义事务的通知 --> 
  <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 指定传播规则 -->

      <tx:attributes> <tx:method name="cre*" read-only="false" propagation="REQUIRED"/>

      <tx:method name="ins*" read-only="false" propagation="REQUIRED"/>

      <tx:method name="upd*" read-only="false" propagation="REQUIRED"/>

      <tx:method name="del*" read-only="false" propagation="REQUIRED"/>

      <tx:method name="*" isolation="DEFAULT" read-only="true" />

  </tx:attributes>

</tx:advice>


<!-- 定义一个切面 -->
<aop:config>
<!--指定切入的位置-->
      <!-- 切入点:第一个* 代表类型 public private protect 要有空格 切入点表达式的使用规则:
      execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
      有“?”号的部分表示可省略的,modifers-pattern表示修饰符如public、protected等,
      ret-type-pattern表示方法返回类型, declaring-type-pattern代表特定的类,
      name-pattern代表方法名称, param-pattern表示参数,
      throws-pattern表示抛出的异常。在切入点表达式中,可以使用*来代表任意字符,用..来表示任意个参数
-->

  <aop:pointcut id="bizMethod" expression="execution(* com.ssh.dao.*.*(..))"/>

      <!--将通知和切点编织在一起-->

       <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethod"/>

  </aop:config>

</beans>

方式一或者方式二选择其中一种

1.采用声明的方式管理事务

@Transactional(添加事务的传播策略和隔离方式)
public void transfer(HashMap<String, Object> param) {
// TODO Auto-generated method stub
studentDao.updateOutMoney(param);
studentDao.updateInMoney(param);
}

 

方式二:只要将要调用事务的方法申明为 ins*/upd*/cre*/del*   为前缀的方法即可

 

有一种报错的情况:用方式二的调用方法没有用通知配置的前缀的时候,事务不会提交

 

spring 事务

标签:连接   text   val   common   nbsp   xmlns   特定   ssi   point   

原文地址:http://www.cnblogs.com/blogxiao/p/7718891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!