-->
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.  [ 8 posts ] 
Author Message
 Post subject: Implications of Session s = factory.getCurrentSession
PostPosted: Tue Mar 31, 2009 10:41 am 
Newbie

Joined: Mon Mar 30, 2009 5:48 pm
Posts: 6
Hibernate version: 3

Name and version of the database you are using:MYSQL 5

Code:
Session s = factory.getCurrentSession();
s.beginTransaction();
s.getTransaction().commit();
factory.close();


This is fairly simple I have seen this code somewhere and I am wondering what it is doing, it seems weird to me. help would be much appreciated.

Note: I am not doing a factory.openSession(); and I think its weird that I am closing the factory... Comments please


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 4:53 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Code:
Session s = factory.getCurrentSession();

This gets you a session or a new session if one is not already open
A session is what manages your jdbc connection and your annotated/mapped pojo's.
You will find that if you load pojo's in one session and try to save them in another you won't be able to.
Also look here for more information on the sessionfactory
http://www.hibernate.org/hib_docs/v3/ap ... ctory.html
and here for the session
http://www.hibernate.org/hib_docs/v3/ap ... ssion.html

Code:
s.beginTransaction();

This begins a transaction. Transactions are a way of performing a set of tasks as one large job. In the api it describes this method like this.
Quote:
Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction. The class of the returned Transaction object is determined by the property hibernate.transaction_factory


Code:
s.getTransaction().commit();

This will commit all of the tasks which were done since the time you called s.beginTransaction()

for instance if you had code like this
Code:
s.beginTransaction();
s.update(myObject01);
s.delete(myObject02);
s.save(myObject03);
s.getTransaction().commit();

Then you would have updated, deleted and saved an object all in one transaction.


Code:
factory.close();

from hibernates api docs
Quote:
Destroy this SessionFactory and release all resources (caches, connection pools, etc). It is the responsibility of the application to ensure that there are no open Sessions before calling close().


You may not however want to close the factory and usually we will close the session instead. As opening and closing connection pools could be very expensive if this is done often.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 10:50 am 
Newbie

Joined: Mon Mar 30, 2009 5:48 pm
Posts: 6
One Follow-up question is I have written the following code (using the factory singleton from the hibernate tutorial).

Code:
if(HibernateUtil.getSessionFactory().isClosed())
   System.out.println("factory has been closed!");
System.out.println("Factory hash:"
        +HibernateUtil.getSessionFactory().hashCode());
HibernateUtil.getSessionFactory().close();
if(HibernateUtil.getSessionFactory().isClosed())
   System.out.println("factory has been closed!");
System.out.println("Factory hash: "
       +HibernateUtil.getSessionFactory().hashCode());
if(HibernateUtil.getSessionFactory().isClosed())
   System.out.println("factory has been closed!");


and the output is the following
Code:
Factory hash: 9838079
07:46:07,525  INFO SessionFactoryImpl:769 - closing
07:46:07,525  INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:mysql://localhost/xwiki?useServerPrepStmts=false
factory has been closed!
Factory hash: 9838079
factory has been closed!


why is it that when I close a factory I can go ahead, and use it still with no apparent problems?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 11:50 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
I may not be the right person to answer this question, but i'll give you what I do know about the session factory. W

hen you close the factory you are not disposing of the object itself. You are just letting go of the connection pool and whatever else is described in the documentation above. So the hashcode would be the same. I cannot find the docs on hibernateutil easily and I have never actually used it myself. But I suspect it's reusing the same factory and handing it back to you.

You should really go through the docs, a lot of these questions are answered in there. Possibly hibernateutils is not part of the hibernate library? Just type into google the package and hibernate utils the docs should be there somewhere and take a look at the documentation on the method you want to know about.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 11:53 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Oh and just to note hashcode does not necessarily mean a java object is unique. So it might very well be returning different objects with the same hashcode.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 6:37 pm 
Newbie

Joined: Mon Mar 30, 2009 5:48 pm
Posts: 6
I didn't know that how do you see if you are dealing with the same object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 6:54 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
I don't really know, unless sessionfactory implemented a hashcode that does return a unique value.

Most hashcode implementations when implemented well however only return a unique value for the contents of the object.

Don't take this as good code because it's not but say we had an object with a firstName and lastName property.

Sometimes a hashcode might take these 2 strings and concatenate them then hash them to arrive at a unique value per a firstName and lastName.

So if we have 2 objects that have the same firstName and lastName they will have the same hashcode.

Rarely have I seen that someone makes the hashcode return a unique value for an instance of a class. I would guess without looking at the source code that it is unique per it's contents and not per it's instance.

I still can't find the proper docs for the HibernateUtils class, but if you do find the api I would trust what it says. If it's telling you that it's returning a new sessionFactory then it is.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 10:33 am 
Newbie

Joined: Mon Mar 30, 2009 5:48 pm
Posts: 6
HibernateUtil is a template recomended in the hibernate documentation

Code:
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    /**
     * Accessor method for single SessionFactory object
     *
     * @return The single SessionFactory in the application
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }



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