Interviewer: Can you talk about some characteristics of the transaction is valid? What are the isolation level?

1, face questions

  • What matters is that several features?

  • Which database transaction isolation level there?

  • MySQL's default isolation level?


2, the interviewer heart analysis

Mysql development with three fundamentals: storage engine, indexing, then the transaction is, you have to use transaction.

Because a business system, be sure to add a bunch of related transactions to ensure the operation, either succeed together or fail together, right? So this is a problem chat database must ask

The most basic use mysql to develop, to 3:00: Storage Engine (understand), index (the index can be built, have to spend writing SQL index), transaction (transaction isolation level of understanding, based transaction support spring in the code Riga Affairs)

Storage Engine -> innodb, index, according to your basic needs are built SQL index (the index may have missed some of the forgotten construction), transaction (@Transactional annotation of unified service layer added Affairs)


3, face questions Analysis

ACID 3.1 transactions

The first talk about ACID, must know:

(1) Atomic: atomicity, is a bunch of SQL, either succeed together, or are not implemented, do not allow a SQL successful, an SQL failure, this is nonsense, not atomic.

(2) Consistency: consistency, this is it for data consistency, is a set of SQL before execution, data must be accurate, after execution, data must also be accurate. So now do, execute SQL finished, the results of the corresponding SQL data modification did not give you perform, that's not what pit father.

(3) Isolation: isolation, this means that multiple transactions can not interfere with each other at the time of running, not A transaction operation data, somehow not get half the children, to change the result of the transaction data B, leading to the transaction a operation error, and would not that funny.

(4) Durability: persistence, transactions are successful, it is necessary to permanently modify the data is valid, after a while children do not own the data is gone, gone, then the fun.


3.2 transaction isolation level

In short, the interview you ask Affairs, to talk about ACID, and then talk about isolation levels

(1) Uncommitted Read, Read Uncommitted: this is very pit father, a transaction that is not submitted in time, modify the data, let other transactions to read, which is disgusting, can easily lead to mistakes. This is also called a dirty read.

(2)读已提交,Read Committed(不可重复读):这个比上面那个稍微好一点,但是一样比较尴尬

就是说事务A在跑的时候, 先查询了一个数据是值1,然后过了段时间,事务B把那个数据给修改了一下还提交了,此时事务A再次查询这个数据就成了值2了,这是读了人家事务提交的数据啊,所以是读已提交。

这个也叫做不可重复读,就是所谓的一个事务内对一个数据两次读,可能会读到不一样的值。如图:

db4e87bdc3b140fd9fc7732e4232a847


(3)可重复读,Read Repeatable:这个比上面那个再好点儿,就是说事务A在执行过程中,对某个数据的值,无论读多少次都是值1;哪怕这个过程中事务B修改了数据的值还提交了,但是事务A读到的还是自己事务开始时这个数据的值。如图:

b64b228a2e524fc3ba5decbd5f148985


(4)幻读:不可重复读和可重复读都是针对两个事务同时对某条数据在修改,但是幻读针对的是插入

比如某个事务把所有行的某个字段都修改为了2,结果另外一个事务插入了一条数据,那个字段的值是1,然后就尴尬了。第一个事务会突然发现多出来一条数据,那个数据的字段是1。

那么幻读会带来啥问题呢?因为在此隔离级别下,例如:事务1要插入一条数据,我先查询一下有没有相同的数据,但是这时事务2添加了这条数据,这就会导致事务1插入失败,并且它就算再一次查询,也无法查询到与其插入相冲突的数据,同时自身死活都插入不了,这就不是尴尬,而是囧了。

(5)串行化:如果要解决幻读,就需要使用串行化级别的隔离级别,所有事务都串行起来,不允许多个事务并行操作。如图:

2891101eef9b4c0d807232f8330fa08f


(6)MySQL的默认隔离级别是Read Repeatable,就是可重复读,就是说每个事务都会开启一个自己要操作的某个数据的快照,事务期间,读到的都是这个数据的快照罢了,对一个数据的多次读都是一样的。

接下来我们聊下MySQL是如何实现Read Repeatable的吧,因为一般我们都不修改这个隔离级别,但是你得清楚是怎么回事儿,MySQL是通过MVCC机制来实现的,就是多版本并发控制,multi-version concurrency control。

当我们使用innodb存储引擎,会在每行数据的最后加两个隐藏列,一个保存行的创建时间,一个保存行的删除时间,但是这儿存放的不是时间,而是事务id,事务id是mysql自己维护的自增的,全局唯一。

事务id,在mysql内部是全局唯一递增的,事务id=1,事务id=2,事务id=3


340bb0ad2a674fc1af0bf1790a42271e


事务id=121的事务,查询id=1的这一行的时候,一定会找到创建事务id <= 当前事务id的那一行

select * from table where id=1,就可以查到上面那一行

事务id=122的事务,将id=1的这一行给删除了,此时就会将id=1的行的删除事务id设置成122

事务id=121的事务,再次查询id=1的那一行,能查到吗?

能查到,要求创建事务id <= 当前事务id,当前事务id < 删除事务id

事务id=121的事务,查询id=2的那一行,查到name=李四

事务id=122的事务,将id=2的那一行的name修改成name=小李四

事务id=121的事务,查询id=2的那一行,答案是:李四,创建事务id <= 当前事务id,当前事务id < 删除事务id

在一个事务内查询的时候,mysql只会查询创建时间的事务id小于等于当前事务id的行,这样可以确保这个行是在当前事务中创建,或者是之前创建的;

同时一个行的删除时间的事务id要么没有定义(就是没删除),要么是必当前事务id大(在事务开启之后才被删除);满足这两个条件的数据都会被查出来。

那么如果某个事务执行期间,别的事务更新了一条数据呢?这个很关键的一个实现,其实就是在innodb中,是插入了一行记录,然后将新插入的记录的创建时间设置为新的事务的id,同时将这条记录之前的那个版本的删除时间设置为新的事务的id。

现在get到这个点了吧?这样的话,你的这个事务其实对某行记录的查询,始终都是查找的之前的那个快照,因为之前的那个快照的创建时间小于等于自己事务id,然后删除时间的事务id比自己事务id大,所以这个事务运行期间,会一直读取到这条数据的同一个版本。

记住,聊到事务隔离级别,必须把这套东西给喷出来,尤其是mvcc,说实话,市面上相当大比重的java程序员,对mvcc是不了解的

觉得文章不错就给小老弟点个关注吧,更多内容陆续奉上。

最后,分享一份面试宝典《Java核心知识点整理.pdf》,覆盖了JVM、锁、高并发、反射、Spring原理、微服务、Zookeeper、数据库、数据结构等等。加入我的个人粉丝群(Java架构技术栈:644872653)获取免费领取方式。


Guess you like

Origin blog.51cto.com/14480698/2470867