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); System.exit(1);
} }
// Close the HikariCP connection pool
Runtime.getRuntime().addShutdownHook(new Thread(DatabaseSessionFactory::close));
if (Constants.DISABLE_TIMERS) if (Constants.DISABLE_TIMERS)
return; return;

View file

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