【问题标题】:Spring pool connections with MySQL与 MySQL 的 Spring 池连接
【发布时间】: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


【解决方案1】:

如果我有同样的问题,我会尝试在应用程序边界之外解决它,以免应用程序与数据库拓扑耦合。

我应该请求将对象持久化到 Spring/Hibernate 将其视为数据库的逻辑实体,该实体不是应用程序的一部分,但它知道数据库拓扑和可用性,然后将数据分配委托给它.

通常你可以通过以下方式解决这个问题:

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多