• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What happens if I do my reads outside a transaction?

 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand writing to the database within transaction boundaries, but it had never occurred me that I need to do the same on read. Do I? Why? Now I have a whole application that does not do that, it's 2-tier and has not yet been tested with many clients fighting for locks.. am I up for a major surprise? H2 database.

I would have just tested and found out.. but I need to give time estimate of the completion date, now, and I don't even know whether I am to modify all my reads.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Hibernate the Session is one to one to a Transaction. and a Session doesn't work without a Transaction. Now you can set your Transaction to read only for those read only calls, but you need a transaction. If you do not have a transaction manager, and Spring can provide one in your scenario if you use Spring in your tier. But if you don't you need to call session.beginTransaction, so that Hibernate can create an internal Transaction for you. But I still believe you have to configure it in your hibernate.cfg.xml or whereever you define your SessionFactory.

Mark
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark! I still don't understand.. could you please elaborate?

Mark Spritzler wrote:Now you can set your Transaction to read only for those read only calls, but you need a transaction.

Why? What are my risks as it is now?

If you do not have a transaction manager, and Spring can provide one in your scenario if you use Spring in your tier.

I don't have Spring, but I do apparently have a transaction manager, since my writes are at present wrapped within transactions.

But if you don't you need to call session.beginTransaction, so that Hibernate can create an internal Transaction for you.

Or else? That's precisely my question actually. I am not calling it now, am I asking for trouble? What kind of trouble? What should I look out for?



 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Hibernate requires a transaction to be one to one with a Session. Hibernate has built in transactions, but you have to tell Hibernate you plan to use it by calling Transaction tx = session.beginTransaction(), then tx.commit() or tx.rollback() as needed.

If you have a transaction manager already, then you don't call those methods as Hibernate will just join the transactions already created by the Transaction Manager.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic