[FIX] Переписан код на try-with-resources в HibernateDAOImpl

This commit is contained in:
Zaxar163 2019-06-03 14:10:56 +03:00
parent 2dde0bb5e1
commit 11a23b4464
3 changed files with 29 additions and 44 deletions

View file

@ -2,13 +2,13 @@
import java.nio.file.Paths; import java.nio.file.Paths;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.LaunchServerDaoFactory; import pro.gravit.launchserver.dao.LaunchServerDaoFactory;
import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
import pro.gravit.launchserver.hibernate.HibernateManager;
import pro.gravit.utils.helper.CommonHelper; import pro.gravit.utils.helper.CommonHelper;
public class HibernateConfiguratorComponent extends Component { public class HibernateConfiguratorComponent extends Component {
@ -19,6 +19,7 @@ public class HibernateConfiguratorComponent extends Component {
public String pool_size; public String pool_size;
public String hibernateConfig; public String hibernateConfig;
public boolean parallelHibernateInit; public boolean parallelHibernateInit;
public SessionFactory sessionFactory = null;
@Override @Override
public void preInit(LaunchServer launchServer) { public void preInit(LaunchServer launchServer) {
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new); LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new);
@ -32,7 +33,7 @@ public void preInit(LaunchServer launchServer) {
.setProperty("hibernate.connection.pool_size", pool_size); .setProperty("hibernate.connection.pool_size", pool_size);
if(hibernateConfig != null) if(hibernateConfig != null)
cfg.configure(Paths.get(hibernateConfig).toFile()); cfg.configure(Paths.get(hibernateConfig).toFile());
HibernateManager.setSessionFactory(launchServer, cfg.buildSessionFactory()); sessionFactory = cfg.buildSessionFactory();
}; };
if(parallelHibernateInit) if(parallelHibernateInit)
CommonHelper.newThread("Hibernate Thread", true, init); CommonHelper.newThread("Hibernate Thread", true, init);

View file

@ -12,23 +12,25 @@
import org.hibernate.Transaction; import org.hibernate.Transaction;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.components.HibernateConfiguratorComponent;
import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO; import pro.gravit.launchserver.dao.UserDAO;
import pro.gravit.launchserver.hibernate.HibernateManager;
public class HibernateUserDAOImpl implements UserDAO { public class HibernateUserDAOImpl implements UserDAO {
private final LaunchServer srv; private final HibernateConfiguratorComponent hibernate;
public HibernateUserDAOImpl(LaunchServer srv) { 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) { 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) { public User findByUsername(String username) {
EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager(); EntityManager em = hibernate.sessionFactory.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> personCriteria = cb.createQuery(User.class); CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
@ -40,7 +42,7 @@ public User findByUsername(String username) {
} }
public User findByUUID(UUID uuid) { public User findByUUID(UUID uuid) {
EntityManager em = HibernateManager.getSessionFactory(srv).createEntityManager(); EntityManager em = hibernate.sessionFactory.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> personCriteria = cb.createQuery(User.class); CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
@ -52,31 +54,33 @@ public User findByUUID(UUID uuid) {
} }
public void save(User user) { public void save(User user) {
Session session = HibernateManager.getSessionFactory(srv).openSession(); try (Session session = hibernate.sessionFactory.openSession()) {
Transaction tx1 = session.beginTransaction(); Transaction tx1 = session.beginTransaction();
session.save(user); session.save(user);
tx1.commit(); tx1.commit();
session.close(); }
} }
public void update(User user) { public void update(User user) {
Session session = HibernateManager.getSessionFactory(srv).openSession(); try (Session session = hibernate.sessionFactory.openSession()) {
Transaction tx1 = session.beginTransaction(); Transaction tx1 = session.beginTransaction();
session.update(user); session.update(user);
tx1.commit(); tx1.commit();
session.close(); }
} }
public void delete(User user) { public void delete(User user) {
Session session = HibernateManager.getSessionFactory(srv).openSession(); try (Session session = hibernate.sessionFactory.openSession()) {
Transaction tx1 = session.beginTransaction(); Transaction tx1 = session.beginTransaction();
session.delete(user); session.delete(user);
tx1.commit(); tx1.commit();
session.close(); }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<User> findAll() { public List<User> findAll() {
return (List<User>) HibernateManager.getSessionFactory(srv).openSession().createQuery("From User").list(); try (Session s = hibernate.sessionFactory.openSession()) {
return (List<User>) s.createQuery("From User").list();
}
} }
} }

View file

@ -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<LaunchServer, SessionFactory> 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);
};
}