日期:2014-05-16 浏览次数:20532 次
HibernateUtil.java
package cn.jsprun;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate数据库连接工具
* 参考:JspRun 源码
* @author SUN
*
*/
public final class HibernateUtil { // final 不能被继承
/** sf 只提供类内部访问,且只有一份实例。 */
private static SessionFactory sessionFactory = null;
private static String message = null;
/**
* 加载到内存时第一个被初始化,且只初始化一次。
*/
static {
buildSessionFactory(); // 构建SF
}
/**
* 构建数据库全局会话工厂对象
*/
public static synchronized void buildSessionFactory() {
// 检查sf不为null才去构建sf对象
if (sessionFactory == null) { // true 为null
try {
// 读取数据库连接配置信息
Properties properties = new Properties();
InputStream fis = new FileInputStream("D:/workspaceee/netzz/src/config.properties");
properties.load(fis);
fis.close();
String dbhost = properties.getProperty("dbhost");
String dbport = properties.getProperty("dbport");
String dbname = properties.getProperty("dbname");
String dbuser = properties.getProperty("dbuser");
String dbpw = properties.getProperty("dbpw");
// 检查数据库配置信息是否可以连接
if (mysql_connect(dbhost, dbport, dbname, dbuser, dbpw)) { // true 可用
// 创建并设置hibernate属性对象
Properties extraProperties = new Properties();
extraProperties.setProperty("hibernate.connection.url",
"jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?zeroDateTimeBehavior=convertToNull");
extraProperties.setProperty("hibernate.connection.username", dbuser);
extraProperties.setProperty("hibernate.connection.password", dbpw);
// 构建SessionFactory
Configuration configuration = new Configuration(); // 创建hibernate配置对象
configuration = configuration.configure("hibernate.cfg.xml"); // 获得hibernate配置文件
configuration = configuration.addProperties(extraProperties); // 添加属性对象
sessionFactory = configuration.buildSessionFactory(); // 构建SF数据库会话工厂对象
// 释放不在使用的资源
extraProperties = null;
configuration = null;
}
properties = null;
} catch (Exception e) {
message = "Create sessionFactory Exception! " + e.getMessage();
}
}
}
/**
* 获得sf
* @return
*/
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
buildSessionFactory();
}
return sessionFactory;
}
/**
* 获得session
* @return
*/
public static Session getSession() {
if (sessionFactory == null) {
buildSessionFactory();
}
return sessionFactory.getCurrentSession();
}
/**
* 重新构建sf
*/
public static void rebuildSessionFactory() {
try {
Properties properties = new Properties();
InputStream fis = new FileInputStream("c:/config.properties");
properties.load(fis);
fis.close();
String dbhost = properties.getProperty("dbhost");
String dbport = properties.getProperty("dbport");
String dbname = properties.getProperty("dbname");
String dbuser = properties.getProperty("dbuser");
String dbpw = properties.getProperty("dbpw");
if (mysql_connect(dbhost, dbport, dbname, dbuser, dbpw)) {
Properties extraProperties = new Properties();
extraProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname
+ "?zeroDateTimeBehavior=convertToNull");
extraProperties.setProperty("hibernate.connection.username", dbuser);
extraProperties.setProperty("hibernate.connection.password", dbpw);
Configuration configuration = new Configuration();
configuration = configuration.configure("hibernate.cfg.xml");
configuration = configuration.addProperties(extraProperties);
sessionFactory = configuration.buildSessionFactory();
extraProperties = null;
configuration = null;
}
properties = null;
} catch (Exception e) {