• Post Reply Bookmark Topic Watch Topic
  • New Topic

hibernate transaction will lock the row in table(MySQL)

stshiva raaj
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Hibernate3.2 in my DAO layer. I need to know, hibernate will do the row level lock after transaction is started. Hibernate do the lock after start the transaction or while commit the transaction? Hibernate has ability to lock the table or row?

Session session1=HibernateSessionFactory.currentSession();
Transaction tran1=session1.beginTransaction();
TaskFolder folder=session1.load(TaskFolder.class,2);//? - here lock the table

tran1.commit();//? - here lock the table

Is there any lock will happen in mysql database due to transaction? or not

Martin Vajsar
Posts: 3752
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is generally a database related problem, so I shall move your question to the JDBC forum.

This isn't anything Hibernate specific. The same logic implemented using pure JDBC would do the same. It has to do with concurrency model in MySQL, which is based on locks. You're using some of the higher isolation levels (I'd say at least TRANSACTION_REPEATABLE_READ), and this means that the database is obliged to return the same unmodified row if you read it again in the same transaction. MySQL and other lock-based concurrency databases generally ensure this by locking rows which have been read by an active transaction. So the locks are obtained at the time a row is read (or modified), and released upon commit or rollback.

You might avoid locking row read by a transaction by using a lower isolation level, for example TRANSACTION_READ_COMMITTED, but it might impact your business logic, so you need to make sure that your code will still work as expected in multi-user environment with the relaxed isolation level.
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!