Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Internal behaviour of spring transaction query

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am posting this as because i am stuck at some point .

Let me tell you the before hand behavior of transaction that i used and after behavior of the code

Before behaviorr
I have used spring transaction annotation (@Transactional(propagation = Propagation.REQUIRED) )  
after that i have  update the  details using


then i try to fetch the details of the domain object which i update (its again a hibernateTemplate find method )
and i am able to fetch the updated details as all are in Transactional boundary.
No problem with anything

certainly i get different result when i fetch the details before update and rest of the code execute as usual (see below)
after behavior


after that i wont get the updated details which i update all are in one transaction boundary


how the step 1 which is get the domain object create a difference in result . and what is the concept behind in the transaction . Can anybody help in this regards


Thanks in advance

 
Ranch Hand
Posts: 1795
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bikasit,
Are you sure your transaction is not rolled back? Do you see any exception thrown in your terminal console?
Or, your change is not committed at all? I believe Hibernate automatically commit your changes after a transaction completely successfully.
 
Saloon Keeper
Posts: 21593
146
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Bikasit!

I believe that transaction propagation is the default in Spring, although I could be wrong.

And I'm not sure if I understood the question, but are you attempting to query the database while a transaction is in progress and expecting the changes you've already made to be returned? A transaction isn't "real" until it's committed. Work in progress cannot be trusted as it may actually be sitting somewhere waiting for the commit and not yet in the database at all.
 
Himai Minh
Ranch Hand
Posts: 1795
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I don't know much details about the code. But I believe Hibernate will automatically commit the changes to the DB after a transaction is completed successfully.
But we may also use Session.flush() to make sure changes are committed.
Reference about flush:
https://stackoverflow.com/questions/3220336/whats-the-use-of-session-flush-in-hibernate
 
Tim Holloway
Saloon Keeper
Posts: 21593
146
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi,
I don't know much details about the code. But I believe Hibernate will automatically commit the changes to the DB after a transaction is completed successfully.
But we may also use Session.flush() to make sure changes are committed.
Reference about flush:
https://stackoverflow.com/questions/3220336/whats-the-use-of-session-flush-in-hibernate



It is important to realize that Hibernate operates in two flavours:

1. Legacy (proprietary) Hibernate, which is based on Session
2. Hibernate JPA, which implements the JEE Standard Java Persistence Architecture components of the Enterprise JavaBean subsystem. JPA uses an EntityManager.

I recommend using Hibernate JPA, not legacy Hibernate. Legacy Hibernate may go away some day and probably will not be as well maintained as Hibernate JPA. Plus JPA is a portable standard API. I have had occasion to switch between Apache OpenJPA and Hibernate JPA in order to gain features and/or dodge bugs and the switch between the two standards implementation is a lot simpler than switching in and out of a proprietary interface.

The flush() method pushes the contents of the Hibernate buffers out to the database server. JPA EntityManager flush() and proprietary Hibernate Session flush() are pretty much identical on that. But that doesn't mean that fetching data from the database will return the flushed-out data. Here you have to distinguish between local Transaction and database transaction contexts. If the local Spring Transaction context being used initiates a database Transaction context, then only when the database Transaction is committed will any other database reading processes see the changed. If Spring initiates a database Transaction automatically then it will automatically do a database Transactiont commit when the Spring framework Transaction is committed.

Spring does offer several different types of Transaction contexts, however, and some of them may allow multiple transactions/commits to be done within a single Spring Transaction. It all depends on how you set things up.

 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!