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 in Database by kartik
• 27,140 points
85 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 by Niroj
• 58,500 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,100 points
105 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,100 points
151 views
0 votes
1 answer

Data model operations in Hbase

1. Get(returns attributes for a specified row, Gets ...READ MORE

answered Jul 16, 2018 in Database by Data_Nerd
• 2,390 points
90 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 in Database by Niroj
• 58,500 points
133 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 in Database by Niroj
• 58,500 points
3,301 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 in Database by Niroj
• 58,500 points
592 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 in Database by Niroj
• 58,500 points
93 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 in Database by Niroj
• 58,500 points
102 views