javase项目中使用hibernate如何不用重复buildSessionFactory,我的项目里面,每操作一次数据可靠就读取一次hibernate.cfg.xml文件,最后报一些这样的错误:
Connections could not be acquired from the underlying database!
.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
[b]问题补充:[/b]
谢谢yourgame ,我有几个问题想请教下
package cn.hlgc.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
HibernateSession工厂
*/
public class HibernateUtil {
/** Hibernate的配置文件hibernate.cfg.xml /
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/* Session的线程本地变量 /
private static final ThreadLocal threadLocal = new ThreadLocal();
/* transaction的线程本地变量 /
private static final ThreadLocal txThreadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
[u] /*我的所有的类都继承这个类了,所以我把这个构造方法改为public了,这样做有什么坏影响吗?(这里写成私有的话,它的子类会被提示错误:Implicit super constructor HibernateUtil() is not visible. Must explicitly
invoke another constructor)**/[/u]
private HibernateUtil() {
}
/**
@throws HibernateException
*/
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;
}
/**
/**[size=x-large][size=large][u]还有这里什么时候关闭它呢,我的项目里免现在是用session.close()关闭一个session的,如果我一直不调用这个方法,有影响吗[/u][/size][/size]
@throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
/**
/**
/**
/**
/**
}
[code="java"] /** * Rebuild hibernate session factory */ public synchronized static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err.println("%%%% 创建 SessionFactory 失败 %%%%"); e.printStackTrace(); } }
[/code]
或者这样就可以了
[code="java"]package cn.hlgc.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
HibernateSession工厂
*/
public class HibernateUtil {
/** Hibernate的配置文件hibernate.cfg.xml /
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/* Session的线程本地变量 /
private static final ThreadLocal threadLocal = new ThreadLocal();
/* transaction的线程本地变量 */
private static final ThreadLocal txThreadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateUtil() {
}
/**
@throws HibernateException
*/
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;
}
/**
/**
@throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
/**
/**
/**
/**
/**
}[/code]
[code="java"] /** * Rebuild hibernate session factory */
public synchronized static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% 创建 SessionFactory 失败 %%%%");
e.printStackTrace();
}
}
[/code]
一般你写在一个静态方法里面,他就只创建一次了.以后要使用你就调用静态方法就可以了
继承他做什么?他是工具类来的