BeginTransaction Hibernate necessary?

来源:互联网 发布:淘宝购物拒签会怎样 编辑:程序博客网 时间:2024/06/04 20:03

Is it really necessary to start a transaction when you are going to execute only one query, without deleting or updating data?
I’m currently using Hibernate 4.1.9 with C3p0

Example,

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 says:

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.

(emphasis mine)

0 0
原创粉丝点击