Advertisement
ARIELCARRARO

SessionFactory.java

Nov 28th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1.  
  2.  
  3. package org.util;
  4.  
  5.  
  6. import org.hibernate.HibernateException;
  7. import org.hibernate.Session;
  8. import org.hibernate.cfg.Configuration;
  9.  
  10. /**
  11. * Configures and provides access to Hibernate sessions, tied to the
  12. * current thread of execution.  Follows the Thread Local Session
  13. * pattern, see {@link http://hibernate.org/42.html }.
  14. */
  15. public class SessionFactory {
  16.  
  17. /**
  18. * Location of hibernate.cfg.xml file.
  19. * Location should be on the classpath as Hibernate uses
  20. * #resourceAsStream style lookup for its configuration file.
  21. * The default classpath location of the hibernate config file is
  22. * in the default package. Use #setConfigFile() to update
  23. * the location of the configuration file for the current session.
  24. */
  25. private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";
  26. private static final ThreadLocal threadLocal = new ThreadLocal();
  27. private  static Configuration configuration = new Configuration();
  28. private static org.hibernate.SessionFactory sessionFactory;
  29. private static String configFile = CONFIG_FILE_LOCATION;
  30.  
  31. private SessionFactory() {
  32. }
  33.  
  34. /**
  35. * Returns the ThreadLocal Session instance.  Lazy initialize
  36. * the SessionFactory if needed.
  37. *
  38. *  @return Session
  39. *  @throws HibernateException
  40. */
  41. public static Session getSession() throws HibernateException {
  42.    Session session = (Session) threadLocal.get();
  43.  
  44.    if (session == null || !session.isOpen()) {
  45. //       System.out.println("<<<<<<<<<>>>>>>>");
  46.        if (sessionFactory == null) {
  47.            rebuildSessionFactory();
  48.        }
  49.        session = (sessionFactory != null) ? sessionFactory.openSession()
  50.                : null;
  51.        threadLocal.set(session);
  52.    }
  53.  
  54.    return session;
  55. }
  56.  
  57. /**
  58. *  Rebuild hibernate session factory
  59. *
  60. */
  61. public static void rebuildSessionFactory() {
  62.    try {
  63.        configuration.configure(configFile);
  64.        sessionFactory = configuration.buildSessionFactory();
  65.    } catch (Exception e) {
  66.        System.err
  67.                .println("%%%% Error Creating SessionFactory %%%%");
  68.        e.printStackTrace();
  69.    }
  70. }
  71.  
  72. /**
  73. *  Close the single hibernate session instance.
  74. *
  75. *  @throws HibernateException
  76. */
  77. public static void closeSession() throws HibernateException {
  78.    Session session = (Session) threadLocal.get();
  79.    threadLocal.set(null);
  80.  
  81.    if (session != null) {
  82.        session.close();
  83.    }
  84. }
  85.  
  86. /**
  87. *  return session factory
  88. *
  89. */
  90. public static org.hibernate.SessionFactory getSessionFactory() {
  91.    return sessionFactory;
  92. }
  93.  
  94. /**
  95. *  return session factory
  96. *
  97. *    session factory will be rebuilded in the next call
  98. */
  99. public static void setConfigFile(String configFile) {
  100.    SessionFactory.configFile = configFile;
  101.    sessionFactory = null;
  102. }
  103.  
  104. /**
  105. *  return hibernate configuration
  106. *
  107. */
  108. public static Configuration getConfiguration() {
  109.    return configuration;
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement