Add shutdown thread to close session factory

This commit is contained in:
Kavin 2023-04-16 22:56:33 +01:00
parent 316248e8eb
commit 977ffa1286
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 17 additions and 7 deletions

View file

@ -74,6 +74,9 @@ public class Main {
System.exit(1);
}
// Close the HikariCP connection pool
Runtime.getRuntime().addShutdownHook(new Thread(DatabaseSessionFactory::close));
if (Constants.DISABLE_TIMERS)
return;

View file

@ -12,15 +12,18 @@ public class DatabaseSessionFactory {
private static final SessionFactory sessionFactory;
static {
try {
final Configuration configuration = new Configuration();
final Configuration configuration = new Configuration();
Constants.hibernateProperties.forEach(configuration::setProperty);
configuration.configure();
Constants.hibernateProperties.forEach(configuration::setProperty);
configuration.configure();
sessionFactory = configuration.addAnnotatedClass(User.class).addAnnotatedClass(Channel.class)
.addAnnotatedClass(Video.class).addAnnotatedClass(PubSub.class).addAnnotatedClass(Playlist.class)
.addAnnotatedClass(PlaylistVideo.class).addAnnotatedClass(UnauthenticatedSubscription.class).buildSessionFactory();
sessionFactory = configuration.addAnnotatedClass(User.class).addAnnotatedClass(Channel.class)
.addAnnotatedClass(Video.class).addAnnotatedClass(PubSub.class).addAnnotatedClass(Playlist.class)
.addAnnotatedClass(PlaylistVideo.class).addAnnotatedClass(UnauthenticatedSubscription.class).buildSessionFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Session createSession() {
@ -30,4 +33,8 @@ public class DatabaseSessionFactory {
public static StatelessSession createStatelessSession() {
return sessionFactory.openStatelessSession();
}
public static void close() {
sessionFactory.close();
}
}