-->
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.  [ 5 posts ] 
Author Message
 Post subject: flush() does not commit to database
PostPosted: Sat Sep 27, 2008 6:32 am 
Newbie

Joined: Sat Sep 27, 2008 6:24 am
Posts: 3
Hi to all,

I'm using hibernate 3.2, jdk5, & mysql5, I'm following the samples in "Hibernate quickly".

when I use flush() to save a record it doesn't commit in the database(MySql)?

Code:
    Student student = new Student():
    // set student parameters
    session.save(student);
    session.flush(); // does not commit to DB???
    session.close();   


but it works if I use transaction.commit()

Code:
    Student student = new Student();
    // set student parameters
    session.save(student);
    transaction.commit();   



Why does flush doesn't work for me, do I have to configure something in hibernate? am I doing something wrong?

Thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2008 12:05 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Because the code above is not wrapped into a transaction.

Where is the context in which that code runs? If it uses annotations or declarative transactions, make sure that the method calling the 1st code is declared as transactional.

If it is just a sample, client code, manually write code to wrap in into a transaction. As a general rule, everything needs a transaction.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2008 1:30 am 
Newbie

Joined: Sat Sep 27, 2008 6:24 am
Posts: 3
How do I declare the method as transactional? (sorry for dumb question)

I didn't use any annotations for the method. Do you mean by "wrapping it in a transaction", is that I should always create a transaction and use the transaction.commit().

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2008 2:09 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
You can create a transaction around your code, either programatically (by manually writing transaction code) of declaratively.

Programatically, it means that you create a transaction with code( begin, commit, etc ) on each database interaction.

Code:
Transaction trx=session. ...
trx.begin....

[your persistence code here]

trx.commit();
}catch(...
trx.rollback()


"Declaratively", means that you are using some sort of "managed environment" (such as Spring) that takes care, mainly through configuration external to the code itself, of creating transactions where necessary.

(BTW, if you want the simplest example on how to integrate Hibernate with Spring, go here:
http://hibernar.org/articulos_en/spring ... xample.php
)

Annotations are one of the tools used by managed environments such as Spring to declaratively create transactions as needed. It is the most modern and easy strategy (shown in my article).

Previous declarative strategies to produce transactions made a more low-leve use of AOP (aspect oriented programming) or "Transaction Proxies" declared in your application context wrapping your business objects.

I don't know what the nature of your application is.

If it is a small, trivial application, you can write the transaction code programmatically (by hand).
If it is a web application, use Spring for creatig transactions declaratively, and remove most (or all) the explicit transaction code from your Hibernate logic.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 28, 2008 3:05 am 
Newbie

Joined: Sat Sep 27, 2008 6:24 am
Posts: 3
Thanks a lot Gonzalo.


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