用hibernate连接两个数据库
hibernate只是用来连接数据库的,没有其它操作,现在想从别的服务器上搜点数据,不会用hibernate配置了
开始是hibernate.cfg.xml
<?xml version= '1.0 ' encoding= 'UTF-8 '?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name= "hibernate.connection.url ">
jdbc:microsoft:sqlserver://*.*.*.11:1433;;SelectMethod=cursor;databaseName=**
</property>
<property name= "hibernate.connection.driver_class ">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name= "hibernate.connection.username "> ** </property>
<property name= "hibernate.connection.password "> ** </property>
<property name= "hibernate.show_sql "> True </property>
<property name= "hibernate.use_outer_join "> True </property>
<property name= "c3p0.max_size "> 150 </property>
<property name= "c3p0.min_size "> 20 </property>
<property name= "c3p0.timeout "> 5000 </property>
<property name= "c3p0.max_statements "> 200 </property>
<property name= "c3p0.idle_test_period "> 3000 </property>
<property name= "c3p0.acquire_increment "> 2 </property>
<property name= "c3p0.validate false "> false </property>
</session-factory>
</hibernate-configuration>
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
HibernateUtil.java
package whwd.hibernate;
import org.hibernate.* ;
import org.hibernate.cfg.* ;
import org.apache.log4j.Logger;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
public class HibernateUtil
{
//private static Log log = LogFactory.getLog(HibernateUtil.class) ;
private static Logger log = Logger.getLogger( "HibernateUtil.class ");
private static final SessionFactory sessionFactory;
static
{
try
{
Configuration cfg = new Configuration() ;
//cfg.addClass(Cat.class) ;
//sessionFactory = cfg.buildSessionFactory() ;
sessionFactory = cfg.configure( "/hibernate.cfg.xml ").buildSessionFactory();
}
catch (Throwable ex)
{
log.error( "初始化 SessionFactory creation failed. ", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
@SuppressWarnings( "unchecked ")
public static Session currentSession() throws HibernateException
{
Session s = (Session)session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
@SuppressWarnings( "unchecked ")
public static void closeSession() throws HibernateException
{
Session s = (Session)session.get();
session.set(null);
if (s != null)
s.close();
}
@SuppressWarnings( "unchecked ")
public static void setSession( Object obj ) throws HibernateException
{
session.set(obj);
}
public static Object getSession() throws HibernateException
{
return session.get() ;
}
}
这是之前连接一个服务器上的数据库时的代码,可以执行
[解决办法]
呵呵,你修改sessionFactory,那么不是耗费资源啊,更换一次连接就要重新加载一遍
[解决办法]
自己得分自己接,不要鄙视我呀,没办法,穷人呀
[解决办法]
那有什么好的办法吗?