gpt4 book ai didi

java - JDBC连接异常 : Unable to acquire JDBC Connection when using JPA

转载 作者:行者123 更新时间:2023-12-01 20:21:50 28 4
gpt4 key购买 nike

我有一个java spring应用程序,我使用websockets在服务器和角度客户端之间进行通信。然而,在一定数量的消息之后,服务器通常无法再处理它并产生 CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection结尾为:Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 29999ms. 。我在错误消息中可以找到的唯一内部文件是指向 save()方法。我使用 gradle 构建服务器

我使用本地运行 MySQL 服务器:

XAMPP 版本 7.1.33

MySQL 服务器版本:10.4.8-MariaDB - 源代码分发

Apache 网络服务器 2.4.41

Apache 网络服务器 PHP 版本 7.1.32

phpMyAdmin 版本 4.9.1

我更新实体的代码:

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

entity.setName(object.getString("name"));
entityRepository.save(bord);

entityManager.flush();
entityManager.getTransaction().commit();

build.gradle :

plugins {
id 'org.springframework.boot' version '2.2.0.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'org.asciidoctor.convert' version '1.5.8'
id 'java'
}

group = 'com.name.project'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}

repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}

ext {
set('snippetsDir', file("build/generated-snippets"))
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
runtimeOnly 'mysql:mysql-connector-java'
compile group: 'org.json', name: 'json', version: '20190722'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
}

test {
outputs.dir snippetsDir
useJUnitPlatform()
}

asciidoctor {
inputs.dir snippetsDir
dependsOn test
}

最佳答案

提交后您没有关闭EntityManager,这意味着您保持连接打开,因此最终会耗尽连接。

要解决此问题,请在提交后关闭 EntityManager

关于java - JDBC连接异常 : Unable to acquire JDBC Connection when using JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58934747/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com