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

标签 java jpa connection

我有一个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/

相关文章:

tcp - 在没有服务器的情况下在两台计算机之间创建 TCP 连接

c# - 错误 1042 MySql C#?

java - 在 AWS linux 上安装 java Open JDK 10

javax.servlet.HttpServletRequest.getContentLength() 在 EE6 中的行为是否正确?

java - try catch 持续异常

java - 由于在 @Transactional 方法中使用带有 Hibernate、Spring 和 CrudRepository 接口(interface)的 JPA 关闭 session 导致延迟加载异常

android - java.net.ConnectException : failed to connect to/103. XX.XX.38(端口 8080):连接失败:ECONNREFUSED(连接被拒绝)

java - 使用 Java 将 RSS Feed XML 转换为 JSON 仅显示最后一个条目

用于网络驱动程序的 JavaScript

java - hibernate JTA : Read DB connection parameters per environment