您的位置:首页 > 数据库

hibernate连接多个数据库,如何动态切换(我一般用配置文件动态切换)

2016-01-14 12:11 549 查看

SSH 联接多个数据库并且动态切换

www.MyException.Cn  网友分享于:2013-12-29  浏览:287次

SSH 连接多个数据库并且动态切换

   在项目中,有时遇到连接多个数据库的情况,并且根据用户的操作不同,连接不同的数据库,这时,就要动态切换数据库。环境:SSH(利用到了Hibernate 注解)。

Spring2.x的版本中采用Proxy模式,就是我们在方案中实现一个虚拟的数据源,并且用它来封装数据源选择逻辑,这样*就可以有效地将数据源选择逻辑从Client中分离出来。Client提供选择所需的上下文(因为这是Client所知道的),**由虚拟的DataSource根据Client提供的上下文来实现数据源的选择。

具体的实现就是,虚拟的DataSource仅需继承AbstractRoutingDataSource实现

determineCurrentLookupKey()在其中封装数据源的选择逻辑。  步骤如下:

一:动态配置多数据源(用类表示)

public class DataSourceConst {

public static final String Admin="admin";//admin和配置文件中的<entry value-ref="adminDataSource" key="admin"></entry> 对应

public static final String User="user";//user和配置文件中的 <entry value-ref="userDataSource" key="user"></entry> 对应

}

二:建立一个获得和设置上下文环境的类,主要负责改变上下文数据源的名称

public class DataSourceContextHolder {

private static final ThreadLocal contextHolder = new ThreadLocal();// 线程本地环境

// 设置数据源类型

public static void setDataSourceType(String dataSourceType) {

contextHolder.set(dataSourceType);

}

// 获取数据源类型

public static String getDataSourceType() {

return (String) contextHolder.get();

}

// 清除数据源类型

public static void clearDataSourceType() {

contextHolder.remove();

}

}

三:建立动态数据源类,返回一个Object,一般是返回字符串

public class DynamicDataSource extends AbstractRoutingDataSource {

@Override

protected Object determineCurrentLookupKey() {

return DataSourceContextHolder.getDataSourceType();

}

}

四:编写spring的配置文件配置多个数据源 applicationContext.xml 部分代码

<!-- 读取并设置数据库相关属性 -->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>   <!--连接数据库的信息(数据库信息已固定)-->

<value>classpath:db.properties</value>

<value>classpath:newdb.properties</value>

</list>

</property>

</bean>

<!-- 配置多个数据源 -->

<!-- 数据源相同部分 -->

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="parentDataSource">

<property name="driverClassName" value="${drivers}" />

</bean>

<!-- 198上的数据库 -->

<bean parent="parentDataSource" id="adminDataSource">

<property name="url" value="${DefaultDBUrl.url}" />

<property name="username" value="${DefaultDBUrl.user}" />

<property name="password" value="${DefaultDBUrl.password}" />

</bean>

<!-- 199 上的数据库 -->

<bean parent="parentDataSource" id="userDataSource">

<property name="url" value="${newDBUrl.url}" />

<property name="username" value="${newDBUser.user}" />

<property name="password" value="${newDBPassword.password}" />

</bean>

<!-- 编写spring配置文件 配置多数据源映射关系 -->

<bean class="DyDataSource.DynamicDataSource" id="dataSource">

<property name="targetDataSources">

<map key-type="java.lang.String">

  <entry value-ref="adminDataSource" key="admin"></entry>

  <entry value-ref="userDataSource" key="user"></entry>

</map>

</property>

<property name="defaultTargetDataSource" ref="adminDataSource"></property>

</bean>

<!-- sessionFactory 配置 -->

<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">

<property name="dataSource">

<ref local="dataSource"></ref>

</property>

<!-- 为sessionFactory设置Hibernate属性 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

<!-- SQLServer:org.hibernate.dialect.SQLServerDialect -->

<!-- MySql -->

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

<!-- 如果做了下面的配置,将影响事务配置,servcie中事务不会回滚, -->

<!--<prop key="hibernate.connection.release_mode">

after_transaction

</prop>-->

</props>

</property>

<property name="packagesToScan">

<list>

<value>com.luguang.model</value>

</list>

</property>

</bean>

五:在应用程序中,动态切换数据库

     ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

    DataSourceContextHolder.setDataSourceType(DataSourceConst.Admin);

    LgispUser user0=new LgispUser();

    user0.setUserAlias("AdminDB 000user");

    user0.setOrgId(1);

    this.lgispUserService.getEntityDao().save(user0);

    DataSourceContextHolder.setDataSourceType(DataSourceConst.User);

    this.lgispUserService.getEntityDao().save(user0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: