一、HibernateSessionFactory.java
 
package com.xh.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class InitSessionFactory {
 private InitSessionFactory() {
 }
 private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
 private static final Configuration cfg = new Configuration();
 private static org.hibernate.SessionFactory sessionFactory;
 public static SessionFactory getInstance() {
  if (sessionFactory == null)
   initSessionFactory();
  return sessionFactory;
 }
 private static synchronized void initSessionFactory() {
  if (sessionFactory == null) {
   try {
    cfg.configure(CONFIG_FILE_LOCATION);
    sessionFactory = cfg.buildSessionFactory();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}
 
 
二、hibernate.cfg.xml
 
<property name="current_session_context_class">thread</property>
 
 
三、TestSession
 
package com.xh.junit;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.xh.hibernate.HibernateSessionFactory;
import com.xh.hibernate.vo.Test;
public class TestSession extends TestCase {
 public HibernateConnection(String name) {
  super(name);
 }
 protected void setUp() throws Exception {
 }
 protected void tearDown() throws Exception {
 }
 public void testInsert() {
  Session session = null;
  Transaction tx = null;
  Test test = null;
  try {
   session = HibernateSessionFactory.getInstance().getCurrentSession();
   tx = session.beginTransaction();
   test = new Test();
   test.setFirstname("mao");
   test.setLastname("mao");
   session.save(test);
   tx.commit();
  } catch (HibernateException he) {
   he.printStackTrace();
   if (tx != null && tx.isActive())
    tx.rollback();
  }
 }
 public void testResult() {
  Session session = null;
  Transaction tx = null;
  List list = null;
  Test test = null;
  try {
   session = HibernateSessionFactory.getInstance().getCurrentSession();
   tx = session.beginTransaction();
   list = session.createQuery("from Test").list();
   for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    test = (Test) iterator.next();
    System.out.println("firstname: " + test.getFirstname());
   }
   tx.commit();
  } catch (HibernateException he) {
   he.printStackTrace();
   if (tx != null && tx.isActive())
    tx.rollback();
  }
 }
}
 
 
四、log4j输出日志
 
00:30:29,671 DEBUG JDBCContext:199 - after transaction completion
00:30:29,671 DEBUG ConnectionManager:398 - aggressively releasing JDBC connection
00:30:29,671 DEBUG ConnectionManager:435 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
00:30:29,687 DEBUG SessionImpl:417 - after transaction completion
00:30:29,687 DEBUG SessionImpl:348 - automatically closing session
00:30:29,687 DEBUG SessionImpl:268 - closing session
00:30:29,687 DEBUG ConnectionManager:369 - connection already null in cleanup : no action
 
 
注:ThreadLocal Session 模式下在事务提交后 session 自动关闭。