在JDBI中始终使用事务是否会有明显的不利影响?

huangapple go评论82阅读模式
英文:

Is there any significant downside to just always using transactions in JDBI?

问题

对于永远不会变得太大的表,是否只使用JdbiExecutor::executeInTransaction而不是JdbiExecutor::execute有任何不利之处吗?

我试图简化一个非常庞大且复杂的代码片段,以便新开发人员能够更快地上手。如果性能上没有明显的差异(即使有差异,在这里进行优化也是过早的优化),那么我想编写一个实用程序,始终只使用JdbiExecutor::executeInTransaction

英文:

For tables that will never get too large, is there any downside to just using JdbiExecutor::executeInTransaction instead of JdbiExecutor::execute?

I'm trying to simplify a very large and complex piece of code so that new developers can onboard more quickly. If there isn't a significant difference in performance (which, even then, it would be premature optimization to address it here) then I'd like write a utility that just always uses JdbiExecutor::executeInTransaction.

答案1

得分: 2

PostgreSQL服务器以自动提交模式运行,因此使用显式事务不仅会导致将COMMIT语句发送到数据库,还会发送开始显式事务的BEGIN语句。

这意味着您每个事务最多可能会多出2次客户端-服务器往返。

仅当您需要将多个语句捆绑到一个事务中时,使用显式事务可能会提高性能。

我会运行一个简单的基准测试,以查看影响是否明显;如果没有影响,可以继续简化您的代码。

英文:

PostgreSQL server operates in autocommit mode, so using explicit transactions will not only cause COMMIT statements to be sent to the database, but also BEGIN statements that start an explicit transaction.

That means you could have up to 2 extra client-server round trips per transaction.

Using explicit transactions only if you need to bundle multiple statements into a transaction might improve your performance.

I would run a simple benchmark to see if the impact is noticeable; if not, go ahead and simplify your code.

huangapple
  • 本文由 发表于 2020年8月25日 22:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63581563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定