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

Hibernate根据方言dialect动态连接多数据库

?

Hibernate根据方言dialect动态连接多数据库

由于最近需要在web项目中动态链接不同地址的数据库,且要链接的子数据库的信息在总数据库(即web项目的主数据库)的表中,所以动手写了一个类。

web项目试用了hibernate,动态生成的子数据库链接打算也用hibernate,虽然动态生成的sessionfactory类,以及Configuration配置没有子数据库的对象关系映射,但是使用 native SQL 也方便。

?

请看后来写的改进篇,有什么建议请留言------------

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

-------------------------------------------------------


代码如下:

public class TempSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    //private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
	private final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private Configuration configuration = new Configuration(); 
    private org.hibernate.SessionFactory sessionFactory;
    //private static String configFile = CONFIG_FILE_LOCATION;

/*	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }*/

	public void setConfiguration(String dialect, String driverClass,
			String ipAddress, String port, String dataBaseName,
			String username, String password) {
		String connection_url = "";

		Configuration configuration = new Configuration();
		if (dialect.indexOf("MySQL") > -1) {
			System.out.println("%%%% DataBase type is MySql %%%%");
			connection_url = "jdbc:mysql://" + ipAddress + "/" + dataBaseName;
		} else if (dialect.indexOf("SQLServer") > -1) {
			System.out.println("%%%% DataBase type is SQLServer %%%%");
			connection_url = "jdbc:sqlserver://" + ipAddress + ":" + port
					+ ";DataBaseName=" + dataBaseName;
		} else if (dialect.indexOf("Oracle") > -1) {
			System.out.println("%%%% DataBase type is Oracle %%%%");
			connection_url = "jdbc:oracle:thin:@" + ipAddress + ":" + port
					+ ":" + dataBaseName;
			// configuration.setProperty("hibernate.connection.oracle.jdbc.V8Compatible","true");
		}

		configuration.setProperty("hibernate.dialect", dialect);
		configuration.setProperty("hibernate.connection.url", connection_url);
		configuration.setProperty("hibernate.connection.driver_class",
				driverClass);
		configuration.setProperty("hibernate.connection.username", username);
		configuration.setProperty("hibernate.connection.password", password);
		// configuration.setProperty("hibernate.default_schema", "dbo");
		// configuration.setProperty("hibernate.default_catalog", dataBaseName);
		// configuration.setProperty("hibernate.show_sql", "true");
		this.configuration = configuration;
	}
	/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     *  
     */
    public Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);
		}

        return session;
    }

	/**
     *  Rebuild hibernate session factory
     *
     */
	public void rebuildSessionFactory() {
		try {
			//configuration.configure(configFile);
			sessionFactory = this.configuration.buildSessionFactory();