Why do we need Transaction in Hibernate for read-only operations

0 votes

Why do I need Transaction in Hibernate for read-only operations?

Does the following transaction put a lock in the DB?

Example code to fetch from DB:

Transaction tx = HibernateUtil.getCurrentSession().beginTransaction(); // why begin transaction?
tx.commit() // why tx.commit? I don't want to write anything

Can I use session.close() instead of tx.commit()?

May 4, 2020 in Database by kartik
• 37,520 points
3,030 views

1 answer to this question.

0 votes

Hello @kartik,

It doesn't matter whether you only read or not - the database must still keep track of your resultset, because other database clients may want to write data that would change your resultset.

I have seen faulty programs to kill huge database systems, because they just read data, but never commit, forcing the transaction log to grow, because the DB can't release the transaction data before a COMMIT or ROLLBACK, even if the client did nothing for hours.

Hope this is helpful!!

Thank You!!

answered May 4, 2020 by Niroj
• 82,840 points

Related Questions In Database

0 votes
1 answer

Do I need to add primary key in each and every table?

Hi Shraddha, You have a doubt to add ...READ MORE

answered May 29, 2019 in Database by sampriti
• 1,120 points
1,498 views
+1 vote
1 answer

Do I need to mention primary key for each table?

Hi Pritha, First, there is no hard and ...READ MORE

answered Jul 4, 2019 in Database by sampriti
• 1,120 points
1,025 views
0 votes
1 answer

How can we perform common set operations (union, intersection, minus) in MS Excel?

The MATCH function can be used as ...READ MORE

answered Mar 31, 2022 in Database by gaurav
• 23,260 points
1,903 views
0 votes
1 answer

Error:Import SQL dump into PostgreSQL database

Hello, Here is the command you are looking ...READ MORE

answered May 4, 2020 in Database by Niroj
• 82,840 points
5,301 views
0 votes
1 answer
0 votes
1 answer

MySQL Error: : 'Access denied for user 'root'@'localhost'

Hello @kartik, I did this to set my ...READ MORE

answered May 4, 2020 in Database by Niroj
• 82,840 points
18,454 views
0 votes
1 answer

Error Code: 2020. Lost connection to MySQL server during query

Hello @kartik, I got the same issue when ...READ MORE

answered May 4, 2020 in Database by Niroj
• 82,840 points
2,714 views
0 votes
1 answer

How do I kill all the processes in Mysql “show processlist”?

Hello kartik, Mass killing operation saves time. Do it ...READ MORE

answered May 4, 2020 in Database by Niroj
• 82,840 points
2,569 views
0 votes
1 answer

I need query based on Timestamp in sqlserver

Hello  vnk, To get some familiarity try creating and ...READ MORE

answered Jun 30, 2020 in Database by Niroj
• 82,840 points
2,292 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP