一開始學習hiebernate的時候,首先感覺上手不是很難,比較容易操作,因爲一開始是在java應用程序裏面運行的,對效率並沒有太高的要求,但是在後來部署在web應用的時候發現太可怕了,插入一條數據竟然要30多秒,這樣的話很顯然是不行的,必須採用優化的方法,不多廢話,優化方法如下:
package com.lab507.test;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.lab507.domain.NlLogs;
import com.lab507.tools.SessionFactoryTools;
import com.opensymphony.xwork2.ActionSupport;
public class test_1Action extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@Override
public String execute() throws Exception {
long start=System.currentTimeMillis();
//實例化對象和設置對象屬性
NlLogs logs=new NlLogs();
logs.setLoginip("55555");
logs.setLoginname("xuning");
logs.setLogintime(new Date());
logs.setStatus(true);
request.put("starttime", new Date().toString());
//獲取sessionfactory,並且顯示獲取的時間
long startsf=System.currentTimeMillis();
//SessionFactory sessionFactory=SessionFactoryTools.GetSessionFactory();
long endsf=System.currentTimeMillis();
System.out.println("獲取sessionfactory用了:"+(endsf-startsf)+"ms");
request.put("getsessionfactorytime", endsf-startsf);
//獲取到session,並且顯示時間
long starts=System.currentTimeMillis();
Session session=SessionFactoryTools.GetCurrentSession();
long ends=System.currentTimeMillis();
System.out.println("獲取session用了:"+(ends-starts)+"ms");
request.put("getsessiontime", ends-ends);
//開始施行後事務。hibernate的dml必須執行事務
session.beginTransaction();
//保存對象
session.save(logs);
//提交事務
session.getTransaction().commit();
//這裏不要去關閉sessionfactory和session,因爲前者爲共用,後者爲自動關閉
//session.close();
//關閉sessionfactory
//sessionFactory.close();
long end=System.currentTimeMillis();
//共計用時:
request.put("sumtime", end-start);
return SUCCESS;
}
@Override
public void setApplication(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.application=arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.session=arg0;
}
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}
}
然後就是上面有一個工具方法,這個方法的作用是非常大的,可以大大的優化hibernate,是線程共用一個sessionfactory
package com.lab507.tools;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*
* 對SessionFactory接口的優化類
*/
public final class SessionFactoryTools {
private static SessionFactory sessionFactory;
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
private SessionFactoryTools(){
}
static{
sessionFactory=new Configuration().configure().buildSessionFactory();
}
public static SessionFactory GetSessionFactory(){
return sessionFactory;
}
public static Session openSession(){
return sessionFactory.openSession();
}
public static Session GetCurrentSession(){
Session session=threadLocal.get();
if(session==null){
session=sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
}