【问题标题】:Spring JPA repository query update stuck / blockingSpring JPA 存储库查询更新卡住/阻塞
【发布时间】:2021-05-15 16:39:54
【问题描述】:

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

我可以确认应用程序可以连接到数据库。该应用程序可以正常启动。我可以很好地执行 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);
    }
}

application.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>

【问题讨论】:

  • 您的应用程序似乎无法连接到数据库。确保您的数据库连接属性正确。请提供 application.properties 和 pom.xml。目前尚不清楚,您使用的是哪个数据库。
  • 检查数据库上的锁。
  • 验证这个link,我想这对你有帮助!
  • 如果它不等待数据库,它会等待其他东西。使用调试器查找它正在等待的确切代码位置和堆栈跟踪。查看线程以了解它在等待什么。
  • @Jens Schauder,事实证明你是对的。这是一个数据库锁定问题。你想添加这个作为你的答案让我批准吗?

标签: java spring-boot spring-data-jpa spring-repositories


【解决方案1】:

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

运行更新,然后检查数据库是否存在阻塞锁。具体操作方法是特定于数据库的。

【讨论】:

    【解决方案2】:

    根据这篇文章: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 !!");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-12
      • 2021-04-16
      • 2015-03-28
      • 2015-10-13
      • 2018-11-09
      • 2017-11-15
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      相关资源
      最近更新 更多