Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Hibernate] Hit the database at a specific moment

 
biraji gafiff
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, at a specific moment in my application I want to commit the transaction, that's the only moment I want to do that. For example:

In #1 I have to hit the database, I could make a method in my DAO to commit the transaction but I think this is not a clever solution. So I wonder if there is a better way to handle this kind of situation. I am trying Interceptor (hibernate) and HibernateInterceptor (spring) but I lost.
I am using Spring too.

Tanks.

[ March 18, 2008: Message edited by: biraji gafiff ]
[ March 18, 2008: Message edited by: biraji gafiff ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try flush(). Just call flush on the Session and it will commit to the database.

Mark
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hitting the database and committing a transaction are really two different things. Is it that you want to write to the database after the unit of work, or you actually want to commit the transaction.

Indeed, flush() will force the Hibernate Session to write any information it has to the database, but I don't believe the flush actually forces a commit? I think the commit happens after the flush, and there is a potential that the data written during the flush may be rolled back by a failed transaction.

public void flush()
throws HibernateException

Force this session to flush. Must be called at the end of a unit of work, before commiting the transaction and closing the session (depending on flush-mode, Transaction.commit() calls this method).

Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.


Hibernate Session flush() method JavaDoc

I agree with Mark, I believe the flush() will accomplish what you require.

-Cameron McKenzie
[ March 18, 2008: Message edited by: Cameron Wallace McKenzie ]
 
biraji gafiff
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, my code was wrong, flush worked fine.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic