【问题标题】:Oracle DB failover in Spring Boot handlingSpring Boot 处理中的 Oracle DB 故障转移
【发布时间】:2018-09-13 15:50:22
【问题描述】:

我想创建基于注释的拦截器,它将记录当前数据库的详细信息,只要主数据库无法为请求提供服务,辅助数据库将开始为应用程序提供支持。

我从提到的链接中找到了下面的代码,但我找不到特定的 orcl 注释,该注释必须用于下面的 @Aspect for aop 之类的注释,请帮助找到这个。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:orcl="http://www.springframework.org/schema/data/orcl"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/data/orcl
       http://www.springframework.org/schema/data/orcl/spring-data-orcl-1.0.xsd">

    <aop:config>
        <aop:advisor pointcut="execution(* *..PetStoreFacade.insertOrder(..))" 1 
            advice-ref="racFailoverInterceptor" order="1"/>
        <aop:advisor pointcut="execution(* *..PetStoreFacade.*(..))" 2 
            advice-ref="txAdvice"/>
    </aop:config>

    <orcl:rac-failover-interceptor id="racFailoverInterceptor"/> 3

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

</beans>

https://docs.spring.io/spring-data/jdbc/old-docs/2.0.0.BUILD-SNAPSHOT/reference/html/orcl.failover.html

【问题讨论】:

  • 我不确定我是否理解正确,但从我的角度来看,您可以使用 @ControllerAdvice 和 @ExceptionHandler 来捕获可能导致数据库连接丢失的异常并使用新的更新 DataSource bean 属性spring 上下文中的配置。这只是我的想法,我相信这个案例应该有更好的方法。

标签: java spring oracle spring-boot failover


【解决方案1】:

基本上,没有开箱即用的“Oracle”注释可用于使 rac 故障转移拦截器工作。但是很容易添加新的自定义注释来为您完成工作。

假设 sprig 数据预言机位于您的类路径中

Maven pom.xml

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-oracle</artifactId>
    <version>1.2.1.RELEASE</version>
</dependency>

只需创建一个您要使用的标记注释。

package org.example;
public @interface OracleFailover {
// just a marker annotation, no body
}

为其配置顾问

<aop:config>
    <aop:advisor 
       pointcut="@annotation(org.example.OracleFailover)" 
        advice-ref="racFailoverInterceptor" order="1"/>
</aop:config>

<orcl:rac-failover-interceptor id="racFailoverInterceptor"/>

然后将其用于您的业务方法

package org.example;

@Service
public class SomeBusinessService {
    @OracleFailover
    void doSomethingWithOracle(){
        // body goes here
    }
}

请记住,Rac Failover Interceptor 应该先于您的跨国拦截器,也就是说,如果在事务已经处于活动状态时完成故障转移拦截,则故障转移将无法按预期工作。

【讨论】:

  • orcl 是一个自定义的 xml 命名空间,如果您的类路径上有 spring-data-oracle 并且 XML 导入已完成,则会自动“导入”(请参阅​​您的 XML 的上半部分)问题)。此外,我还使用用于 spring-data-oracle maven 导入的 maven pom 更新了我的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多