2019独角兽企业重金招聘Python工程师标准>>>
题记:关于Spring事务管理的文章网络上有许多资源,自己也看过一些,但只是当时看了看也没做整理与总结,后面也就淡忘了,比如说事务的传播行为,当时看了几遍博文清楚的记得有7种,但是工作中常用到的就是一两种,今天看今日头条看到一篇讲解事务管理的文章,就立马回想这7种,但是发现不能全部记起来,觉得以后看的文章还是要做些日志记录总结吧,快工作一年半的人原创博客没有不说,连记录学习的博客都没有,也太那个啥了... 因本身也是学习,如果日志中有不正确的地方,也望有看过本文的学友们指出,先行谢过!
- 什么是事务
- 事务管理的核心接口
- 事务的种类
什么是事务
一般我们说的事务就是指数据库事务,百度百科对其定义:“数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行。” ,我们可以把取钱看做是一个事务,A去ATM机取1000,银行卡先扣款1000,然后ATM机出款1000,这两步都成功就是一个成功执行的事务,假若银行卡扣款成功,但是ATM机没出,那么A会损失1000,或者银行卡扣款扣款失败,但是ATM机却出了1000,那么银行将损失1000,这两步有一步失败双方都将受损失,这时就得保证两步都可以取消原来的操作才避免双方的损失,这就是事务的回滚,也即“要么完全地执行,要么完全地不执行”。
事务的四个属性:ACID
- 原子性(Atomicity)事务是一个原子操作,由一系列动作组成。事务的原子性确保动作要么全部完成,要么完全不起作用。
- 一致性(Consistency)一旦事务完成(不管成功还是失败),系统必须确保它所建模的业务处于一致的状态,而不会是部分完成部分失败。在现实中的数据不应该被破坏。
- 隔离性(Isolation)可能有许多事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏。
- 持久性(Durability)一旦事务完成,无论发生什么系统错误,它的结果都不应该受到影响,这样就能从任何系统崩溃中恢复过来。通常情况下,事务的结果被写到持久化存储器中
事务管理的核心接口
PlatformTransactionManager
TransactionDefinition
TransactionStatus
PlatformTransactionalManager
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
TransactionalDefinition
该接口定义了五个基本的事务属性(传播行为、隔离规则、是否只读、事务名字、事务超时时间)
传播行为
PROPAGATION_REQUIRED
Support a current transaction; create a new one if none exists.
int PROPAGATION_REQUIRED = 0;
PROPAGATION_SUPPORTS
Support a current transaction; execute non-transactionally if none exists.
int PROPAGATION_SUPPORTS = 1;
PROPAGATION_MANDATORY
Support a current transaction; throw an exception if no current transaction exists.
int PROPAGATION_MANDATORY = 2;
PROPAGATION_REQUIRES_NEW
Create a new transaction, suspending the current transaction if one exists.
int PROPAGATION_REQUIRES_NEW = 3;
PROPAGATION_NOT_SUPPORTED
Do not support a current transaction; rather always execute non-transactionally.
int PROPAGATION_NOT_SUPPORTED = 4;
PROPAGATION_NEVER
Do not support a current transaction; throw an exception if a current transaction exists
int PROPAGATION_NEVER = 5;
PROPAGATION_NESTED
Execute within a nested transaction if a current transaction exists,behave like PROPAGATION_REQUIRED else.
int PROPAGATION_NESTED = 6;
隔离规则
ISOLATION_DEFAULT 底层数据库默认隔离级别
Use the default isolation level of the underlying datastore.
int ISOLATION_DEFAULT = -1;
ISOLATION_READ_UNCOMMITTED 读未提交
Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;
ISOLATION_READ_COMMITTED 读已提交
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
ISOLATION_REPEATABLE_READ 可重复读
Indicates that dirty reads and non-repeatable reads are prevented phantom reads can occur.
int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;
ISOLATION_SERIALIZABLE 序列化
Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.
int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
方法
int getPropagationBehavior();
int getIsolationLevel();
int getTimeout();
boolean isReadOnly();
String getName();
TransactionStatus
boolean isNewTransaction(); // Return whether the present transaction is new (else participating in an existing transaction, or potentially not running in an actual transaction in the first place).
boolean hasSavepoint(); // Return whether this transaction internally carries a savepoint, that is, has been created as nested transaction based on a savepoint.
void setRollbackOnly(); // Set the transaction rollback-only. This instructs the transaction manager that the only possible outcome of the transaction may be a rollback, as alternative to throwing an exception which would in turn trigger a rollback.
boolean isRollbackOnly(); // Return whether the transaction has been marked as rollback-only (either by the application or by the transaction infrastructure).
void flush(); // Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions.
boolean isCompleted(); // Return whether this transaction is completed, that is, whether it has already been committed or rolled back.
事务的种类
编程式和声明式事务的区别
Spring提供了对编程式事务和声明式事务的支持,编程式事务允许用户在代码中精确定义事务的边界,而声明式事务(基于AOP)有助于用户将操作与事务规则进行解耦。
简单地说,编程式事务侵入到了业务代码里面,但是提供了更加详细的事务管理;而声明式事务由于基于AOP,所以既能起到事务管理的作用,又可以不影响业务代码的具体实现。
实际开发中一般不使用编程式事务管理
常见的两种声明式事务配置方式
1. 使用tx标签配置的拦截器
2. 注解 (配合@Tranactional使用)
此时Dao上加上@Transactional
package com.bluesky.spring.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.bluesky.spring.domain.User;
@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public List listUsers() {
return this.getSession().createQuery("from User").list();
}
}
此文为自己总结梳理所用,文章有一部分内容来自博文 http://www.mamicode.com/info-detail-1248286.html ,后续日志会记录事务回滚规则相关内容