2014年5月17日星期六

About hibernate 's SessionFactory , and Session question of the use

recently wrote hibernate data manipulation templates. SessionFactory , and Session for the use of springing a doubt , get off to write a transaction object method start ():
public void start () {
try {
stmt = HibernateUtil.getSessionFactory () getCurrentSession ().;
trans = stmt.beginTransaction ();
} catch (HibernateException hibernateException) {
hibernateException.printStackTrace ();
}
}
So every time you call start () method, then SessionFactory object actually been there. If I write a close () method is session.close (), SessionFactory always will not be closed , and that this will have any effect on the performance of it? If the close () method is HibernateUtil.getSessionFactory (). Close (), then the SessionFactory will be closed after each call to start () method will be to create a new SessionFactory and call the close () method to close after this and Hibernate console output from running information found in each call start () method will be to re- load the Hibernate configuration , what impact this will have on the performance of it?
------ Solution ---------------------------------------- ----
SessionFactory itself is not closed , and this is to create a session factory , for the session only need to close
------ Solution -------------- ------------------------------

  
I get SessionFactory is new AnnotationConfiguration (). configure (). buildSessionFactory () itself has provided close () method . If that does not close , then this in itself is not to create more more more? Or that it has always existed and it was the impact on performance and not after creation ?  
sessionFactory do not need to have been created. SessionFactory is time-consuming to build , he needs to resolve hibernate configuration file, and JPA annotations. We are using session operation, only you do not have the entire project only then sessionFactory off.
------ Solution ---------------------------------------- ----
factory for the thing you want to close off the villain who made ​​
------ Solution ------------------------------------------- -
the SessionFactory into something singleton , it has been open it
------ Solution ---------------------- ----------------------
sessionfactory Why close
------ Solution ----------- ---------------------------------

you can create the SessionFactory written in a special class can be called HibernateUtils inside with getSessionFactory get SessionFactory, and this should be a singleton SessionFactory . So as long as dao HibernateUtils.getSessionFactory () on it

public class HibernateUtils{
   private static SessionFactory sessionFactory;
   static{
      sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
   }
   public static SessionFactory getSessionFactory(){
      return sessionFactory;
   }
}

------ Solution ------------------------------------- -------

you come ah. .
------ For reference only ------------------------------------ ---


I get SessionFactory is new AnnotationConfiguration (). configure (). buildSessionFactory () itself has provided close () method . If that does not close , then this in itself is not to create more more more? Or that it has always existed and it was the impact on performance and not after creation ?
------ For reference only -------------------------------------- -

    
I get SessionFactory is new AnnotationConfiguration (). configure (). buildSessionFactory () itself has provided close () method . If that does not close , then this in itself is not to create more more more? Or that it has always existed and it was the impact on performance and not after creation ?          
sessionFactory do not need to have been created. SessionFactory is time-consuming to build , he needs to resolve hibernate configuration file, and JPA annotations. We are using session operation, only you do not have the entire project only then sessionFactory off.  

say so , it should be how to create a SessionFactory is better? I get SessionFactory is placed in start () method , the method that each operation will be called Dao once , is not that it has been created ? Or create a SessionFactory itself is within the scope exist after the entire project is used by all users, other calls start () method can automatically obtain the SessionFactory object has been created ?
------ For reference only -------------------------------------- -


because I think every Dao operation will be used to invoke a method of operating a SessionFactory is created , it does not shut down to create more more more?
------ For reference only -------------------------------------- -

you can create the SessionFactory written in a special class, which can be called HibernateUtils with getSessionFactory get SessionFactory, and this should be a singleton SessionFactory . So as long as dao HibernateUtils.getSessionFactory () on it   
  

public class HibernateUtils{
   private static SessionFactory sessionFactory;
   static{
      sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
   }
   public static SessionFactory getSessionFactory(){
      return sessionFactory;
   }
}
 

So, it is a singleton pattern to create objects in the application to keep only one . Enlightened ! Many thanks for your advice !
------ For reference only -------------------------------------- -


already understand, thank you for help !
------ For reference only -------------------------------------- -

you come ah. .  
you with ah
------ For reference only ---------------------------------------

you come ah . .          
you with ah     

That 's Qingdao , Xiao ?
------ For reference only -------------------------------------- -

you come ah. .                
you with ah               
  
That 's Qingdao , Xiao ?  
Qingdao little brother

没有评论:

发表评论