我需要执行多个SQL查询(select,update,delete兼)和回滚如果有够程出现了错误.因此问题是:DB 事务是否可以安全地进行并发访问?
一般来说是的,但您必须定义所需的安全级别。交易中可能发生的三种标准现象是:
- Dirty reads (read uncommitted data)
- Nonrepeatable reads (a row is retrieved twice and the values within the row differ between reads)
- Phantom reads ( two identical queries are executed, and the collection of rows returned by the second query is different from the first)
Run Code Online (Sandbox Code Playgroud)
根据接受的行为,您可以使用不同的隔离级别:
- Read uncommitted (all phenomena possible)
- Read committed (dirty read prevented)
- Repeatable reads (phantom read can occur)
- Serializable (non of the phenomena is possible)
Run Code Online (Sandbox Code Playgroud)
一般来说,您使用的隔离级别“越高”,获得的并发性就越差。较差是因为使用了更多的锁并阻止了来自其他事务的并发查询。如果您知道要更新选定的行,则可以选择 ... 进行更新。
例如,请参阅http://en.wikipedia.org/wiki/Isolation_%28database_systems%29以获得更全面的解释。