【发布时间】:2012-11-02 06:32:44
【问题描述】:
我的应用程序(假设)有以下服务器;我使用 MySQL。
1) 应用程序使用的数据库(位于日本的服务器)
2) 数据库备份(位于秘鲁的服务器)
3) 应急数据库(服务器位于美国)
我有几个关于 Spring 功能的问题:
A) 如何在所有数据源中同时持久化?
B) 我如何在 Spring 中创建一个连接池,以便如果我的第一个数据源没有响应,系统会自动使用第二个数据源?
这是我的真实applicationContext-datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>META-INF/database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
问候
【问题讨论】:
-
你会得到一些严重的延迟,不是吗?一种选择是定义两个数据源,并以编程方式检查第一个是否响应,如果没有,则使用第二个。
-
这不是问题,它是一个学术项目,所以服务器就在附近。休眠的东西和我的@Autowired 数据源不必知道应用程序现在正在与其他数据库一起工作。这就是连接池的精髓吗?
标签: java mysql spring datasource connection-pooling