各位高人: 本人有一项目要连接多个数据库.(Hibernate3.5,无Spring),根据用户选择版本不同,登录不同数据库。请问如何动态改变连接.
非常感激!!! 在线等....
email:[email protected] 连接文件如下: package com.sae.pls.hibernate;import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil
{
private static final SessionFactory mysessionFactory;
//Product PLS DB
//private static String CONFIG_FILE="/Pls_hibernate.cfg.xml";
//Test PLS DB
public static String ConfigFile="/TestPls_hibernate.cfg.xml";
static final Configuration myconfig;
static
{
try
{
myconfig=new Configuration();
myconfig.configure(ConfigFile);
mysessionFactory =myconfig.buildSessionFactory();
}
catch(Exception ex)
{
System.out.println("HibernateUtil.init Script error: " + ex.toString());
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal threadLocal=new ThreadLocal();
public static Session currentSession()
{
Session currentSession=(Session)threadLocal.get();
if(currentSession==null)
{
currentSession=mysessionFactory.openSession();
threadLocal.set(currentSession);
}
return currentSession;
}
public static void closeSession()
{
Session currentSession=(Session)threadLocal.get();
if(currentSession!=null)
{
currentSession.close();
}
threadLocal.set(null);
}}
非常感激!!! 在线等....
email:[email protected] 连接文件如下: package com.sae.pls.hibernate;import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil
{
private static final SessionFactory mysessionFactory;
//Product PLS DB
//private static String CONFIG_FILE="/Pls_hibernate.cfg.xml";
//Test PLS DB
public static String ConfigFile="/TestPls_hibernate.cfg.xml";
static final Configuration myconfig;
static
{
try
{
myconfig=new Configuration();
myconfig.configure(ConfigFile);
mysessionFactory =myconfig.buildSessionFactory();
}
catch(Exception ex)
{
System.out.println("HibernateUtil.init Script error: " + ex.toString());
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal threadLocal=new ThreadLocal();
public static Session currentSession()
{
Session currentSession=(Session)threadLocal.get();
if(currentSession==null)
{
currentSession=mysessionFactory.openSession();
threadLocal.set(currentSession);
}
return currentSession;
}
public static void closeSession()
{
Session currentSession=(Session)threadLocal.get();
if(currentSession!=null)
{
currentSession.close();
}
threadLocal.set(null);
}}
配置文件为xml,就需要使用到解析和生成xml的知识了!