do database transactions prevent other users from interfering with it

后端 未结 6 1975
失恋的感觉
失恋的感觉 2021-01-01 04:25

Suppose I do (note: the syntax below is probably not correct, but don\'t worry about it...it\'s just there to make a point)

Start Transaction
INSERT INTO tab         


        
相关标签:
6条回答
  • 2021-01-01 04:50

    As other user is updating the same row, row level lock will be applied. So he is able to make change only after your transaction ends. So you will be seeing the result set that you inserted. Hope this helps.

    0 讨论(0)
  • 2021-01-01 04:50

    The only users that get effect is those that require access to the same rows in a table. Otherwise the user will not be affected.

    However is is slightly more complicated as the row locking can be a read lock or a write lock.

    Here is an explanation for the InnoDB storage engine.

    0 讨论(0)
  • 2021-01-01 04:52

    The transaction will make it seem like that the statements in the transaction run without any interference from other transactions. Most DBMSs (including MySQL) maintain ACID properties for transactions. In your case, you are interested in the A for Atomic, which means that the DBMS will make it seem like all the statements in your transactions run atomically without interruption.

    0 讨论(0)
  • 2021-01-01 05:03

    For efficiency reasons, developers do not set transactions to totally isolated for each other. Databases support multiples isolation levels namely Serializable, Repeatable reads, Read committed and Read uncommitted. They are list from the most strict to least strict.

    0 讨论(0)
  • 2021-01-01 05:08

    Interfere is a fuzzy word when it comes to SQL database transactions. What rows a transaction can see is determined in part by its isolation level.

    Hence the goal of the select is to get ALL info from the table that just got inserted by the preceding insert and ONLY by the preceding INSERT...

    Preceding insert is a little fuzzy, too.

    You probably ought to COMMIT the insert in question before you try to read it. Otherwise, under certain conditions not under your control, that transaction could be rolled back, and the row with id=100 might not actually exist.

    Of course, after it's committed, other transactions are free to change the value of "id", of "value", or both. (If they have sufficient permissions, that is.)

    0 讨论(0)
  • 2021-01-01 05:16

    This depends entirely on the Transaction Isolation that is used by the DB Connection.

    According to MySQL 5.0 Certification Study Guide

    enter image description here

    Page 420 describes three transactional conditions handled by Isolation Levels

    • A dirty read is a read by one transaction of uncommitted changes made by another. Suppose the transaction T1 modifies a row. If transaction T2 reads the row and sees the modificationeven though T1 has not committed it, that is a dirty read. One reason this is a problem is that if T1 roll backs, the change is undone but T2 does not know that.
    • A non-repeatable read occurs when a transaction performs the same retrieval twice but gets a different result each time.Suppose that T1 reads some rows, and that T2 then changes some of those rows and commits the changes. If T1 sees the changes when it reads the rows again, it gets a different result; the initial read is non-repeatable. This is a problem because T1 does not get a consistent result from the same query.
    • A phantom is a row that appears where it was not visible before. Suppose that T1 and T2 begin, and T1 reads some rows. If T2 inserts a new and T1 sees that row when it reads again, the row is a phantom.

    Page 421 describes the four(4) Transaction Isolation Levels:

    • READ-UNCOMMITTED : allows a transaction to see uncommitted changes made by other transactions. This isolation level allows dirty reads, non-repeatable reads, and phantoms to occur.
    • READ-COMMITTED : allows a transaction to see changes made by other transactions only if they've been committed. Uncommitted changes remains invisible. This isolation level allows non-repeatable reads, and phantoms to occur.
    • REPEATABLE READ (default) : ensure that is a transaction issues the same SELECT twice, it gets the same result both times, regardless of committed or uncommitted changesmade by other transactions. In other words, it gets a consistent result from different executions of the same query. In some database systems, REPEATABLE READ isolation level allows phantoms, such that if another transaction inserts new rows,in the inerbal between the SELECT statements, the second SELECT will see them. This is not true for InnoDB; phantoms do not occur for the REPEATABLE READ level.
    • SERIALIZABLE : completely isolates the effects of one transaction from others. It is similar to REPEATABLE READ with the additional restriction that rows selected by one transaction cannot be changed by another until the first transaction finishes.

    Isolation level can be set for your DB Session globally, within your session, or for a specific transaction:

    SET GLOBAL TRANSACTION ISOLATION LEVEL isolation_level;
    SET SESSION TRANSACTION ISOLATION LEVEL isolation_level;
    SET TRANSACTION ISOLATION LEVEL isolation_level;
    

    where isolation_level is one of the following values:

    • 'READ UNCOMMITTED'
    • 'READ COMMITTED'
    • 'REPEATABLE READ'
    • 'SERIALIZABLE'

    In my.cnf you can set the default as well:

    [mysqld]
    transaction-isolation = READ-COMMITTED
    
    0 讨论(0)
提交回复
热议问题