spring事务

Posted fight139

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring事务相关的知识,希望对你有一定的参考价值。

1.导入jar包

spring-tx...jar

2.三个顶级接口

2.1 PlatformTransactionManager:平台事务管理器,只要管理事务必须使用事务管理器

  进行事务配置时,必须配置事务管理器

  导入jar包:需要使用实现类,jdbc..jar  ,  orm...jar

public interface PlatformTransactionManager {
//通过事务详情获取事务状态,从而管理事务 TransactionStatus getTransaction(TransactionDefinition var1)
throws TransactionException; //根据状态提交 void commit(TransactionStatus var1) throws TransactionException; //根据状态回滚 void rollback(TransactionStatus var1) throws TransactionException; }

 

 

 

 

2.2TransactionDefinition:事务定义、事务详情、事务属性。用于确定具体事务,例如:隔离级别、是否只读、超时时间

  进行事务配置时,必须配置事务详情。spring将配置项封装到对象实例中。

 

 传播行为:两个业务如何共享事务

PROPAGATION_REQUIRED , required , 必须  【默认值】

       支持当前事务,A如果有事务,B将使用该事务。

       如果A没有事务,B将创建一个新的事务。

PROPAGATION_SUPPORTS ,supports ,支持

       支持当前事务,A如果有事务,B将使用该事务。

       如果A没有事务,B将以非事务执行。

PROPAGATION_MANDATORY,mandatory ,强制

       支持当前事务,A如果有事务,B将使用该事务。

       如果A没有事务,B将抛异常。

PROPAGATION_REQUIRES_NEW , requires_new ,必须新的

       如果A有事务,将A的事务挂起,B创建一个新的事务

       如果A没有事务,B创建一个新的事务

PROPAGATION_NOT_SUPPORTED ,not_supported ,不支持

       如果A有事务,将A的事务挂起,B将以非事务执行

       如果A没有事务,B将以非事务执行

PROPAGATION_NEVER ,never,从不

       如果A有事务,B将抛异常

       如果A没有事务,B将以非事务执行

PROPAGATION_NESTED ,nested ,嵌套

       A和B底层采用保存点机制,形成嵌套事务。

 

掌握:PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED 

 

 

2.3 TransactionStatus:事务状态,记录当前事务状态。例如:是否有保存点,事务是否已经完成

  spring底层根据具体状态进行操作

 3 事务管理:转账

 3.1 搭建环境

  导入jar包:

核心:4+1

aop:4 (aop联盟[规范],spring-aop【实现】,aspectJ规范,spring-aspectJ【实现】

数据库:jdbc/tx

驱动:mysql

连接池:c3p0

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123"></property>
</bean>
<bean id="accountDao" class="dao.AccountDaoImp">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountService" class="service.AccountServiceImp">
    <property name="accountDao" ref="accountDao"></property>
</bean>
AccountDao
public class AccountDaoImp extends JdbcDaoSupport implements AccountDao {
    @Override
    public void in(Account account, int money) {
        this.getJdbcTemplate().update("UPDATE account set balance=balance+? where id = ?",money,account.getId());
    }

    @Override
    public void out(Account account, int money) {
        this.getJdbcTemplate().update("UPDATE account set balance=balance-? where id = ?",money,account.getId());
    }
    @Test
    public void test(){
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
        System.out.println(accountDao);
        accountDao.in(new Account(1,"",12),100);
    }
}

 

public class AccountServiceImp implements AccountService {
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(Account in, Account out, int money) {
        accountDao.out(out,money);
//        int i = 1/0;
        accountDao.in(in,money);
    }
    @Test
    public void test(){
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        AccountService accountService = (AccountService) applicationContext.getBean("accountService");
        System.out.println(accountService);
        accountService.transfer(new Account(2,"",10),new Account(1,"",10),500);
    }
}
AccountDao

 

 

3.2 手动管理事务管理器(了解)

   spring 底层使用TransactionTemplate事务模板进行操作

  (1)service需要获得并使用TransactionTemplate模板

  (2)spring配置模板,并注入给service

   (3)模板需要注入事务管理器

  (4)配置事务管理器:DataSourceTransactionTemplate

 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123"></property>
</bean>
<bean id="accountDao" class="dao.AccountDaoImp">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountService" class="service.AccountServiceImp">
    <property name="accountDao" ref="accountDao"></property>
    <property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
<!--配置模板-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"></property>
</bean>
<!--配置事务管理器,需要事务,而事务从连接池中获得-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
applicationContext.xml

 

 

public class AccountServiceImp implements AccountService {
    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;

    //需要spring注入事务模板
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(final Account in,final Account out,final int money) {
        try{
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    accountDao.out(out,money);
//                    int i = 1/0;
                    accountDao.in(in,money);
                }
            });
        }catch (Exception e){}
    }
AccountServiceImp.java

 

 

3.3 工厂bean生成代理:半自动

配置bean:

  service 代理对象

  • proxyInterfaces 接口
  • target 目标类
  •  transactionManager 事务管理器
  • transactionAttributes 事务属性(事务详情)

prop.key :确定哪些方法使用当前事务配置
prop.text:用于配置事务详情
格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
传播行为 隔离级别 是否只读 异常回滚 异常提交
例如:
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> 默认传播行为,和隔离级别
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly</prop> 只读
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop> 有异常扔提交

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
    <bean id="accountDao" class="b_transaction_proxy_factoryBean.dao.AccountDaoImp">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="accountService" class="b_transaction_proxy_factoryBean.service.AccountServiceImp">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!--配置生成事务代理对象的工厂bean-->
    <bean id="proxyAccountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="proxyInterfaces" value="b_transaction_proxy_factoryBean.service.AccountService"></property>
        <property name="target" ref="accountService"></property>
        <!--配置事务管理器-->
        <property name="transactionManager" ref="txManager"></property>
        <!--配置事务详情(属性)-->
        <property name="transactionAttributes">
            <props>
                <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
            </props>
        </property>
    </bean>
    <!--配置事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
applicationContext.xml

 

public class AccountServiceImp implements AccountService {
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(Account in, Account out, int money) {
        accountDao.out(out, money);
        int i = 1 / 0;
        accountDao.in(in, money);
    }

}
AccountServiceImp.java

 

 

 

 

3.4 AOP配置基于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: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">
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
    <bean id="accountDao" class="c_transaction_aop.dao.impl.AccountDaoImp">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="accountService" class="c_transaction_aop.service.impl.AccountServiceImp">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!--配置事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置通知(Advice),事务详情
        在aop筛选的基础上,确定具体方法使用什么事务。例如:AC只读,B读写
            propagation:传播行为
    -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!--配置事务详情的属性-->
        <tx:attributes>
            <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--配置生成事务代理对象的工厂bean-->
    <!--aop方式配置代理对象-->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="pc" expression="execution(* c_transaction_aop.service.impl.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"></aop:advisor>
    </aop:config>

</beans>
applicationContext.xml

 

 

 

 

3.5 AOP配置基于注解

 1.配置事务管理器,将事务交给spring

 2.在目标类或方法前加@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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       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="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
    <bean id="accountDao" class="d_transaction_aop_annotation.dao.impl.AccountDaoImp">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--将事务管理器交给spring
        transaction-manager:配置事务管理器
        proxy-target-class:强制使用cglib
        -->
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />

    <!--扫描注解包-->
    <context:component-scan base-package="d_transaction_aop_annotation"></context:component-scan>
</beans>
applicationContex.xml

 

Service("accountService")
//配置详情(通知)
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class AccountServiceImp implements AccountService {
    @Autowired
    @Qualifier("accountDao")
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(Account in, Account out, int money) {
        accountDao.out(out,money);
        int i = 1/0;
        accountDao.in(in,money);
    }
}

 

以上是关于spring事务的主要内容,如果未能解决你的问题,请参考以下文章

BottomNavigationView 滞后于片段事务

理解片段事务期间片段的生命周期方法调用

提交带有全屏片段的片段事务

使用 OnItemClickListener 列出视图片段到片段事务

spring中声明式事务 配置好后,在java代码中怎么使用

阶段3 2.Spring_10.Spring中事务控制_5 spring事务控制的代码准备