• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

What happens if I do my reads outside a transaction?

 
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.
 
ranger
Posts: 17344
11
Mac IntelliJ IDE 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
Posts: 17344
11
Mac IntelliJ IDE 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
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!