gpt4 book ai didi

Java/Hibernate 错误 : Connection leak detected. 内部连接池已达到最大大小,当前没有可用连接

转载 作者:行者123 更新时间:2023-11-29 06:27:08 24 4
gpt4 key购买 nike

我是 Java/Hibernate 的新手,我不知道这个错误是什么意思。

ERROR: Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:mysql://localhost:3306/hb_student_records?useSSL=false&serverTimezone=UTC
Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!

我在 Stack Overflow 的其他论坛上查找了答案,但对我来说没有任何意义。

我的类(class)如下:

创建学生演示

package com.rsharma.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.rsharma.hibernate.demo.entity.Student;

public class CreateStudentDemo {

public static void main(String[] args) {

// create session factory
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();

// create session
Session session = factory.getCurrentSession();

try {
// create a student object
System.out.println("Creating new student object...");
Student tempStudent = new Student("Rishav", "Sharma", "paul@luv2code.com");

// start a transaction
session.beginTransaction();

// save the student object
System.out.println("Saving the student...");
session.save(tempStudent);

// commit transaction
session.getTransaction().commit();

System.out.println("Done!");
}
finally {
factory.close();
}
}

}

学生.java

package com.rsharma.hibernate.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email")
private String email;



public Student(){

}


public Student(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getFirstName() {
return firstName;
}


public void setFirstName(String firstName) {
this.firstName = firstName;
}


public String getLastName() {
return lastName;
}


public void setLastName(String lastName) {
this.lastName = lastName;
}


public String getEmail() {
return email;
}


public void setEmail(String email) {
this.email = email;
}


@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}

}

还有我的config.xml文件

hibernate .cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hb_student_records?useSSL=false&amp;serverTimezone=UTC</property>
<property name="connection.username">hbstudent</property>
<property name="connection.password">hbstudent</property>

<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>

<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>

<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>

</session-factory>

</hibernate-configuration>

我不知道是什么原因导致我打开连接,因为我已经关闭了类里面的工厂并且 session 是临时的。

最佳答案

我认为您还应该在关闭工厂对象之前刷新并关闭 session 对象。

关于Java/Hibernate 错误 : Connection leak detected. 内部连接池已达到最大大小,当前没有可用连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51940495/

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