可以提供两个 hibernate1.cfg.xml ; hibernate2.cfg.xml
在 你用HbernateSessionFactory的时候。。这样子写public class HibernateSessionFactory {
private static SessionFactory sessionFactory; public synchronized static Session openSession_db1() throws HibernateException {
if (sessionFactory == null) {
init_db1();
}
return sessionFactory.openSession();
}
public synchronized static Session openSession_db2() throws HibernateException {
if (sessionFactory == null) {
init_db2();
}
return sessionFactory.openSession();
} private static void init_db1() throws HibernateException {
File file = new File("hibernate1.cfg.xml");
Configuration conf = new Configuration().configure(file);
try {
sessionFactory = conf.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
private static void init_db2() throws HibernateException {
File file = new File("hibernate2.cfg.xml");
Configuration conf = new Configuration().configure(file);
try {
sessionFactory = conf.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}================================
而在你的DAO中,获得Session的时候
连接数据库1 Session session1 = HibernateSessionFactory.openSession_db1();
Session session2 = HibernateSessionFactory.openSession_db2();
其他的都一样了。
在 你用HbernateSessionFactory的时候。。这样子写public class HibernateSessionFactory {
private static SessionFactory sessionFactory; public synchronized static Session openSession_db1() throws HibernateException {
if (sessionFactory == null) {
init_db1();
}
return sessionFactory.openSession();
}
public synchronized static Session openSession_db2() throws HibernateException {
if (sessionFactory == null) {
init_db2();
}
return sessionFactory.openSession();
} private static void init_db1() throws HibernateException {
File file = new File("hibernate1.cfg.xml");
Configuration conf = new Configuration().configure(file);
try {
sessionFactory = conf.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
private static void init_db2() throws HibernateException {
File file = new File("hibernate2.cfg.xml");
Configuration conf = new Configuration().configure(file);
try {
sessionFactory = conf.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}================================
而在你的DAO中,获得Session的时候
连接数据库1 Session session1 = HibernateSessionFactory.openSession_db1();
Session session2 = HibernateSessionFactory.openSession_db2();
其他的都一样了。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货