简体   繁体   English

为什么在两个相同的查询之间会得到相同的结果?

[英]Why did i get the same result between two same query?

I'm working on a JSF project.Our development based on MySql 5.1, JBoss 4.22GA and i got some trouble today and hope someone can help me to figure it out. 我正在开发一个JSF项目,我们基于MySql 5.1,JBoss 4.22GA的开发,今天遇到了一些麻烦,希望有人能帮助我解决这个问题。

Here's the situation:for some reasons i have to do some update to a table and then see if we can find these changes out.To do so, i call the method something like below. 这是一种情况:由于某些原因,我必须对表进行一些更新,然后查看是否可以找到这些更改。为此,我调用下面的方法。

create table `table` (
    id int,
    update_date datetime,
);

// consider this method as one transaction
somemethod() {

    A. select max(update_date) from table where id = 1;

    B. update table set update_date = now() where id = 1;

    C. select max(update_date) from table where id = 1;
}

Note that : 注意 :

1.we break after A (you know using break point in eclipse)

2.we execute B from somewhere outside application (which means execute update in something like GUI DB manager)

3.after B we proceed to execute C (same we did as A)

and we got same result as we did at A. But we can confirm that change in the GUI DB manager. 并且获得了与A相同的结果。但是我们可以在GUI DB管理器中确认该更改。 Dose any one can tell me why did this happen? 剂量有人可以告诉我为什么会这样吗? I'll be very appreciate for any helping. 我将不胜感激。 Thank you! 谢谢! (And thus my bad english i hope i've already made myself clear enough.) (因此我的英语不好,我希望我已经足够清楚了。)

The default transaction isolation level in MySQL is REPEATABLE READ, this is what the manual tells about it MySQL中的默认事务隔离级别是REPEATABLE READ,这就是该手册说明的内容

If the transaction isolation level is REPEATABLE READ (the default level), all consistent reads within the same transaction read the snapshot established by the first such read in that transaction. 如果事务隔离级别为REPEATABLE READ(默认级别),则同一事务中的所有一致读取将读取该事务中第一个此类读取所建立的快照。 You can get a fresher snapshot for your queries by committing the current transaction and after that issuing new queries. 您可以通过提交当前事务并在此之后发出新查询来获取查询的更新快照。

In short that means if you read something during a transaction, all following reads of the same data in the same transaction will return the same result, which is exactly what you're seeing. 简而言之,这意味着如果您在事务处理期间读取某些内容,则随后在同一事务处理中对相同数据的所有读取都将返回相同的结果,这正是您所看到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM