嗨,这就是我要做的事情 -
我有三个数据库 - 每个数据库完全相同 - 一个是主数据库,两个数据库是三个数据库。
我正在使用hibernate和spring连接到数据库。我有弹簧应用程序上下文文件,配置了3个会话工厂和3个相应的数据源。
我想知道是否有一种方法使用spring,如果应用程序无法连接到数据库1(抛出异常)它将连接到数据库2 - 反过来数据库3关闭它将连接到3并继续。
实施此操作的最佳方法是什么?我已经阅读了关于AbstractRoutingDataSource ..但不知道如何在这种情况下使用它...如果还有其他想法..会欣赏一些方向。谢谢!
这是我在应用程序上下文中的配置xml -
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="$g{jdbc.driverClassName}" />
<property name="jdbcUrl" value="$g{url1}" />
<property name="user" value="$l{uid1}" />
<property name="password" value="$l{pwd1}" />
<property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
<property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
<property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
<property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
</bean>
<bean id="sessionFactory1"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="packagesToScan">
<list>
<value>com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
<prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="$g{jdbc.driverClassName}" />
<property name="jdbcUrl" value="$g{url2}" />
<property name="user" value="$l{uid2}" />
<property name="password" value="$l{pwd2}" />
<property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
<property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
<property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
<property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
</bean>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="packagesToScan">
<list>
<value>com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
<prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="$g{jdbc.driverClassName}" />
<property name="jdbcUrl" value="$g{url3}" />
<property name="user" value="$l{uid3}" />
<property name="password" value="$l{pwd3}" />
<property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
<property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
<property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
<property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
</bean>
<bean id="sessionFactory3"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource3" />
<property name="packagesToScan">
<list>
<value>com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
<prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager1"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory1" />
</bean>
<bean id="transactionManager2"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory2" />
</bean>
<bean id="transactionManager3"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory3" />
</bean>
答案 0 :(得分:0)
有一种方法可以通过使用Spring Abstract Data Source Routing机制实现这一点 -
https://spring.io/blog/2007/01/23/dynamic-datasource-routing/