-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: How to make sure every transation is committed?
PostPosted: Wed Feb 13, 2008 5:12 am 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
Hi all,

I am using JDBC transaction management in my JSF application running on tomcat. I am however wondering how to make sure that all queries are committed (which I think is required?).

For example, I have a JSF detail page to show everything of a project. First I do a get of the project:


Code:
   public Project get(String projectId) {
      Session session = Manager.getSessionFactory().getCurrentSession();

      session.beginTransaction();

      Project t = (Project) session.load(Project.class, projectId);

      // TODO Check
      //session.getTransaction().commit();

      return t;
   }


As you can see, I don't commit here to not detach the object. JSF will call some getters while rendering my page, and hibernate will equally need to get these lazy properties from the database.
So, as you can see my transaction is never committed.
- Is that a problem?
- How can I best solve this? Is there a way to get an object with all lazy properties so that I could get them all at once and commit immediately after?

Thanks,
Steven


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 11:42 am 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
Adding to this, I use ThreadLocalSessionContext and so I am wondering where the transaction would actually be destroyed.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 2:09 am 
Newbie

Joined: Thu May 24, 2007 3:01 am
Posts: 11
Hi,

In this scenario (during retrieve operations), we dont use transactions i.e we never start the transaction. I think transaction is not necessary for retrieve operations.

Otherwise we can start the transaction get all the child record and stored it in a pojo, return it and then commit the transaction.

Regards,
Siva


Top
 Profile  
 
 Post subject: Insane
PostPosted: Fri Feb 15, 2008 6:17 pm 
Newbie

Joined: Fri May 11, 2007 3:53 pm
Posts: 12
Are you kidding me?

Read this on sessions and transactions!
http://www.hibernate.org/42.html

It is totally FALSE and BOGUS to state that transactions aren't needed for retrieval.

I guess Christian, Max and the others are tied of fighting such nonsense.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 16, 2008 6:23 am 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
Thanks xantros,

that at least confirms what I was thinking ;) I read in some hibernate article that with the threaded method the transactions are started with beginTransaction() and should be closed at all times since Hibernate cannot know when the JVM will cut off the thread.

Still my question stands. For those who are not familiar with JSF, I have a Java bean where in the constructor I retrieve an object from my DAO. The object is however not fully retrieved because if has several properties defined "lazy".

Then, later on in the execution, JSF calls getObject().getName() . Now if getName() is lazy, the object must not be detached which is why I don't commit.

Just an idea: Should I maybe perform a commit() in the finalise() method of my bean then?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 16, 2008 7:47 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
tomba wrote:
Then, later on in the execution, JSF calls getObject().getName() . Now if getName() is lazy, the object must not be detached which is why I don't commit.

Well, in fact it is NOT a question of commit or not commit - it is a question of keeping the session opened (which you apparently are doing) or closing the session. If you would close the session, you would have a detached object and calling later "getObject().getName()" could result in the famous LazyInstantionException.

So, the whole point in such a scenario is: if you keep the session opened, you must eventually come to a point where you are closing the session. Else your session would grow and grow with persistent objects and you would get a memory problem.

Remember: trransaction.commit and session.close are totally different things.

Carlo


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 16, 2008 10:52 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
have you looked into open session in view? You can implement it as either a servlet filter or an aspect.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 4:50 am 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
farzad wrote:
have you looked into open session in view? You can implement it as either a servlet filter or an aspect.


Farzad-

Thanks,
could you please explain a bit what you mean by this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 11:31 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
tomba wrote:
Thanks,
could you please explain a bit what you mean by this?


I can't explain a lot more than this: http://www.hibernate.org/43.html



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 4:45 pm 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
Great! Thank you.

Still one more thing... when you perform an update and later require the changed data, won't you need to commit in between those two statements?

If so, it is not sure that at the end of the page a session is still open (ie not committed), so what will commit() do then?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 4:53 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You won't have a problem if the two are in the same transaction. Modified data is visible to the same transaction (and possibly to other transactions) and a commit is not needed for that. If transactions are isolated from each other then a commit is needed so that the data modified in transaction T1 is visible to transaction T2, and this is not your case as I understand.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 5:19 pm 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
Going to give it a try tomorrow ;)

Thinking about it though, I hope beginTransaction() is a lightweight action since a lot of non-db pages will have this executed too... :s


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 5:21 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
tomba wrote:
Going to give it a try tomorrow ;)

Thinking about it though, I hope beginTransaction() is a lightweight action since a lot of non-db pages will have this executed too... :s



You should be able to tweak it per command sent to the sever. It's true not every action needs a hibernate session and/or transaction.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 24, 2008 6:10 pm 
Newbie

Joined: Mon Mar 05, 2007 4:38 am
Posts: 17
Allright, things worked out fine.
In fact it also solved my problems with broken pipes to the mysql database.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.