gpt4 book ai didi

hibernate - 获取数据时在 hibernate 状态下是否需要 `session.getTransaction().commit();`

转载 作者:行者123 更新时间:2023-12-01 12:39:34 26 4
gpt4 key购买 nike

我想知道

session.getTransaction().commit();获取数据时在 hibernate 状态下需要

private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}

此示例取自 http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#tutorial-firstapp-firstclass

最佳答案

是的,您甚至需要一个事务来进行读取操作,并且应该按照 hibernate 文档提交它。

以下是详细信息:

13.2. Database transaction demarcation

Database, or system, transaction boundaries are always necessary. No communication with the database can occur outside of a database transaction (this seems to confuse many developers who are used to the auto-commit mode). Always use clear transaction boundaries, even for read-only operations. Depending on your isolation level and database capabilities this might not be required, but there is no downside if you always demarcate transactions explicitly. Certainly, a single database transaction is going to perform better than many small transactions, even for reading data.



在普通 JDBC 的情况下,默认情况下启用自动提交。以下是它的详细信息:

Disabling Auto-Commit Mode

When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. (To be more precise, the default is for a SQL statement to be committed when it is completed, not when it is executed. A statement is completed when all of its result sets and update counts have been retrieved. In almost all cases, however, a statement is completed, and therefore committed, right after it is executed.)

关于hibernate - 获取数据时在 hibernate 状态下是否需要 `session.getTransaction().commit();`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com