hibernate isolation 幻读

来源:互联网 发布:华悦网络加速器怎么样 编辑:程序博客网 时间:2024/06/11 14:24

@transactional代表一个transaction

Isolation默认是

FAULT
          Use the default isolation level of the underlying datastore.

如果用oracle 则默认的是In the read committed isolation level, which is the default

因为今天在同一个@transactional里面查询2次同样的对象,出现不同的结果,才知道产生了幻读

  • Nonrepeatable (fuzzy) reads

    A transaction rereads data it has previously read and finds that another committed transaction has modified or deleted the data. For example, a user queries a row and then later queries the same row, only to discover that the data has changed.

     

    Read Committed Isolation Level

    In theread committed isolation level, which is the default, every query executed by a transaction sees only data committed before the query—not the transaction—began. This level of isolation is appropriate for database environments in which few transactions are likely to conflict.

    注意是在query之前commit数据会看到,而不是transaction开始之前的数据,所有在@transactional中,query之前其他transaction修改后commit的数据都会看到,也就是我发现的问题

    接着

    A query in a read committed transaction avoids reading data that commits while the query is in progress. For example, if a query is halfway through a scan of a million-row table, and if a different transaction commits an update to row 950,000, then the query does not see this change when it reads row 950,000. However, because the database does not prevent other transactions from modifying data read by a query, other transactions may change databetween query executions. Thus, a transaction that runs the same query twice may experience fuzzy reads and phantoms.

    就是在query读数据的过程中,如果其他transaction修改并提交了数据,正在查询的数据是看不到变化的,而且也不会阻止其他transaction修改,再次在该transaction查询就会发现和之前的数据不一样了,也就是幻读fuzzy reads