Spring JPA 存储库查询更新卡住/阻塞

问题描述

在我的一生中,我无法看到导致更新查询卡住的代码有什么问题。我直接在数据库上测试了查询,没问题。

我可以确认应用程序可以连接到数据库。该应用程序可以正常启动。我可以很好地执行 findBy 查询。只是更新查询卡住了。

存储库类:

@Repository
public interface TestRepository extends CrudRepository<Test,String> {
    // Tried with @Modifying(flushAutomatically = true,clearautomatically = true).  Still no luck.
    @Modifying
    @Query("UPDATE Test SET stopAllPayment = 'Y' WHERE location = 'london'")
    int stopAllPayment();
}

服务类

@Service
@requiredArgsConstructor
public class StopPaymentService {
    private final TestRepository testRepository;

    @Transactional
    public void run() {

        // Stuck here.  Blocking.
        // In debug mode,the thread is waiting at Socketdispatcher.read0()
        testRepository.stopAllPayments();  
    }
}

主类

@SpringBootApplication
@requiredArgsConstructor
@EnableTransactionManagement
public class Main implements CommandLineRunner {
    private final StopPaymentService service;

    @Override
    public void run(String... args) throws Exception {
        service.run();
    }

    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}

应用程序.yml

spring:
  datasource:
    url: jdbc:oracle:thin:@dbdev:1521/LT110
    username: user
    password: password
    driver-class-name: oracle.jdbc.OracleDriver
  jpa:
    hibernate:
      ddl-auto: none

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath />
    </parent>

   
    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

        <!-- Oracle JDBC driver -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>21.1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.4.5</version>
        </dependency>
    </dependencies>
</project>

解决方法

当 find 方法起作用并且只有更新挂起时,最可能的解释是数据库锁定。

运行更新,然后检查您的数据库是否存在阻塞锁。如何执行此操作的详细信息取决于数据库。

,

根据这篇文章:https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/

实现 CommandLineRunner 的 Spring Boot 应用程序的正确代码如下:

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner {

    @Autowired
    private StopPaymentService stopPaymentService;
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }
 
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class,args);
    }
 
 
    @Override
    public void run(String... args) throws Exception {
        stopPaymentService.run();
        logger.info("Application Started !!");
    }
}