- private synchronized void buildSessionFactory() {
- if (sessionFactory == null) {
- sessionFactory = new Configuration().configure("hibernate.cfg.xml")
- .addAnnotatedClass(AnchorEntity.class)
- .addAnnotatedClass(DataspaceEntity.class)
- .addAnnotatedClass(SchemaSetEntity.class)
- .addAnnotatedClass(YangResourceEntity.class)
- .buildSessionFactory();
+ @PostConstruct
+ private void postConstruct() {
+ final Thread shutdownHook = new Thread(this::closeAllSessionsInShutdown);
+ Runtime.getRuntime().addShutdownHook(shutdownHook);
+ }
+
+ private void closeAllSessionsInShutdown() {
+ for (final String sessionId : sessionMap.keySet()) {
+ try {
+ closeSession(sessionId, WITH_ROLLBACK);
+ log.info("Session with session ID {} rolled back and closed", sessionId);
+ } catch (final Exception e) {
+ log.warn("Session with session ID {} failed to close", sessionId);
+ }