Jboss 数据源中的数据库故障转移

发布于 2025-01-08 03:10:19 字数 176 浏览 2 评论 0原文

在 JBoss 数据源中,如何为我想要的数据库故障转移提供多个连接字符串。

将有两个具有相同表的 Mysql 数据库,例如 DB1 和 DB2。我想将数据插入到DB1中,如果DB1宕机了,那么我需要将其插入到DB2中。在插入 DB2 期间,如果 DB1 出现,我需要将其余数据插入 DB1。我如何在 JBoss 中配置它?

In JBoss data source how can I give multiple connection strings for database fail over in which I want .

There will be two Mysql db with same tables say DB1 and DB2. I want to insert data to DB1, if DB1 is down, then I need to insert it into DB2. During inserting into DB2 if the DB1 comes up I need to insert rest of the data into DB1. How can I configure this in my JBoss?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2025-01-15 03:10:19
<?xml version="2.0" encoding="UTF-8"?>
<!-- $Id$ -->

 <!--  Datasource config for MySQL using 3.0.9 available from:
    http://www.mysql.com/downloads/api-jdbc-stable.html
      -->
  <datasources>
    <local-tx-datasource>

      <jndi-name>MySqlDSTest</jndi-name>
      <use-java-context>true</use-java-context>
      <connection-url>jdbc:mysql:loadbalance://ip1,ip2:3306/dbname?</connection-url>
      <url-delimiter>,</url-delimiter>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <connection-property name="readOnly">false</connection-property>
      <autoReconnect>true</autoReconnect>
      <failOverReadOnly>false</failOverReadOnly>
      <user-name>userName</user-name>
      <password>password</password>
      <check-valid-connection-sql>selcect count(*) from TEST_TAB</check-valid-connection-sql>
      <maxReconnects>0</maxReconnects>
      <initialTimeout>15</initialTimeout>
      <idle-timeout-minutes>0</idle-timeout-minutes>
      <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
      <!-- Advanced options for the MySQL Driver can be set with
              <connection-property name="property">value</connection-property>
        -->
      <min-pool-size>5</min-pool-size>
      <!-- Don't set this any higher than max_connections on your
           MySQL server, usually this should be a 10 or a few 10's
           of connections, not hundreds or thousands -->
      <max-pool-size>20</max-pool-size>
      <!-- Don't allow connections to hang out idle too long,
           never longer than what wait_timeout is set to on the
           server...A few minutes is usually okay here,
           it depends on your application
           and how much spikey load it will see -->

      <!-- If you're using Connector/J 3.1.8 or newer, you can use
           our implementation of these to increase the robustness
           "mysql-ds.xml" 64L, 3683C      of the connection pool. -->
      <exception-sorter-class-name>
         com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter
      </exception-sorter-class-name>
      <valid-connection-checker-class-name>
         com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker
      </valid-connection-checker-class-name>
      <!-- sql to call when connection is created -->
      <new-connection-sql>select 1</new-connection-sql>
      <!-- sql to call on an existing pooled connection when it is obtained from pool -    MySQLValidConnectionChecker is preferred for newer drivers -->
      <check-valid-connection-sql>
        select 1
      </check-valid-connection-sql>
      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
      <metadata>
        <type-mapping>mySQL</type-mapping>
      </metadata>
    </local-tx-datasource>
  </datasources>

这适用于 jboss 映射

<?xml version="2.0" encoding="UTF-8"?>
<!-- $Id$ -->

 <!--  Datasource config for MySQL using 3.0.9 available from:
    http://www.mysql.com/downloads/api-jdbc-stable.html
      -->
  <datasources>
    <local-tx-datasource>

      <jndi-name>MySqlDSTest</jndi-name>
      <use-java-context>true</use-java-context>
      <connection-url>jdbc:mysql:loadbalance://ip1,ip2:3306/dbname?</connection-url>
      <url-delimiter>,</url-delimiter>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <connection-property name="readOnly">false</connection-property>
      <autoReconnect>true</autoReconnect>
      <failOverReadOnly>false</failOverReadOnly>
      <user-name>userName</user-name>
      <password>password</password>
      <check-valid-connection-sql>selcect count(*) from TEST_TAB</check-valid-connection-sql>
      <maxReconnects>0</maxReconnects>
      <initialTimeout>15</initialTimeout>
      <idle-timeout-minutes>0</idle-timeout-minutes>
      <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
      <!-- Advanced options for the MySQL Driver can be set with
              <connection-property name="property">value</connection-property>
        -->
      <min-pool-size>5</min-pool-size>
      <!-- Don't set this any higher than max_connections on your
           MySQL server, usually this should be a 10 or a few 10's
           of connections, not hundreds or thousands -->
      <max-pool-size>20</max-pool-size>
      <!-- Don't allow connections to hang out idle too long,
           never longer than what wait_timeout is set to on the
           server...A few minutes is usually okay here,
           it depends on your application
           and how much spikey load it will see -->

      <!-- If you're using Connector/J 3.1.8 or newer, you can use
           our implementation of these to increase the robustness
           "mysql-ds.xml" 64L, 3683C      of the connection pool. -->
      <exception-sorter-class-name>
         com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter
      </exception-sorter-class-name>
      <valid-connection-checker-class-name>
         com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker
      </valid-connection-checker-class-name>
      <!-- sql to call when connection is created -->
      <new-connection-sql>select 1</new-connection-sql>
      <!-- sql to call on an existing pooled connection when it is obtained from pool -    MySQLValidConnectionChecker is preferred for newer drivers -->
      <check-valid-connection-sql>
        select 1
      </check-valid-connection-sql>
      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
      <metadata>
        <type-mapping>mySQL</type-mapping>
      </metadata>
    </local-tx-datasource>
  </datasources>

This will work for jboss mapping

超可爱的懒熊 2025-01-15 03:10:19

请遵循本文档。 Jboss有配置设置。
https://community.jboss.org/wiki/JBossJCADatabaseFailover

此外,如果您不使用JNDI 和普通 JDBC 调用我有另一个解决方案 - 假设您正在进行 jdbc 调用,那么您将需要获得数据库连接,如果数据库关闭,那么您将获得数据库连接异常,在 try catch 块中,如果遇到连接异常,请在 catch 块本身中为第二个数据库创建连接:-)

更新:2/16/2022。

更新永远不会太晚。您还可以使用像 Resilience4J 这样的断路器。如果连接结果返回异常,您可以使用 .onError 事件尝试连接第二个数据库。 9年后,我会使用CB来解决这个问题。 Jboss 提供了内置机制来执行此操作,但其他一些服务器则不需要重新启动或额外的手动步骤。

Follow this documentation. Jboss has configuration setting.
https://community.jboss.org/wiki/JBossJCADatabaseFailover

Additionally, if you are NOT using JNDI and plain JDBC call I hav another solution - Say you are making a jdbc call then you will need to get a DB connection, if the DB is down then you will get Database connection exception, in the try catch block, if you encounter connection exception, create a connection in catch block itself for the second database :-)

Updated: 2/16/2022.

Never too late to update. You can also use Circuit breaker like Resilience4J. If the connection result comes back with exception, you can use .onError event to try connecting to second database. 9 Years later, I would use CB to solve this issue. Jboss provides inbuilt mechanism to do that but some other servers don't without restarting or additional manual steps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文