Hibernate动态连接多数据库改进篇
本人曾经写过一篇
Hibernate根据方言dialect动态连接多数据库 的文章,发现效率不高,每次访问其他数据库,都要动态生成一个 sessionFactory实例,不算是个好的解决方法,后来查看hibernate源码,发现org.hibernate.cfg.Configuration类中有一个保护方法:
protected void reset() {
……
}
故重写Configuration类,在动态的跳转不同数据库的时候,不用重新生成sessionFactory实例,发现效果不错。
故以此记录我的方法:
1. 自己重写的Configuration类,extends org.hibernate.cfg.Configuration :
public class HibernateConfiguration extends org.hibernate.cfg.Configuration {
public HibernateConfiguration() {
super();
}
public void reset() {
super.reset();
}
public HibernateConfiguration(String dialect, String driverClass,
String ipAddress, String port, String dataBaseName,
String username, String password) throws HibernateException {
String connection_url = "";
if (dialect.indexOf("MySQL") > -1) {
connection_url = "jdbc:mysql://" + ipAddress + "/" + dataBaseName;
} else if (dialect.indexOf("SQLServer") > -1) {
connection_url = "jdbc:sqlserver://" + ipAddress + ":" + port
+ ";DataBaseName=" + dataBaseName;
} else if (dialect.indexOf("Oracle") > -1) {
connection_url = "jdbc:oracle:thin:@" + ipAddress + ":" + port
+ ":" + dataBaseName;
} else {
throw new HibernateException("The dialect was not allowed.==fd=="
+ dialect);
}
super.setProperty("hibernate.dialect", dialect);
super.setProperty("hibernate.connection.url", connection_url);
super.setProperty("hibernate.connection.driver_class", driverClass);
super.setProperty("hibernate.connection.username", username);
super.setProperty("hibernate.connection.password", password);
// super.setProperty("hibernate.show_sql", "true");
}
public HibernateConfiguration(String dialect, String driverClass,
String ipAddress, String port, String dataBaseName,
String username, String password, String schema, String catalog)
throws HibernateException {
String connection_url = "";
if (dialect.indexOf("MySQL") > -1) {
connection_url = "jdbc:mysql://" + ipAddress + "/" + dataBaseName;
} else if (dialect.indexOf("SQLServer") > -1) {
connection_url = "jdbc:sqlserver://" + ipAddress + ":" + port
+ ";DataBaseName=" + dataBaseName;
} else if (dialect.indexOf("Oracle") > -1) {
connection_url = "jdbc:oracle:thin:@" + ipAddress + ":" + port
+ ":" + dataBaseName;
} else {
throw new HibernateException("The dialect was not allowed.==fd=="
+ dialect);
}
super.setProperty("hibernate.dialect", dialect);
super.setProperty("hibernate.connection.url", connection_url);
super.setProperty("hibernate.connection.driver_class", driverClass);
super.setProperty("hibernate.connection.username", username);
super.setProperty("hibernate.connection.password", password);
super.setProperty("hibernate.default_schema", schema);
super.setProperty("hibernate.default_catalog", catalog);
// super.setProperty("hibernate.show_sql", "true");
}
}
2. TempSessionFactory 的工厂类,这里把各个对象都变成了 static ,类也是static 类了,每次调用,都不用生成一个sessionFactory实例了,主要还是自己定义了一个reflashSessionFactory方法,对每次不同的数据库连接进行动态加载,其他的就是hibernate.cfg.xml 加载的问题,首次默认加载是用hibernate2.cfg.xml,其他时候就才用最新设置的参数,用了自定义类HibernateConfiguration 。 见下:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.tools.hibernate.utils.HibernateConfiguration;
public class TempSessionFactory {
.......//略
public static void reflashSessionFactory(
HibernateConfiguration tempConfiguration) {
try {
// configuration.configure(configFile);
if (tempConfiguration.getProperty("hibernate.dialect").equals(
configuration.getProperty("hibernate.dialect"))
&& tempConfiguration
.getProperty("hibernate.connection.url")
.equalsIgnoreCase(
configuration
.getProperty("hibernate.connection.url"))
&& tempConfiguration
.getProperty("hibernate.connection.username")
.equals(
configuration
.getProperty("hibernate.connection.username"))
&& tempConfiguration
.getProperty("hibernate.connection.password")
.equals(
configuration
.getProperty("hibernate.connection.password"))) {
System.out.println("%%%% TempSessionFactory is created aready!!-fd %%%%");
// sessionFactory = configuration.buildSessionFactory();
} else {
closeSession();
closeSessionFactory();
configuration.reset();
configuration = tempConfiguration;
sessionFactory = configuration.buildSessionFactory();
System.out.println("%%%% TempSessionFactory is reflashed here!!-fd %%%%");
}
} catch (Exception e) {
System.err.println("%%%% Error Reflashing TempSessionFactory %%%%");
e.printStackTrace();
}
}
......//略
}
3. 顺便发一下测试类,自己在不同服务器上面 建几个数据库,建几张测试表,就可以了:
public static void main(String[] args) {
try{
String timeFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
HibernateConfiguration configuration1 = new HibernateConfiguration("org.hibernate.dialect.SQLServerDialect","com.microsoft.sqlserver.jdbc.SQLServerDriver",
"255.255.255.255","1433","dbname1","sa","sa");
System.out.println("hibernate.connection.url==1="+TempSessionFactory.getConfiguration().getProperty("hibernate.connection.url"));
TempSessionFactory.reflashSessionFactory(configuration1);
System.out.println("hibernate.connection.url==2="+TempSessionFactory.getConfiguration().getProperty("hibernate.connection.url"));
Session session1=TempSessionFactory.getSession();
Transaction tx1 = session1.beginTransaction();
Query query1 = session1.createSQLQuery("select name as aaa from testtable ").setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP);
Map obj1 = (Map)query1.setMaxResults(1).uniqueResult();
System.out.println("fd1111===="+obj1.get("aaa"));
System.out.println("hibernate.connection.url==3="+TempSessionFactory.getConfiguration().getProperty("hibernate.connection.url"));
HibernateConfiguration configuration2 = new HibernateConfiguration("org.hibernate.dialect.Oracle10gDialect","oracle.jdbc.driver.OracleDriver",
"255.255.255.255","1521","orclDB1","username","passwd");
TempSessionFactory.reflashSessionFactory(configuration2);
System.out.println("hibernate.connection.url==4="+TempSessionFactory.getConfiguration().getProperty("hibernate.connection.url"));
session1=TempSessionFactory.getSession();
tx1 = session1.beginTransaction();
Query query2 = session1.createSQLQuery("select testname1 AAA from testtable1 ").setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP);
Map obj2 = (Map)query2.setMaxResults(1).uniqueResult();
System.out.println("fd2222===="+obj2.get("AAA"));
System.out.println("hibernate.connection.url==5="+TempSessionFactory.getConfiguration().getProperty("hibernate.connection.url"));
HibernateConfiguration configuration3 = new HibernateConfiguration("org.hibernate.dialect.Oracle10gDialect","oracle.jdbc.driver.OracleDriver",
"255.255.255.255","1521","orclDB2","username","passwd");
TempSessionFactory.reflashSessionFactory(configuration3);
System.out.println("hibernate.connection.url==6="+TempSessionFactory.getConfiguration().getProperty("hibernate.connection.url"));
session1=TempSessionFactory.getSession();
tx1 = session1.beginTransaction();
Query query3 = session1.createSQLQuery("select testname1 AAA from testtable2 ").setResultTransformer(
Transformers.ALIAS_TO_ENTITY_MAP);
Map obj3 = (Map)query3.setMaxResults(1).uniqueResult();
System.out.println("fd3333===="+obj3.get("AAA"));
}catch (Exception e) {
System.err.println(e);
}
}
有什么好的建议,请留言。。。。。。。。。。。。。。。。。。。
转载注明 出处。。。。。。。。。。。。。。。。。。。。。。。。。