当前位置: 代码迷 >> Java Web开发 >> [求助]请教应用hibernate的线程问题!
  详细解决方案

[求助]请教应用hibernate的线程问题!

热度:227   发布时间:2006-12-15 13:16:18.0
[求助]请教应用hibernate的线程问题!

用myeclipse生成hibernate的时候,同时了这个类。


package com.jct.common.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {


private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

public HibernateSessionFactory() {
}

public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}


public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}


public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}


public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}


public static Configuration getConfiguration() {
return configuration;
}

}

搜索更多相关主题的帖子: 线程  hibernate  应用  

----------------解决方案--------------------------------------------------------

HibernateSessionFactory 是一个线程
如果把每一个表对应一个DAO的话,那么再这个程序中,我写多个DAO的会不会成为一个多线程?
JctCtyDAO.class,JctCttDao.class(如果每次只操纵HibernateSessionFactory 话,肯定是一个现成。但现在我用两个不同的类继承了他,会不会产生2个Thread?)

public class JctCtyDAO extends HibernateSessionFactory {
public boolean insert(JctCty cty){
Session session = super.getSession();
Transaction ts = session.beginTransaction();
session.save(cty);
session.flush();
ts.commit();
return true;
}
}
___________

public class JctCttDAO extends HibernateSessionFactory {
public boolean insert(JctCtt ctt){
Session session = super.getSession();
Transaction ts = session.beginTransaction();
session.save(ctt);
session.flush();
ts.commit();
return true;
}
}

[此贴子已经被作者于2006-12-15 13:22:04编辑过]


----------------解决方案--------------------------------------------------------
首先你的知道你为什么那么做!
之所以用工厂方法的原因之一就是想只实例话一个。

[此贴子已经被作者于2006-12-15 13:39:14编辑过]


----------------解决方案--------------------------------------------------------

这样产生的线程是几个?2个?1个?

[此贴子已经被作者于2006-12-15 14:03:19编辑过]


----------------解决方案--------------------------------------------------------

不知道你说的线程是什么意思,这个线程有什么关系?


----------------解决方案--------------------------------------------------------
不懂!!
----------------解决方案--------------------------------------------------------
好象
不会

Hibernate是线程安全的

你只要不用Thread就应该不会产生新线程

----------------解决方案--------------------------------------------------------
  相关解决方案