-->
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.  [ 12 posts ] 
Author Message
 Post subject: update before flush
PostPosted: Wed Jun 09, 2010 5:05 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I use the code below to modify an object and update the view:
Code:
Transaction transaction = session.beginTransaction();
try
{
   // Load, edit and update Objects
   // using session.save() and session.update()
   transaction.commit();
}
catch (Exception e)
{
   transaction.rollback();
   Logger.get().error(e.getMessage());
}
rebuildModel(session);            // Loads objects from db using the same session
session.flush();                  // Here the db is updated
session.close();
All changes need to be in the DB before rebuildModel() because this will update the view.
I thought transaction.commit() did the changes in the db, but apparently it doesn't.
Is there an easy solution for this? Or should I close the session and use a new session for rebuildModel()


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 5:51 am 
Newbie

Joined: Wed Mar 03, 2010 3:36 am
Posts: 5
Use Transaction.commit() in place of session.flush()


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 5:57 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
sunilmahulkar wrote:
Use Transaction.commit() in place of session.flush()

Check line 6.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 6:16 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I thought transaction.commit() did the changes in the db, but apparently it doesn't.


Yes, transaction.commit() should commit all changes to the database. It is not apparent to me why you think it doesn't do that. Can you explain this and also answer the following questions:

1. What is going on in the rebuildModel() method? Why do you need to rebuild it?

2. Why do you call session.flush() after the rebuilding? Are you doing additional changes outside the already committed transaction?


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 6:33 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
nordborg wrote:
Yes, transaction.commit() should commit all changes to the database. It is not apparent to me why you think it doesn't do that. Can you explain this and also answer the following questions:

1. What is going on in the rebuildModel() method? Why do you need to rebuild it?
I show a table with a lot of records, including the ones I change in this code. RebuildModel gets all records from the db and fills this table.
Because some records are changed, the table needs to be updated.
nordborg wrote:
Are you doing additional changes outside the already committed transaction?
All changes are done before transaction.commit(), but rebuildModel doesn't always show the latest changes (sometimes it does, or partially).
So this means the changes in the db are not done by transaction.commit(), so I quess it's done on session.flush().
nordborg wrote:
2. Why do you call session.flush() after the rebuilding?
To close my sessions I use a method which does flush and close. After rebuilModel() this method is called.
Adding session.flush() before rebuildModel() doesn't change anything.

Edit: If I close the session before rebuildModel() and open another one to use in rebuildModel(), it does work.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 6:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
So this means the changes in the db are not done by transaction.commit(), so I quess it's done on session.flush().


Which flush-mode are you using? transaction.commit() usually also flushes the session, except when using FlushMode.MANUAL. But in that case it should make more sense to call session.flush() before transaction.commit() and not after.

Have you turned on the logging in Hibernate to see when things are actually sent to the database?

Quote:
If I close the session before rebuildModel() and open another one to use in rebuildModel(), it does work.


This indicates that transaction.commit() is actually working as expected.

Quote:
All changes are done before transaction.commit(), but rebuildModel doesn't always show the latest changes (sometimes it does, or partially).


Are there other transactions also updating the database at the same time? If there are are, your session may not see those changes because it has already cached some information.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 7:30 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I never changed the flushMode, so I use the default.

I checked my logs and the changes are done in session.flush();
But if I use session.flush(), before rebuildModel() it doesn't work. It does when I flush, close and open another session.

I never use multiple transactions at the same time.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 7:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Does your code reach transaction.commit() or is there an exception before this? That could explain a lot of things. Do you know that you are not "allowed" to continue using the same session if an error has happened when using it? http://docs.jboss.org/hibernate/stable/ ... exceptions


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 7:47 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
The code is executed without exceptions.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 8:18 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This doesn't fit. Can you post a more complete code example were you show which entities you are loading and which changes you are making to them. Then include a log from Hibernate that shows that transaction.commit() doesn't send anything to the database, but session.flush() does.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 8:26 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I could, but if I just copy it all, it would be thousands lines of code.
And it would take hours to filter the important stuff, so you can understand it.

So unless someone comes with another suggestion, I'll use a workaround.


Top
 Profile  
 
 Post subject: Re: update before flush
PostPosted: Wed Jun 09, 2010 8:37 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
No point in posting thousands lines of code. The things you describe can't be explained by just the code snipped you showed in the first post. I am pretty sure the problem lies within your code somewhere.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.