terminate hibernate main class with connenction pool

Description

I wrote a program with *Hibernate* that runs a Main class. When I run a Main, everything is fine, but in the end it doesn't terminate without a `sessionFactory.close()`.

pom.xml
```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>Hibernate</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
</dependencies>

</project>
````

```
public class HibernateUtil {
private static boolean mySql = false;
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistery;

public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Properties properties = new Properties();
properties.put(Environment.DRIVER, "org.h2.Driver");
properties.put(Environment.URL, "jdbc:h2:file:D:/sourceCode/MyExample/Hibernate/src/main/resources/h2data/dbdata;AUTO_SERVER=true");
properties.put(Environment.USER, "sa");
properties.put(Environment.PASS, "");
properties.put(Environment.DIALECT, org.hibernate.dialect.H2Dialect.class.getName());
properties.put(Environment.SHOW_SQL, "true");
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
properties.put(Environment.HBM2DDL_AUTO, "create");
Configuration configuration = new Configuration();
configuration.setProperties(properties);
configuration.addAnnotatedClass(Student.class);
serviceRegistery = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistery);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}

public static void shutdown() {
if (serviceRegistery != null) {
// StandardServiceRegistryBuilder.destroy(serviceRegistery);
sessionFactory.close();
}
}
}
```

```
public class App {
public static void main(String[] args) {
StudentDao studentDao = new StudentDao();
Student student = new Student("alex", "yokohama", "a.yokohama@hotmail.ir");
studentDao.saveStudent(student);
student = new Student("john", "fenochi", "j.taagholi@hotmail.ir");
studentDao.saveStudent(student);
}
}
```
```
public class StudentDao {
public void saveStudent(Student student) {
Transaction transaction = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
```

output in console is:
```
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Users\m.taagholi\AppData\Local\JetBrains\IntelliJ IDEA 2020.2.3\lib\idea_rt.jar=62644:C:\Users\m.taagholi\AppData\Local\JetBrains\IntelliJ IDEA 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;D:\sourceCode\MyExample\Hibernate\target\classes;C:\Users\m.taagholi\.m2\repository\com\h2database\h2\1.4.200\h2-1.4.200.jar;C:\Users\m.taagholi\.m2\repository\org\hibernate\hibernate-core\5.3.7.Final\hibernate-core-5.3.7.Final.jar;C:\Users\m.taagholi\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\m.taagholi\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\m.taagholi\.m2\repository\org\javassist\javassist\3.23.1-GA\javassist-3.23.1-GA.jar;C:\Users\m.taagholi\.m2\repository\net\bytebuddy\byte-buddy\1.8.17\byte-buddy-1.8.17.jar;C:\Users\m.taagholi\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\m.taagholi\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\m.taagholi\.m2\repository\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;C:\Users\m.taagholi\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\m.taagholi\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\m.taagholi\.m2\repository\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;C:\Users\m.taagholi\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.0.4.Final\hibernate-commons-annotations-5.0.4.Final.jar" App
Oct 15, 2020 9:49:42 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.7.Final}
Oct 15, 2020 9:49:42 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 15, 2020 9:49:43 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
Oct 15, 2020 9:49:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Oct 15, 2020 9:49:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:file:D:/sourceCode/MyExample/Hibernate/src/main/resources/h2data/dbdata;AUTO_SERVER=true]
Oct 15, 2020 9:49:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
Oct 15, 2020 9:49:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 15, 2020 9:49:43 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Oct 15, 2020 9:49:48 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: drop table student if exists
Hibernate: create table student (id integer generated by default as identity, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (id))
Oct 15, 2020 9:49:48 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@70e659aa] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Oct 15, 2020 9:49:48 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@6ce1f601] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Oct 15, 2020 9:49:48 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@6c451c9c'
Hibernate: insert into student (id, email, first_name, last_name) values (null, ?, ?, ?)
Hibernate: insert into student (id, email, first_name, last_name) values (null, ?, ?, ?)
```
As seen above, Main Class doesn't terminate.

########

After add this dependency to pom.xml
```
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>5.3.7.Final</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.1</version>
</dependency>
```

and add configuration to HibernateUtil
```
properties.put(Environment.CONNECTION_PROVIDER, "com.zaxxer.hikari.hibernate.HikariConnectionProvider");
Properties prop = loadProperties("primary-db-hikari-config.properties");
properties.putAll(prop);
```

my console is shown:
```
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Users\m.taagholi\AppData\Local\JetBrains\IntelliJ IDEA 2020.2.3\lib\idea_rt.jar=62845:C:\Users\m.taagholi\AppData\Local\JetBrains\IntelliJ IDEA 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;D:\sourceCode\MyExample\Hibernate\target\classes;C:\Users\m.taagholi\.m2\repository\com\h2database\h2\1.4.200\h2-1.4.200.jar;C:\Users\m.taagholi\.m2\repository\org\hibernate\hibernate-core\5.3.7.Final\hibernate-core-5.3.7.Final.jar;C:\Users\m.taagholi\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\m.taagholi\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\m.taagholi\.m2\repository\org\javassist\javassist\3.23.1-GA\javassist-3.23.1-GA.jar;C:\Users\m.taagholi\.m2\repository\net\bytebuddy\byte-buddy\1.8.17\byte-buddy-1.8.17.jar;C:\Users\m.taagholi\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\m.taagholi\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\m.taagholi\.m2\repository\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;C:\Users\m.taagholi\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\m.taagholi\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\m.taagholi\.m2\repository\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;C:\Users\m.taagholi\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.0.4.Final\hibernate-commons-annotations-5.0.4.Final.jar;C:\Users\m.taagholi\.m2\repository\org\hibernate\hibernate-hikaricp\5.3.7.Final\hibernate-hikaricp-5.3.7.Final.jar;C:\Users\m.taagholi\.m2\repository\com\zaxxer\HikariCP\2.6.1\HikariCP-2.6.1.jar;C:\Users\m.taagholi\.m2\repository\org\slf4j\slf4j-api\1.7.22\slf4j-api-1.7.22.jar" App
Oct 15, 2020 10:01:36 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.7.Final}
Oct 15, 2020 10:01:36 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 15, 2020 10:01:36 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
Oct 15, 2020 10:01:37 AM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
INFO: HHH000130: Instantiating explicit connection provider: com.zaxxer.hikari.hibernate.HikariConnectionProvider
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Oct 15, 2020 10:01:46 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: drop table student if exists
Hibernate: create table student (id integer generated by default as identity, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (id))
Oct 15, 2020 10:01:46 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@51abf713'
Hibernate: insert into student (id, email, first_name, last_name) values (null, ?, ?, ?)
Hibernate: insert into student (id, email, first_name, last_name) values (null, ?, ?, ?)

Process finished with exit code 0
```

But when I add the *HikraiCP* connection pool to program, it terminates without call `sessionFactory.close`.
Does anyone know why?

Environment

None

Assignee

Unassigned

Reporter

mehrdad

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Priority

Major
Configure