• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate transaction will lock the row in table(MySQL)

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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?

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

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

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

Thanks...
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic