Spring详解(五)

本文详细介绍了Spring框架中的事务管理,包括事务的四大特性(ACID)、PlatformTransactionManager接口及其在不同持久化机制中的实现。此外,还展示了编程式事务处理和声明式事务处理的实现方式,包括TransactionTemplate的使用和基于AOP的XML配置及注解配置。通过实例演示了转账操作在不同事务管理方式下的处理过程。
摘要由CSDN通过智能技术生成

事务的四个特性(ACID)

①、原子性(Atomicity):事务是一个原子操作,由一系列动作组成。事务的原子性确保动作要么全部完成,要么完全不起作用。

  ②、一致性(Consistency):一旦事务完成(不管成功还是失败),系统必须确保它所建模的业务处于一致的状态,而不会是部分完成部分失败。在现实中的数据不应该被破坏。

  ③、隔离性(Isolation):可能有许多事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏。

  ④、持久性(Durability):一旦事务完成,无论发生什么系统错误,它的结果都不应该受到影响,这样就能从任何系统崩溃中恢复过来。通常情况下,事务的结果被写到持久化存储器中。


PlatformTransactionManager 事务管理器

 Spring事务管理器的接口是
org.springframework.transaction.PlatformTransactionManager,Spring并不直接管理事务,通过这个接口,Spring为各个平台如JDBC、Hibernate等都提供了对应的事务管理器,也就是将事务管理的职责委托给Hibernate或者JTA等持久化机制所提供的相关平台框架的事务来实现。

也就是说Spring事务管理的为不同的事务API提供一致的编程模型,具体的事务管理机制由对应各个平台去实现

AccountDao 接口:

public interface AccountDao {

/**

* 汇款

* @param outer 汇款人

* @param money 汇款金额

*/

public void out(String outer,int money);

/**

* 收款

* @param inner 收款人

* @param money 收款金额

*/

public void in(String inner,int money);

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.my.dao.AccountDao;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

/**

* 根据用户名减少账户金额

*/

@Override

public void out(String outer, int money) {

this.getJdbcTemplate().update("update account set money = money - ? where username = ?",money,outer);

}

/**

* 根据用户名增加账户金额

*/

@Override

public void in(String inner, int money) {

this.getJdbcTemplate().update("update account set money = money + ? where username = ?",money,inner);

}

}

public interface AccountService {

/**

* 转账

* @param outer 汇款人

* @param inner 收款人

* @param money 交易金额

*/

public void transfer(String outer,String inner,int money);

}

public class AccountServiceImpl implements AccountService{

private AccountDao accountDao;

public void setAccountDao(AccountDao accountDao) {

this.accountDao = accountDao;

}

@Override

public void transfer(String outer, String inner, int money) {

accountDao.out(outer, money);

accountDao.in(inner, money);

}

}

<?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: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/aop

http://www.springframework.org/schema/aop/spring-aop.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/test"></property>

<property name="user" value="root"></property>

<property name="password" value="root"></property>

</bean>

<bean id="accountDao" class="com.my.dao.impl.AccountDaoImpl">

<property name="dataSource" ref="dataSource"></property>

</bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值