Merge pull request #564 from TeamPiped/session-factory

Add shutdown thread to close session factory
This commit is contained in:
Kavin 2023-04-16 22:59:05 +01:00 committed by GitHub
commit 0af28bc2d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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,7 +12,7 @@ public class DatabaseSessionFactory {
private static final SessionFactory sessionFactory;
static {
try {
final Configuration configuration = new Configuration();
Constants.hibernateProperties.forEach(configuration::setProperty);
@ -21,6 +21,9 @@ public class DatabaseSessionFactory {
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();
}
}