简体   繁体   English

Spring池与MySQL的连接

[英]Spring pool connections with MySQL

I have (hypothetically) the following servers for my application; (假设)我的应用程序具有以下服务器; I work with MySQL. 我使用MySQL。

1) Database used by the application (server located in Japan) 1)应用程序使用的数据库(位于日本的服务器)
2) Database Backup (server located in Peru) 2)数据库备份(服务器位于秘鲁)
3) Emergency Database (server located in the US) 3)紧急数据库(位于美国的服务器)

I have a couple of questions about the capabilities of Spring: 关于Spring的功能,我有几个问题:

A) How I can persist simultaneously in all datasources? A)如何在所有数据源中同时存在?

B) How I can create a connection pool in Spring so that if my first datasource does not respond, the system automatically works with the second datasource? B)如何在Spring中创建连接池,以便如果我的第一个数据源不响应,系统将自动使用第二个数据源?

This is my actual applicationContext-datasource.xml 这是我实际的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>

Regards 问候

If I had the same problem I would try solve it outside of the application boundaries, so as not to have the application coupled with database topology . 如果我遇到相同的问题,我会尝试在应用程序范围之外解决它,以免使应用程序与数据库拓扑耦合。

I should request to persist an object to a logical entity perceived by Spring/Hibernate as a database and which is not part of the application, but it's aware of the database topology and availability, then delegate to it the distribution of the data. 我应该请求将一个对象持久化为Spring / Hibernate认为是数据库的逻辑实体,它不是应用程序的一部分,但是它知道数据库的拓扑和可用性,然后将数据的分配委托给它。

Normally you can solve this through: 通常,您可以通过以下方法解决此问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 oatomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。 春季启动+ MySql - o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool. Spring Boot + MySql 带有测试容器的 Spring 不会将连接返回到池 - Spring with Testcontainers not returning connections to pool Hikari 池连接何时工作? - 弹簧/弹簧靴 - When does a Hikari pool connections work? - spring/spring boot Spring Boot MySQL睡眠连接 - Spring boot mysql sleep connections 尝试设置数据库连接的初始池大小(spring-boot) - Try set initial pool size of database connections (spring-boot) Spring Integration:同一池中连接的各个超时设置 - Spring Integration: individual timeouts settings for connections in the same pool 无法创建池异步Jersey + Spring + Tomcat的初始连接 - Unable to create initial connections of pool Async Jersey+Spring+Tomcat 何时使用 Spring JPA (Hibernate) Entity Manager 将连接返回到连接池? - When are connections returned to the connection pool with Spring JPA (Hibernate) Entity Manager? MySQL连接池超过Java中的最大连接数 - MySQL connection pool is exceeding max number of connections in Java SpringBoot MySQL JDBC 无法创建池的初始连接 - SpringBoot MySQL JDBC Unable to create initial connections of pool
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM