hibernateutils.java

来自「dorado 展现中间件是一款企业级的商用AJAX平台软件」· Java 代码 · 共 52 行

JAVA
52
字号
package sample.skills.data.hibernate;

import java.sql.Connection;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.bstek.dorado.common.ds.ConnectionHelper;

public class HibernateUtils {
  private static final String CONFIGURE =
      "sample/skills/data/hibernate/hibernate.cfg.xml";
  private static final SessionFactory sessionFactory;

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

  public static final ThreadLocal session = new ThreadLocal();

  public static Session openSession()
      throws Exception {
    Session s = (Session)session.get();
    // Open a new Session, if this Thread has none yet
    if (s == null) {
      Connection connection = ConnectionHelper.getDefaultConnection();
      s = sessionFactory.openSession(connection);
      session.set(s);
    }
    return s;
  }

  public static void closeSession()
      throws Exception {
    Session s = (Session)session.get();
    if (s != null) {
      Connection connection = s.close();
      connection.close();
    }
    session.set(null);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?