From 11a23b44646e837a3d25d7ac3dec6e01da3f8e2b Mon Sep 17 00:00:00 2001 From: Zaxar163 Date: Mon, 3 Jun 2019 14:10:56 +0300 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=20=D0=BA=D0=BE=D0=B4=20=D0=BD=D0=B0=20try-wi?= =?UTF-8?q?th-resources=20=D0=B2=20HibernateDAOImpl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HibernateConfiguratorComponent.java | 5 +- .../dao/impl/HibernateUserDAOImpl.java | 48 ++++++++++--------- .../hibernate/HibernateManager.java | 20 -------- 3 files changed, 29 insertions(+), 44 deletions(-) delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java index 6b5017e6..87ff23e9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -2,13 +2,13 @@ import java.nio.file.Paths; +import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.dao.LaunchServerDaoFactory; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; -import pro.gravit.launchserver.hibernate.HibernateManager; import pro.gravit.utils.helper.CommonHelper; public class HibernateConfiguratorComponent extends Component { @@ -19,6 +19,7 @@ public class HibernateConfiguratorComponent extends Component { public String pool_size; public String hibernateConfig; public boolean parallelHibernateInit; + public SessionFactory sessionFactory = null; @Override public void preInit(LaunchServer launchServer) { LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new); @@ -32,7 +33,7 @@ public void preInit(LaunchServer launchServer) { .setProperty("hibernate.connection.pool_size", pool_size); if(hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); - HibernateManager.setSessionFactory(launchServer, cfg.buildSessionFactory()); + sessionFactory = cfg.buildSessionFactory(); }; if(parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java index 818cb4a2..a7420506 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java @@ -12,23 +12,25 @@ import org.hibernate.Transaction; import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.components.HibernateConfiguratorComponent; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.UserDAO; -import pro.gravit.launchserver.hibernate.HibernateManager; public class HibernateUserDAOImpl implements UserDAO { - private final LaunchServer srv; + private final HibernateConfiguratorComponent hibernate; public HibernateUserDAOImpl(LaunchServer srv) { - this.srv = srv; + hibernate = (HibernateConfiguratorComponent) srv.config.components.values().stream().filter(e -> HibernateConfiguratorComponent.class.isInstance(e)).findFirst().get(); } public User findById(int id) { - return HibernateManager.getSessionFactory(srv).openSession().get(User.class, id); + try (Session s = hibernate.sessionFactory.openSession()) { + return s.get(User.class, id); + } } public User findByUsername(String username) { - EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager(); + EntityManager em = hibernate.sessionFactory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -40,7 +42,7 @@ public User findByUsername(String username) { } public User findByUUID(UUID uuid) { - EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager(); + EntityManager em = hibernate.sessionFactory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -52,31 +54,33 @@ public User findByUUID(UUID uuid) { } public void save(User user) { - Session session = HibernateManager.getSessionFactory(srv).openSession(); - Transaction tx1 = session.beginTransaction(); - session.save(user); - tx1.commit(); - session.close(); + try (Session session = hibernate.sessionFactory.openSession()) { + Transaction tx1 = session.beginTransaction(); + session.save(user); + tx1.commit(); + } } public void update(User user) { - Session session = HibernateManager.getSessionFactory(srv).openSession(); - Transaction tx1 = session.beginTransaction(); - session.update(user); - tx1.commit(); - session.close(); + try (Session session = hibernate.sessionFactory.openSession()) { + Transaction tx1 = session.beginTransaction(); + session.update(user); + tx1.commit(); + } } public void delete(User user) { - Session session = HibernateManager.getSessionFactory(srv).openSession(); - Transaction tx1 = session.beginTransaction(); - session.delete(user); - tx1.commit(); - session.close(); + try (Session session = hibernate.sessionFactory.openSession()) { + Transaction tx1 = session.beginTransaction(); + session.delete(user); + tx1.commit(); + } } @SuppressWarnings("unchecked") public List findAll() { - return (List) HibernateManager.getSessionFactory(srv).openSession().createQuery("From User").list(); + try (Session s = hibernate.sessionFactory.openSession()) { + return (List) s.createQuery("From User").list(); + } } } \ No newline at end of file diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java deleted file mode 100644 index 6cfed7f9..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java +++ /dev/null @@ -1,20 +0,0 @@ -package pro.gravit.launchserver.hibernate; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.hibernate.SessionFactory; - -import pro.gravit.launchserver.LaunchServer; - -public class HibernateManager { - private static final Map sessionMap = new ConcurrentHashMap<>(); - - public static SessionFactory getSessionFactory(LaunchServer srv) { - return sessionMap.get(srv); - }; - - public static SessionFactory setSessionFactory(LaunchServer srv, SessionFactory fact) { - return sessionMap.get(srv); - }; -}