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 80246de1..1c29dfc3 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 @@ -9,30 +9,29 @@ import javax.persistence.criteria.Root; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.Transaction; import pro.gravit.launcher.OshiHWID; -import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.UserDAO; import pro.gravit.launchserver.dao.UserHWID; -import pro.gravit.launchserver.hibernate.SessionFactoryManager; public class HibernateUserDAOImpl implements UserDAO { - private final SessionFactoryManager manager; + private final SessionFactory factory; - public HibernateUserDAOImpl(LaunchServer srv) { - manager = SessionFactoryManager.forLaunchServer(srv); + public HibernateUserDAOImpl(SessionFactory factory) { + this.factory = factory; } public User findById(int id) { - try (Session s = manager.fact.openSession()) { + try (Session s = factory.openSession()) { return s.get(User.class, id); } } public User findByUsername(String username) { - EntityManager em = manager.fact.createEntityManager(); + EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -44,7 +43,7 @@ public User findByUsername(String username) { } public User findByUUID(UUID uuid) { - EntityManager em = manager.fact.createEntityManager(); + EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -57,7 +56,7 @@ public User findByUUID(UUID uuid) { @Override public List findHWID(OshiHWID hwid) { - EntityManager em = manager.fact.createEntityManager(); + EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(UserHWID.class); @@ -77,7 +76,7 @@ public List findHWID(OshiHWID hwid) { } public void save(User user) { - try (Session session = manager.fact.openSession()) { + try (Session session = factory.openSession()) { Transaction tx1 = session.beginTransaction(); session.save(user); tx1.commit(); @@ -85,7 +84,7 @@ public void save(User user) { } public void update(User user) { - try (Session session = manager.fact.openSession()) { + try (Session session = factory.openSession()) { Transaction tx1 = session.beginTransaction(); session.update(user); tx1.commit(); @@ -93,7 +92,7 @@ public void update(User user) { } public void delete(User user) { - try (Session session = manager.fact.openSession()) { + try (Session session = factory.openSession()) { Transaction tx1 = session.beginTransaction(); session.delete(user); tx1.commit(); @@ -102,7 +101,7 @@ public void delete(User user) { @SuppressWarnings("unchecked") public List findAll() { - try (Session s = manager.fact.openSession()) { + try (Session s = factory.openSession()) { return (List) s.createQuery("From User").list(); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java index c000e80e..541f43ed 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java @@ -6,7 +6,6 @@ import pro.gravit.launchserver.dao.UserHWID; import pro.gravit.launchserver.dao.UserService; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; -import pro.gravit.launchserver.hibernate.SessionFactoryManager; import pro.gravit.utils.helper.CommonHelper; import java.nio.file.Paths; @@ -22,8 +21,6 @@ public class HibernateDaoProvider extends DaoProvider { @Override public void init(LaunchServer server) { - userDAO = new HibernateUserDAOImpl(server); - userService = new UserService(userDAO); Runnable init = () -> { Configuration cfg = new Configuration() .addAnnotatedClass(User.class) @@ -35,7 +32,8 @@ public void init(LaunchServer server) { .setProperty("hibernate.connection.pool_size", pool_size); if(hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); - SessionFactoryManager.forLaunchServer(server).fact = cfg.buildSessionFactory(); + userDAO = new HibernateUserDAOImpl(cfg.buildSessionFactory()); + userService = new UserService(userDAO); }; if(parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java deleted file mode 100644 index 978c8004..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java +++ /dev/null @@ -1,18 +0,0 @@ -package pro.gravit.launchserver.hibernate; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; - -import org.hibernate.SessionFactory; - -import pro.gravit.launchserver.LaunchServer; - -public class SessionFactoryManager { - public SessionFactory fact; - private static final Map sessionFactories = new ConcurrentHashMap<>(); - private static final Function cr = e -> new SessionFactoryManager(); - public static SessionFactoryManager forLaunchServer(LaunchServer srv) { - return sessionFactories.computeIfAbsent(srv, cr); - } -}