37.Spring-事务控制.md


目录

1.分类

事务控制的方式:

  • 编程式事务控制:编程灵活,但是开发繁琐,每次都要开启、回滚等操作

    • jdbc:Conn.setAutoCommite(false); //设置手动事务控制
    • hibernate:Session.beginTransaction(); //开启事务
  • 声明式事务控制:使用配置文件实现,使用时添加配置,不需要时候移除配置即可。核心是aop,所以控制粒度在方法。也就是对方法来应用事务,不能对方法中的某几行代码实现。对于事务的控制应该放到Server层来处理。

    • jdbc:DataSourceTransactionManager

    • hibernate:


2.Spring对jadc事务管理

2.1xml方式

2.1.1首先定义Dao对象和Server对象

package per.liyue.spring.jdbc_trancation_xml;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by liyue on 2016/11/30.
 */
public class RoleDao
{
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(String sql)
    {
        jdbcTemplate.update(sql);
    }
}
package per.liyue.spring.jdbc_trancation_xml;

/**
 * Created by liyue on 2016/11/30.
 */
public class RoleService
{
    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao)
    {
        this.roleDao = roleDao;
    }

    public void save(String sql)
    {
        roleDao.save(sql);
        int i = 10 / 0;//这里执行失败要回滚成功的操作       
        roleDao.save(sql);
    }
}

2.1.2配置文件实现事务管理

配置中要注意:

  • 配置文件中对于事务的命名空间(xmlns:tx="http://www.springframework.org/schema/tx")要引入
  • 对于jdbc来说核心是通过org.springframework.jdbc.datasource.DataSourceTransactionManager来实现事务控制
  • 对于DataSourceTransactionManager的实现还需要配置tx:advice来指明对于哪些方法有哪些权限的操作
  • 对于拦截方法可以用*来模糊匹配
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd">    <!--1.jdbc的基本配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hi"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="roleDao" class="per.liyue.spring.jdbc_trancation_xml.RoleDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="roleService" class="per.liyue.spring.jdbc_trancation_xml.RoleService">
        <property name="roleDao" ref="roleDao"></property>
    </bean>    <!--2.事务配置-->    <!--2.1事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>    <!--2.2事务控制          transaction-manager:jdbc的事务管理器            method:配置需要管理的方法和属性,这里可以用*来模糊匹配    -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" read-only="false"/>
        </tx:attributes>
    </tx:advice>    <!--2.3事务AOP          aop:advisor:指定事务控制和切入点    -->
    <aop:config>
        <aop:pointcut id="pt"
                      expression="execution(* per.liyue.spring.jdbc_trancation_xml.RoleService.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>
</beans>

2.2注解方式

注解方式实现相对简单很多!
需要改动的点:

  • 开启注解
  • 指定注解的事务管理类
  • 对各个类增加注解(@Repository,@Service,@Resource)
  • 对于需要事务管理的位置增加注解@Transactional:
    • 对方法增加:对方法实现事务管理

    • 对类增加:对类实现事务管理

2.2.1对象类

package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by liyue on 2016/11/30.
 */
@Repositorypublic
class RoleDao
{
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(String sql)
    {
        jdbcTemplate.update(sql);
    }
}
package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by liyue on 2016/11/30.
 */
@Servicepublic
class RoleService
{
    @Resource
    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao)
    {
        this.roleDao = roleDao;
    }

    @Transactional
    public void save(String sql)
    {
        roleDao.save(sql);
        int i = 10 / 0;//这里执行失败要回滚成功的操
        roleDao.save(sql);
    }
}

2.2.2配置文件

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd">    <!--1.jdbc的基本配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hi"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="roleDao" class="per.liyue.spring.jdbc_trancation_annotation.RoleDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="roleService" class="per.liyue.spring.jdbc_trancation_annotation.RoleService">
        <property name="roleDao" ref="roleDao"></property>
    </bean>    <!--2.事务配置-->    <!--2.1事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>    <!--2.2开启注解扫描-->
    <context:component-scan
            base-package="per.liyue.spring.jdbc_trancation_annotation"></context:component-scan>    <!--2.3开启注解事务模式-->
    <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
</beans>

2.3事务的属性

事务对于Dao的操作,这里举例如:

package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by liyue on 2016/11/30.
 */
@Servicepublic
class RoleService
{
    @Resource
    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao)
    {
        this.roleDao = roleDao;
    }

    @Transactional(readOnly = false,                           //只读   
            timeout = -1,                               //超时,但是最终是由底层数据库来决定  
            noRollbackFor = ArithmeticException.class,  //对于指定异常不执行回滚操作,这里的示例对于数学异常不会滚    )   
            public void save(String sql)
    {
        roleDao.save(sql);
        int i = 10 / 0;//这里执行失败要回滚成功的操作   
        roleDao.save(sql);
    }
}

propagation:

isolation:


3.

原文地址:https://www.cnblogs.com/bugstar/p/8513466.html