2015-04-06 132 views
0

我有一個JSP,Servlet(純JSP,Servlet)應用程序,它使用Hibernate。以下是一個表的Hibernate實現類。Hibernate問題`SessionFactory`

DesignationImpl.java

package dao; 

import model.sub.DesigAllBean; 
import java.util.List; 
import model.main.Designation; 
import org.hibernate.Query; 
import org.hibernate.SQLQuery; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 

/** 
* 
* @author Yohan 
*/ 
public class DesignationImpl implements DesignationInterface 
{ 


    @Override 
    public Session openCurrentSession() { 
      Session currentSession = getSessionFactory().openSession(); 
      return currentSession; 
    } 

    @Override 
    public Transaction openTransaction(Session session) { 
     Transaction beginTransaction = session.beginTransaction(); 
     return beginTransaction; 
    } 

    private static SessionFactory getSessionFactory() { 

     Configuration configuration = new Configuration().configure(); 
     StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() 
         .applySettings(configuration.getProperties()); 
     SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build()); 
     return sessionFactory; 
    } 

    @Override 
    public void save(Designation d, Session session) 
    { 
     session.save(d); 
    } 

    @Override 
    public void update(Designation d, Session session) 
    { 
     session.update(d); 
    } 
} 

下面是服務類調用上面的類。

DesignationService的.java

package service; 

import dao.Common; 
import model.sub.*; 
import dao.DesignationImpl; 
import dao.DesignationInterface; 
import java.util.ArrayList; 
import java.util.List; 
import model.main.Designation; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 

/** 
* 
* @author Yohan 
*/ 
public class DesignationService 
{ 
    private DesignationInterface designationInterface; 

    public DesignationService() 
    { 
     designationInterface = new DesignationImpl(); 
    } 

    public Session getSession() 
    { 
     Session session = designationInterface.openCurrentSession(); 
     return session; 
    } 

    public Transaction getTransaction(Session session) 
    { 
     return designationInterface.openTransaction(session); 
    } 

    public String save(Designation d) 
    { 
     Session session = designationInterface.openCurrentSession(); 
     Transaction transaction = null; 
     String result=""; 

     try 
     { 
      transaction = designationInterface.openTransaction(session); 
      designationInterface.save(d,session); 
      transaction.commit(); 
      result = Common.SAVE_SUCCESS; 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 

      if(transaction!=null) 
      { 
       transaction.rollback(); 
      } 
      result = Common.SAVE_ROLLBACK; 
     } 
     finally 
     { 
      session.close(); 
     } 

     return result; 
    } 

    public String update(Designation d) 
    { 
     Session session = designationInterface.openCurrentSession(); 
     Transaction transaction = null; 
     String result=""; 

     try 
     { 
      transaction = designationInterface.openTransaction(session); 
      designationInterface.update(d,session); 
      transaction.commit(); 
      result = Common.SAVE_SUCCESS; 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 

      if(transaction!=null) 
      { 
       transaction.rollback(); 
      } 
      result = Common.SAVE_ROLLBACK; 
     } 
     finally 
     { 
      session.close(); 
     } 

     return result; 
    } 
} 

而且servlet的那樣稱呼他們下面。

DesignationService desigSrvc=new DesignationService(); 

     Designation designation=desigSrvc.findByForiegnKey(idEmployee); 

     Employee empl=new Employee(); 
     empl.setIdEmployee(idEmployee); 

     if(designation.getDateCreated()==null) 
     { 
      designation.setDateCreated(Common.getCurrentDateSQL()); 
     } 

     designation.setEmployee(empl); 
     designation.setDesignation(txtDesignation); 
     designation.setLocation(location); 
     designation.setSalary(salary); 
     designation.setDatePromoted(datePromoted); 
     designation.setLastUpdated(Common.getCurrentDateSQL());  

     desigSrvc.save(designation); 

正如你所看到的,有壞事發生的事情,也就是小服務器創建新SessionFactory情況下,它executes.I時遇到Driver#Connect問題,每一次,我想這可能是它的原因。

我讀了stackoverflow帖子,有些人似乎建議整個應用程序只使用一個SessionFactory。如果它合適,那我該怎麼做呢?也許像下面這樣創建一個單例類,並將其用在我的實現類中?

public class SessionFactoryBuilder 
{ 
    private static SessionFactoryBuilder instance; 
    private static SessionFactory sessionFactory; 

    private SessionFactoryBuilder() 
    { 
    } 

    private static void buildConfig() 
    { 
     Configuration configuration = new Configuration().configure(); 
      StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() 
          .applySettings(configuration.getProperties()); 
      sessionFactory = configuration.buildSessionFactory(builder.build()); 
    } 

    public static SessionFactoryBuilder getInstance() 
    { 
     if(instance == null) 
     { 
      instance = new SessionFactoryBuilder(); 
      buildConfig(); 
     } 
     return instance; 
    } 

    public SessionFactory getSessionFactory() 
    { 
     return sessionFactory; 
    } 
} 

但是那麼線程呢? Servlets是多線程的,不是嗎?

+0

你的DesignationInterface在哪裏? – Pratik 2015-04-06 05:12:30

+0

嗨,你需要建立一個單身人士sessionfactory提供者類..我認爲它應該解決你的poroblem .. – 2015-04-06 05:19:15

回答

2

正如我評論我有HibernateUtil.java類作爲Singleton。這個類可以爲您提供的SessionFactory使用HibernateUtil.getSessionFactory(),你應該從你的DesignationImpl類中刪除了相關的代碼

public class HibernateUtil { 

private static StandardServiceRegistry serviceRegistry; 
private static SessionFactory INSTANCE = null; 
public static SessionFactory getSessionFactory() { 
    if(INSTANCE=null){ 
      createSessionFactory(): 
     } 
     return sessionFactory; 
} 
private synchronized static void createSessionFactory(){ 
    if(INSTANCE!=null){return;} 
    Configuration configuration = new Configuration(); 
    configuration.configure(); 
    SeviceRegistry=newStandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
    sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
    } 
    } 
} 

查找上面的代碼,並請建議/評論確認code.I的正確性認爲穿線將被照顧使用此..我添加一個雙重檢查,以避免多個sessionfactory創建和它的沉重資源。

+0

我也發佈了我的單身人士班。案例是線程問題。無論如何,我已經聽說'SessionFactory'是線程安全的。 – 2015-04-06 05:33:51

+0

DesignationImpl class是什麼意思..?我不認爲它是SingleTon ..我認爲這是因爲它創建sessionfactory每次你調用getSessionFactory()..你能幫我或糾正我.. – 2015-04-06 05:46:34

+0

我在談論'SessionFactoryBuilder'類。在我的問題中,我提到我也創建了它,即使我不使用它。 – 2015-04-06 05:51:00

相關問題