test.java

来自「sturts好例子」· Java 代码 · 共 42 行

JAVA
42
字号
package com.db;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sf;
public static final ThreadLocal sessionThread = new ThreadLocal();
public Test(){}



  public static Session currentSession() throws HibernateException {  
        
	    Session s = (Session) sessionThread.get();        
        if (s == null) {  
        	try{
            SessionFactory sf = new Configuration().configure().buildSessionFactory();  
             s = sf.openSession();  
            sessionThread.set(s);     
        	}catch(Exception e){System.out.println("SessionFactory Initial error!");}
         }  
         return s;  
     }  
public static String getInfor(){
	return "this is an information";
}



public static void closeSession() {
	Session s = (Session) sessionThread.get();
	if (s != null)
		s.close();
		sessionThread.set(null);
	}
}

⌨️ 快捷键说明

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