Spring Boot 处理 Oracle 数据库故障转移

4
我想创建基于注解的拦截器,当主数据库无法提供服务时,它将记录当前数据库的详细信息,并启用辅助数据库来支持应用程序。
我从下面的链接中找到了以下代码,但我找不到特定的 orcl 注解,该注解类似于下面的 aop 中的 @Aspect,请帮助找到它。
<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来捕获可能导致数据库连接丢失的异常,并在Spring上下文中使用新配置更新DataSource bean属性。这只是我的想法,我相信应该有更好的方法来处理这种情况。 - Kamil W
1个回答

2

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

前提是sprig数据oracle在您的类路径中

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
}

为IT配置顾问

<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 应该在您的事务拦截器之前执行,如果在事务已经激活时执行故障转移拦截,那么故障转移将无法按预期工作。

如果您的类路径上有spring-data-oracle,并且已经完成XML导入(请参见问题中的XML上部),则“orcl”是自定义XML命名空间,会自动“导入”。此外,我已经使用spring-data-oracle maven导入的maven pom更新了我的示例。 - Babl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接