为什么是“hibernate.connection.autocommit = true"?不推荐在 Hibernate 中使用? [英] Why is "hibernate.connection.autocommit = true" not recommended in Hibernate?

查看:17
本文介绍了为什么是“hibernate.connection.autocommit = true"?不推荐在 Hibernate 中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Hibernate API 中,有一个属性 hibernate.connection.autocommit 可以设置为 true.

In Hibernate API, there is a property hibernate.connection.autocommit which can be set to true.

但是在API中,他们已经提到不建议这样设置:

But in the API, they have mentioned that it is not recommended to set it like so:

为 JDBC 池连接启用自动提交(它不是推荐).

Enables autocommit for JDBC pooled connections (it is not recommended).

为什么不推荐?将此属性设置为 true 有什么不良影响?

Why is it not recommended ? What are the ill-effects of setting this property to true ?

推荐答案

所有数据库语句都在物理事务的上下文中执行,即使我们没有明确声明事务边界 (BEGIN/COMMIT/ROLLBACK).

All database statements are executed within the context of a physical transaction, even when we don’t explicitly declare transaction boundaries (BEGIN/COMMIT/ROLLBACK).

如果不声明事务边界,那么每个语句都必须在单独的事务中执行.这甚至可能导致每个语句打开和关闭一个连接.

If you don't declare the transaction boundaries, then each statement will have to be executed in a separate transaction. This may even lead to opening and closing one connection per statement.

将服务声明为@Transactional 将在整个事务期间为您提供一个连接,并且所有语句都将使用该单个隔离连接.这比一开始不使用显式事务要好得多.在大型应用程序中,您可能有很多并发请求,降低数据库连接获取请求率肯定会提高您的整体应用程序性能.

Declaring a service as @Transactional will give you one connection for the whole transaction duration, and all statements will use that single isolation connection. This is way better than not using explicit transactions in the first place. On large applications you may have many concurrent requests and reducing the database connection acquiring request rate is definitely improving your overall application performance.

所以经验法则是:

  1. 如果您有只执行一个查询的只读事务,您可以为这些事务启用自动提交.

  1. If you have read-only transactions that only execute one query, you can enable auto-commit for those.

如果您的事务包含多个语句,则需要禁用自动提交,因为您希望所有操作都在一个工作单元中执行,并且您不想给它施加额外的压力您的连接池.

If you have transactions containing more than one statement, you need to disable auto-commit, since you want all operations to execute in a single unit-of-work and you don't want to put extra pressure on your connection pool.

这篇关于为什么是“hibernate.connection.autocommit = true"?不推荐在 Hibernate 中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆