-->
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.  [ 1 post ] 
Author Message
 Post subject: Flushing and closing a session
PostPosted: Tue Dec 29, 2009 6:30 am 
Newbie

Joined: Thu Aug 13, 2009 3:52 am
Posts: 3
Hi!
I'm looking into an old application that uses methods like the following when saving objects to the database.

Code:
    public void saveMyObject(MyObject myObject) {
        Session session = sessionFactory.openSession();
        try{           
            session.saveOrUpdate("MyObject", myObject);
        } finally {
            session.flush();
            session.close();
        }   
    }


When looking at it I started wondering, what if session.flush() throws an exception? Then close will never be called and the connection remains open?
Or does flush() handle closing the session in case of an exception?
Would it be better to do the flush in a try-catch-ignore kind of clause?

Fetching objects are done like:
Code:
    public Collection getSomeOtherObjects() {
        Session session = sessionFactory.openSession();
        try{
            Query listQ =  session.createQuery("from MyOtherObject as MyOtherObject");
            List items = listQ.list();
            return items;
        } finally {
            session.flush();
            session.close();
        }
    }

Is the flush really needed there?

Best,
E


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

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.