java - Spring Boot 处理中的 Oracle DB 故障转移

标签 java spring oracle spring-boot failover

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

我从提到的链接中找到了下面的代码,但我找不到特定的 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

最佳答案

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

前提是 sprig data 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
}

为其配置顾问

<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 故障转移拦截器应该在您的跨国拦截器之前运行,如果在事务已经处于 Activity 状态时进行故障转移拦截,故障转移将不会按预期工作。

关于java - Spring Boot 处理中的 Oracle DB 故障转移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265335/

相关文章:

oracle - Oracle 中 varchar2 PL/SQL 子程序参数的大小限制是多少?

java - 为什么 CORSFilter 与 ExceptionMapper<Throwable> 冲突?

java - 当自定义事务管理器连接失败时,Spring Boot应用程序不会快速失败

java - 为什么我的文本字段不是我指定的大小?

java - 我如何使用 spring 框架监视(收听)我使用 @scheduled 安排的工作?

java - 无法使用 mvn spring-boot :build-image 构建 war 文件的镜像

java - jgrapht 中的 jgrapht 类将允许我动态构建图表

java - Spring Data JPA - 编写复杂的查询方法

java - 使用 Java 创建到 oracle db 的 ssl 连接

java - 如何在oracle XE中创建数据库?