【发布时间】: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