Java/Hibernate Error: Connection leak detected. The internal connection pool has reached its maximum size and no connection is currently available

I am new to Java/Hibernate and I have no idea what this error means.

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!

I looked up answers on other forums from Stack Overflow but nothing made sense to me there.

My classes are here as follows:

Create Student Demo

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()

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

        try {           
            // create a student object
            System.out.println("Creating new student object...");
            Student tempStudent = new Student("Rishav", "Sharma", "[email protected]");

            // start a transaction

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

            // commit transaction

        finally {


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;

public class Student {

    private int id; 

    private String firstName; 

    private String lastName; 

    private String email; 

    public Student(){ 


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

    public int getId() {
        return id;

    public void setId(int 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) { = email;

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


And my config.xml file


<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



        <!-- 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>



I don’t know what’s causing my open connection as I have closed my factory in the class and the session is temporary.


I think you should also flush & close the session object before closing the factory object.

Leave a Reply

Your email address will not be published. Required fields are marked *