From 2dde0bb5e11baa45f3e12c3beb2f76715c49adf0 Mon Sep 17 00:00:00 2001 From: Zaxar163 Date: Mon, 3 Jun 2019 13:56:54 +0300 Subject: [PATCH] [FIX] Hibernate. --- .../HibernateConfiguratorComponent.java | 4 ++-- .../dao/LaunchServerDaoFactory.java | 16 +++++++++------ .../gravit/launchserver/dao/UserService.java | 2 +- .../dao/impl/DefaultUserDAOImpl.java | 2 ++ .../dao/impl/HibernateUserDAOImpl.java | 20 ++++++++++++------- .../hibernate/HibernateManager.java | 15 +++++++++++++- 6 files changed, 42 insertions(+), 17 deletions(-) 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 da4171f5..6b5017e6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -21,7 +21,7 @@ public class HibernateConfiguratorComponent extends Component { public boolean parallelHibernateInit; @Override public void preInit(LaunchServer launchServer) { - LaunchServerDaoFactory.setUserDaoProvider(HibernateUserDAOImpl::new); + LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new); Runnable init = () -> { Configuration cfg = new Configuration() .addAnnotatedClass(User.class) @@ -32,7 +32,7 @@ public void preInit(LaunchServer launchServer) { .setProperty("hibernate.connection.pool_size", pool_size); if(hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); - HibernateManager.sessionFactory = cfg.buildSessionFactory(); + HibernateManager.setSessionFactory(launchServer, cfg.buildSessionFactory()); }; if(parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java index c1184c09..9c5b39ef 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java @@ -1,18 +1,22 @@ package pro.gravit.launchserver.dao; -import java.util.function.Supplier; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl; public class LaunchServerDaoFactory { - private static Supplier getUserDao = DefaultUserDAOImpl::new; + private static final Function defDao = DefaultUserDAOImpl::new; + private static final Map> daos = new ConcurrentHashMap<>(); - public static void setUserDaoProvider(Supplier getDao) { - LaunchServerDaoFactory.getUserDao = getDao; + public static void setUserDaoProvider(LaunchServer srv, Function getDao) { + daos.put(srv, getDao); } - public static UserDAO createUserDao() + public static UserDAO createUserDao(LaunchServer srv) { - return getUserDao.get(); + return daos.getOrDefault(srv, defDao).apply(srv); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java index 73332786..57155f32 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java @@ -10,7 +10,7 @@ public class UserService { private final UserDAO usersDao; public UserService(LaunchServer server) { - usersDao = LaunchServerDaoFactory.createUserDao(); + usersDao = LaunchServerDaoFactory.createUserDao(server); } public User findUser(int id) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java index 0c9e6507..31027e1a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java @@ -4,10 +4,12 @@ import java.util.List; import java.util.UUID; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.UserDAO; public class DefaultUserDAOImpl implements UserDAO { + public DefaultUserDAOImpl(LaunchServer srv) { } @Override public User findById(int id) { return null; 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 13dd60a9..818cb4a2 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 @@ -11,18 +11,24 @@ import org.hibernate.Session; import org.hibernate.Transaction; +import pro.gravit.launchserver.LaunchServer; 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; + + public HibernateUserDAOImpl(LaunchServer srv) { + this.srv = srv; + } public User findById(int id) { - return HibernateManager.sessionFactory.openSession().get(User.class, id); + return HibernateManager.getSessionFactory(srv).openSession().get(User.class, id); } public User findByUsername(String username) { - EntityManager em = HibernateManager.sessionFactory.createEntityManager(); + EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -34,7 +40,7 @@ public User findByUsername(String username) { } public User findByUUID(UUID uuid) { - EntityManager em = HibernateManager.sessionFactory.createEntityManager(); + EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -46,7 +52,7 @@ public User findByUUID(UUID uuid) { } public void save(User user) { - Session session = HibernateManager.sessionFactory.openSession(); + Session session = HibernateManager.getSessionFactory(srv).openSession(); Transaction tx1 = session.beginTransaction(); session.save(user); tx1.commit(); @@ -54,7 +60,7 @@ public void save(User user) { } public void update(User user) { - Session session = HibernateManager.sessionFactory.openSession(); + Session session = HibernateManager.getSessionFactory(srv).openSession(); Transaction tx1 = session.beginTransaction(); session.update(user); tx1.commit(); @@ -62,7 +68,7 @@ public void update(User user) { } public void delete(User user) { - Session session = HibernateManager.sessionFactory.openSession(); + Session session = HibernateManager.getSessionFactory(srv).openSession(); Transaction tx1 = session.beginTransaction(); session.delete(user); tx1.commit(); @@ -71,6 +77,6 @@ public void delete(User user) { @SuppressWarnings("unchecked") public List findAll() { - return (List) HibernateManager.sessionFactory.openSession().createQuery("From User").list(); + return (List) HibernateManager.getSessionFactory(srv).openSession().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 index fc770f90..6cfed7f9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java @@ -1,7 +1,20 @@ 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 { - public static SessionFactory sessionFactory; + 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); + }; }