gpt4 book ai didi

hibernate - BeginTransaction Hibernate有必要吗?

转载 作者:行者123 更新时间:2023-12-02 18:55:19 26 4
gpt4 key购买 nike

当您只打算执行一个查询而不删除或更新数据时,是否真的有必要启动事务?

我目前正在使用带有 C3p0 的 Hibernate 4.1.9

示例,

session session = hibernateutil.getsessionfactory().opensession();
Transaction tx = session.beginTransaction();
List messages = session.createQuery("from Message m order by m.text asc").list();
tx.commit();
session.close();

问候

最佳答案

The documentation说:

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.

(强调我的)

关于hibernate - BeginTransaction Hibernate有必要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15814607/

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