/******************************************************************************* * Copyright 2014 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ package at.gv.egovernment.moa.id.commons.db; import java.util.Properties; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import org.hibernate.HibernateException; import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; import at.gv.egovernment.moa.logging.Logger; public final class ConfigurationDBUtils { private static EntityManagerFactory entitymanagerfactory; @SuppressWarnings("rawtypes") private static final ThreadLocal THREAD_LOCAL_CONFIG = new ThreadLocal(); private static boolean automaticSessionHandling = false; protected ConfigurationDBUtils() { } public static void initHibernate(Properties props) throws MOADatabaseException { try { Logger.debug("Creating initial session factory..."); entitymanagerfactory = Persistence.createEntityManagerFactory("at.gv.egovernment.moa.id.commons.db.dao.config", props); Logger.debug("Initial ConfigDB session factory successfully created."); } catch (Throwable ex) { Logger.error("Initial session factory creation failed: " + ex.getMessage()); throw new MOADatabaseException("Initialization of Configuration Hibernate session factory failed.",ex); } } /** * Checks if a session factory is currently available. If necessary a new * session factory is created. * * @return current (or new) session factory * @throws HibernateException * thrown if a hibernate error occurs */ public static EntityManager getCurrentSession() { if (automaticSessionHandling) { return entitymanagerfactory.createEntityManager(); } EntityManager session = (EntityManager) THREAD_LOCAL_CONFIG.get(); if (session != null && session.isOpen()) { //maybe a hack, but sometimes we do not know if the session is closed (session already closed but isOpen()=true) try { javax.persistence.Query query = session.createQuery("select userdatabase from UserDatabase userdatabase"); query.getResultList(); } catch (Throwable e) { Logger.warn("JPA Session Handling Warning!!!! - This error should not occur."); session = getNewSession(); } } else session = getNewSession(); return session; } @SuppressWarnings("unchecked") public static EntityManager getNewSession() { if (automaticSessionHandling) { Logger.warn("Session is being automatically handled by hibernate. Therefore this session maybe not being newly created. Use HibernateUtil.getCurrentSession() instead."); return entitymanagerfactory.createEntityManager(); } EntityManager session = (EntityManager) THREAD_LOCAL_CONFIG.get(); if (session != null ) { Logger.warn("Previous session has not been closed; closing ConfigDB session now."); closeSession(); } Logger.debug("Opening new ConfigDB hibernate session..."); try { session = entitymanagerfactory.createEntityManager(); THREAD_LOCAL_CONFIG.set(session); } catch (HibernateException hex) { Logger.error(hex.getMessage()); } return session; } /** * Closes the current session. * * @throws HibernateException * thrown if session is already closed or a hibernate error * occurs. */ @SuppressWarnings("unchecked") public static void closeSession() { if (automaticSessionHandling) { Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand."); return; } Logger.debug("Closing current ConfigDB hibernate session..."); EntityManager session = (EntityManager) THREAD_LOCAL_CONFIG.get(); THREAD_LOCAL_CONFIG.set(null); if (session != null) { try { session.close(); } catch (HibernateException hex) { Logger.error(hex.getMessage()); } } } public static boolean save(Object dbo) throws MOADatabaseException { EntityTransaction tx = null; try { EntityManager session = ConfigurationDBUtils.getCurrentSession(); tx = session.getTransaction(); synchronized (session) { tx.begin(); session.persist(dbo); tx.commit(); //session.clear(); } return true; } catch(HibernateException e) { Logger.warn("Error during Config database saveOrUpdate. Rollback.", e); tx.rollback(); throw new MOADatabaseException(e); } } public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException { EntityTransaction tx = null; try { EntityManager session = ConfigurationDBUtils.getCurrentSession(); tx = session.getTransaction(); synchronized (session) { tx.begin(); session.merge(dbo); session.flush(); tx.commit(); //session.clear(); } return true; } catch(HibernateException e) { Logger.warn("Error during Config database saveOrUpdate. Rollback.", e); tx.rollback(); throw new MOADatabaseException(e); } } public static boolean delete(Object dbo) { EntityTransaction tx = null; try { EntityManager session = ConfigurationDBUtils.getCurrentSession(); tx = session.getTransaction(); synchronized (session) { tx.begin(); session.remove(session.contains(dbo) ? dbo : session.merge(dbo)); tx.commit(); } return true; } catch(HibernateException e) { Logger.warn("Error during Config database delete. Rollback.", e); tx.rollback(); return false; } } }