Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeginTransaction Hibernate necessary?

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();

regards

like image 315
Charlessmori Avatar asked Apr 04 '13 14:04

Charlessmori


1 Answers

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)

like image 165
JB Nizet Avatar answered Nov 10 '22 13:11

JB Nizet