java spring 事务管理器

spring框架有自己的事务管理机制,spring使用DataSourceTransactionManager作为Jdbc的事务管理器,同时把被管理的对象使用TransactionProxyFactoryBean配置,TransactionProxyFactoryBean是个事务代理的Bean,能够使用IOC、AOP等注入事务管理代码


demo代码使用https://www.yeetrack.com/post/2013-03-27/spring%20DAO%E6%A8%A1%E5%9D%97%E6%94%AF%E6%8C%81jdbc这里的代码,新建TransactionRun.java文件:

package com.yeetrack.springDAO;

import java.util.Date;

/**
 *
 * @author youthflies
 *
 */
public class TransactionRun
{

    private IPersonDAO personDao;

    public IPersonDAO getPersonDao()
    {
        return personDao;
    }

    public void setPersonDao(IPersonDAO personDao)
    {
        this.personDao = personDao;
    }

    /**
     * runsql中的代码与TestDao中的一样,不过其中getPersonName()方法会报异常
     * 如果没有事务控制,personDao.addPerson()会执行成功
     * 如果有事务控制器,因为runSql中爆出异常,整个事务都会回滚
     */
    public void run()
    {
        Person person =new Person();
        person.setName("youthflies--1");
        person.setAge(22);
        person.setSex("M");
        person.setBirthday(new Date());
        personDao.addPerson(person);

        System.out.println(personDao.getPersonName(1212312312389));//这里因为异常,会导致整个事务执行失败

        System.out.println(personDao.getPersionCount());

        System.out.println(personDao.listPerson());
    }

}

在applicationContext.xml中配置事务:

<!-- jdbc 事务管理器 -->
    <bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 为方法配置事务 -->
    <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        <property name="properties">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>   <!-- 事务类型,如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。 -->
            </props>
        </property>
    </bean>

    <!-- 使用proxy,将事务管理注入到目标方法中 -->
    <bean id="transactionRun" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="jdbcTransactionManager"></property>
        <property name="target">
            <bean class="com.yeetrack.springDAO.TransactionRun">
                <property name="personDao" ref="personDao"></property>
            </bean>
        </property>
        <property name="transactionAttributeSource" ref="transactionAttributeSource"></property>
    </bean>

spring会在指定方法执行前开启事务,在指定方法结束后提交事务,如果中间有异常,事务会回滚。
下面是测试代码:

package com.yeetrack.springDAO;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestTransction
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        TransactionRun transaction = (TransactionRun) context.getBean("transactionRun");
        transaction.run();

    }

}
版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=4