美文网首页
Spring简单实现事务操作

Spring简单实现事务操作

作者: dillqq | 来源:发表于2019-11-10 13:39 被阅读0次

主配置文件

<?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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com"/>



<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
</bean>

  <!--    配置数据源-->
<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--        连接数据库的必备信息-->

    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql:///db1"></property>
    <property name="user" value="root"></property>
    <property name="password" value="zheng"></property>
</bean>

<!--    配置Connection的工具类-->
<bean id="connectionUtils"  class="com.utils.ConnectionUtils">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!--    事务管理器-->
<bean id="transManager" class="com.utils.TransactionManager">
    <property name="connectionUtils" ref="connectionUtils"></property>
</bean>


</beans>

单连接类

public class ConnectionUtils {
private ThreadLocal<Connection> t1 = new ThreadLocal<Connection>();

private DataSource dataSource;

public DataSource getDataSource() {
    return dataSource;
}

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

public Connection getConnection()
{
    Connection connection = t1.get();
    if (connection == null)
    {
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        t1.set(connection);

    }
    return connection;
}

public void remove()
{
    t1.remove();
}
}

管理连接的事务类

  public class TransactionManager {

private ConnectionUtils connectionUtils;

public ConnectionUtils getConnectionUtils() {
    return connectionUtils;
}

public void setConnectionUtils(ConnectionUtils connectionUtils) {
    this.connectionUtils = connectionUtils;
}

public void beginTransaction(){
    try {
        connectionUtils.getConnection().setAutoCommit(false);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void CommitTransaction(){
    try {
        connectionUtils.getConnection().commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void RollTransaction(){
    try {
        connectionUtils.getConnection().rollback();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
public void release(){
    try {
        connectionUtils.getConnection().close();
        connectionUtils.remove();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

在业务层中加入事务控制

  @Service("accountService")
public class IAccountService implements IAccountAervice {
@Autowired
private IAccountDao  dao;
@Autowired
private TransactionManager transactionManager;


public TransactionManager getTransactionManager() {
    return transactionManager;
}

public void setTransactionManager(TransactionManager transactionManager) {
    this.transactionManager = transactionManager;
}

public IAccountDao getDao() {
    return dao;
}

public void setDao(IAccountDao dao) {
    this.dao = dao;
}

public List<Account> findAllAccount() {
    try{
        transactionManager.beginTransaction();
        List<Account> list =  dao.findAllAccount();
        transactionManager.CommitTransaction();
        return list;
    }catch (Exception e){
        transactionManager.RollTransaction();
        throw  new RuntimeException();
    }finally {
        transactionManager.release();
    }
}

public Account findById(Integer i) {
    try{
        transactionManager.beginTransaction();
        Account list =  dao.findById(i);
        transactionManager.CommitTransaction();
        return list;
    }catch (Exception e){
        transactionManager.RollTransaction();
        throw new RuntimeException();
    }finally {
        transactionManager.release();
    }
}

public void saveAccount(Account account) {
    try{
        transactionManager.beginTransaction();
         dao.saveAccount(account);
        transactionManager.CommitTransaction();
    }catch (Exception e){
        transactionManager.RollTransaction();
    }finally {
        transactionManager.release();
    }
}

public void updateAccount(Account account) {
    try{
        transactionManager.beginTransaction();
        dao.updateAccount(account);
        transactionManager.CommitTransaction();
    }catch (Exception e){
        transactionManager.RollTransaction();
    }finally {
        transactionManager.release();
    }
}

public void deleteAccount(Integer i) {
    try{
        transactionManager.beginTransaction();
        dao.deleteAccount(i);
        transactionManager.CommitTransaction();
    }catch (Exception e){
        transactionManager.RollTransaction();
    }finally {
        transactionManager.release();
    }
}

public void transfer(String sourceName, String targetName, Float money) throws Exception {

    try{
        transactionManager.beginTransaction();
        Account source = dao.findByName(sourceName);
        Account  target = dao.findByName(targetName);
        source.setMoney(source.getMoney() - money);
        target.setMoney(target.getMoney() +money);
        dao.updateAccount(source);
        int i = 2/0;
        dao.updateAccount(target);
        transactionManager.CommitTransaction();
    }catch (Exception e){
        transactionManager.RollTransaction();
        throw new RuntimeException("事务出错");
    }finally {
        transactionManager.release();
    }

}
}

改变持久层,将单线程中的连接注入QueryRunner方法中

  @Repository("accuntDaoImp")
  public class IAccuntDaoImp implements IAccountDao {
@Autowired
private QueryRunner runner;
@Autowired
private ConnectionUtils connectionUtils;

public QueryRunner getRunner() {
    return runner;
}

public void setRunner(QueryRunner runner) {
    this.runner = runner;
}

public List<Account> findAllAccount() {
    try {
        return runner.query(connectionUtils.getConnection(),"select * from account2", new BeanListHandler<Account>(Account.class));
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }
}

public Account findById(Integer i) {
    try {
        return runner.query(connectionUtils.getConnection(),"select * from account2 where id = ?", new BeanHandler<Account>(Account.class),i);
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }
}

public void saveAccount(Account account) {
    try {
         runner.update(connectionUtils.getConnection(),"insert into account2(username,money) values(?,?)", account.getUsername(), account.getMoney());
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }
}

public void updateAccount(Account account) {
    try {
        runner.update(connectionUtils.getConnection(),"update    account2 set username=?, money =? where id = ?",
                             account.getUsername(), account.getMoney(), account.getId());
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }
}

public void deleteAccount(Integer i) {
    try {
        runner.update(connectionUtils.getConnection(),"delete from    account2  where id = ?", i);
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }
}

public Account findByName(String sourceName) throws Exception {
    try {
        List<Account> lists =  runner.query(connectionUtils.getConnection(),"select * from account2 where username = ?", new BeanListHandler<Account>(Account.class),sourceName);
        if (lists == null || lists.size() == 0)
        {
            return null;
        }
        if (lists.size() > 1)
        {
            throw new Exception("数据不唯一");
        }
        return lists.get(0);
    } catch (SQLException e) {
        throw  new RuntimeException(e);
    }
}
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountTest {
@Autowired
IAccountService accountService = null;


@Test
public void tesTransaction()
{
    ////执行方法
    try {
        accountService.transfer("hao","chen",5.0f);
    } catch (Exception e) {
        e.printStackTrace();
    }
    testfindAll();
}
}

相关文章

  • Spring简单实现事务操作

    主配置文件 单连接类 管理连接的事务类 在业务层中加入事务控制 改变持久层,将单线程中的连接注入QueryRunn...

  • Spring事务

    Spring 事务 分类 Spring可以支持编程式事务和声明式事务。 编程式事务 实现 Spring使用事务管理...

  • spring04

    Spring JdbcTemplate学习 Spring 声明式事务 xml配置实现 Spring 声明式事务 注...

  • Spring中实现事务方式

    Spring 中实现事务的方式 Spring 并不直接支持事务,只有当数据库支持事务时,Spring 才支持事务,...

  • Spring 事务实现分析

    1. Spring 事务简介 Spring 本身并不实现事务,Spring事务 的本质 还是 底层数据库 对事务的...

  • SSM spring 2

    1 Spring 事务实现方式、事务的传播机制、默认的事务类别 Spring事务的1+3种实现方式、7种传播行为、...

  • 程序员之Spring

    1. Spring事务 spring的事务实现原理Spring事务将connection放入到当前线程的threa...

  • spring事务的实现原理

    spring实现事务的原理 Spring事务 的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring...

  • Spring源码解读(三)事务

    一、spring事务简介 spring中有两种事务实现方式: 1)编程式事务使用TransactionTempla...

  • 面试问题记录(一) Day50 2019-01-09

    并发中的常用锁,乐观锁和悲观锁,实现及使用 事务的特性,原理,spring如何实现事务 spring的实现机制,I...

网友评论

      本文标题:Spring简单实现事务操作

      本文链接:https://www.haomeiwen.com/subject/nrwobctx.html