我有一個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是多線程的,不是嗎?
你的DesignationInterface在哪裏? – Pratik 2015-04-06 05:12:30
嗨,你需要建立一個單身人士sessionfactory提供者類..我認爲它應該解決你的poroblem .. – 2015-04-06 05:19:15