日期:2014-05-16  浏览次数:20413 次

Hibernate动态连接多数据库改进篇

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.co