简体   繁体   English

在事务中运行sql查询是否有副作用?

[英]Are there side effects running a sql query within a transaction?

Are there side effects running a sql select query within a transaction? 在事务中运行sql select查询是否有副作用?

I am executing a service method which queries and inserts/updates data in one transaction block. 我正在执行一种服务方法,该服务方法在一个事务块中查询和插入/更新数据。

The query is included in the transaction. 该查询包含在事务中。 Should I expect any negative behavior from this? 我应该期望由此产生任何负面行为吗?

Negative or possitive behavior is not an absolute concept. 消极或积极的行为不是绝对的概念。

You should design isolation and transactions according to your application needs. 您应该根据应用程序需求设计隔离和事务。 More isolation also longs transactions means more locks. 更多的隔离也使多头交易意味着更多的锁定。

I wouldn't say there are any negative behaviors, but I know in SQL Server by default transactions lock rows. 我不会说有任何负面行为,但是我知道默认情况下SQL Server中的事务锁定行。 So if you have queries hitting the table you are updating/inserting into and it takes a while the queries could lock and/or timeout. 因此,如果您有查询命中该表,则您正在更新/插入其中,查询可能需要一段时间才能锁定和/或超时。 You can find and example of this here . 您可以在此处找到示例。

About the select statement that is not necessarily bad if you are using it as a condition for a successful insert/update. 关于select语句,如果将其用作成功插入/更新的条件,则不一定很糟糕。 The point of a transaction is to be able to rollback any issues if something fails. 事务的重点是如果出现故障,则能够回滚任何问题。 So if the query does not accomplish this goal I would leave it out of the transaction. 因此,如果查询未实现此目标,则将其排除在事务之外。 Here's a cool article that you might want to leaf through to help you get the concept of how to use transactions effectively. 这是一篇很酷的文章,您可能希望翻阅,以帮助您了解如何有效使用事务的概念。

You will lock while the transaction is open. 交易打开时您将锁定。 If you'd doing this from .NET and not careful, you will leave the lock on the table. 如果要从.NET进行此操作并且不小心,则将锁留在表上。 Also, if you roll back your transaction on a table that has an identity column and insert while the first transaction is still open, you'll end up with non-contiguous identities. 同样,如果您在具有标识列的表上回滚事务并在第一个事务仍处于打开状态时插入,则最终将获得不连续的标识。

However, the benefits for running things in transactions can out weigh these problems. 但是,在事务中运行事物的好处可以抵消这些问题的影响。

You should try to keep your transactions as small as possible. 您应该尝试使交易尽可能小。

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

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