Use transactions for select statements?(将事务用于选择语句?)
问题描述
我不经常使用存储过程,我想知道将我的选择查询包装在事务中是否有意义.
I don't use Stored procedures very often and was wondering if it made sense to wrap my select queries in a transaction.
我的程序有三个简单的选择查询,其中两个使用第一个的返回值.
My procedure has three simple select queries, two of which use the returned value of the first.
推荐答案
在高度并发的应用程序中,(理论上)可能会发生在第一个选择中读取的数据在其他选择执行之前被修改的情况.
In a highly concurrent application it could (theoretically) happen that data you've read in the first select is modified before the other selects are executed.
如果您的应用程序中可能发生这种情况,您应该使用事务来包装您的选择.请确保选择正确的隔离级别,但并非所有事务类型都能保证一致的读取.
If that is a situation that could occur in your application you should use a transaction to wrap your selects. Make sure you pick the correct isolation level though, not all transaction types guarantee consistent reads.
更新:您还可以找到关于并发更新/插入解决方案(又名upsert)有趣.它把几种常用的 upsert 方法放到测试中,看看是什么方法实际上保证数据在 select 和 next 语句之间没有被修改.结果是,嗯,令人震惊.
Update : You may also find this article on concurrent update/insert solutions (aka upsert) interesting. It puts several common methods of upsert to the test to see what method actually guarantees data is not modified between a select and the next statement. The results are, well, shocking I'd say.
这篇关于将事务用于选择语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将事务用于选择语句?


- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01