diff options
author | Thomas Lenz <tlenz@iaik.tugraz.at> | 2013-07-26 17:06:11 +0200 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2013-07-26 17:06:11 +0200 |
commit | cc2e2e4ecf5bd8c4bbe16edba5a7d63fa808adcb (patch) | |
tree | 21707a6f7248c0955eee6bba34621fdaee29730e /id/server/moa-id-commons/src | |
parent | 59fd2c0ea0649c94340d67b735a2d53696065e4c (diff) | |
download | moa-id-spss-cc2e2e4ecf5bd8c4bbe16edba5a7d63fa808adcb.tar.gz moa-id-spss-cc2e2e4ecf5bd8c4bbe16edba5a7d63fa808adcb.tar.bz2 moa-id-spss-cc2e2e4ecf5bd8c4bbe16edba5a7d63fa808adcb.zip |
Bugfix:
Database Session management
Diffstat (limited to 'id/server/moa-id-commons/src')
4 files changed, 100 insertions, 38 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java index 7a6efc5ac..80d95628e 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBRead.java @@ -8,6 +8,7 @@ import javax.persistence.EntityManager; import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration; import at.gv.egovernment.moa.id.commons.db.dao.config.OnlineApplication; +import at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.MiscUtil; @@ -16,8 +17,11 @@ public class ConfigurationDBRead { private static Map<String, String> QUERIES = new HashMap<String, String>(); static { - QUERIES.put("getOnlineApplicationWithID", "select onlineapplication from OnlineApplication onlineapplication where onlineapplication.publicURLPrefix like SUBSTRING(:id, 1, LENGTH(onlineapplication.publicURLPrefix))"); + QUERIES.put("getOnlineApplicationWithID", "select onlineapplication from OnlineApplication onlineapplication where onlineapplication.publicURLPrefix like SUBSTRING(:id, 1, LENGTH(onlineapplication.publicURLPrefix)) and onlineapplication.isActive = '1'"); QUERIES.put("getMOAIDConfiguration", "select moaidconfiguration from MOAIDConfiguration moaidconfiguration"); + QUERIES.put("getAllOnlineApplications", "select onlineapplication from OnlineApplication onlineapplication"); + QUERIES.put("getUserWithUserID", "select userdatabase from UserDatabase userdatabase where userdatabase.id = :id"); + } @SuppressWarnings("rawtypes") @@ -26,20 +30,21 @@ public class ConfigurationDBRead { Logger.trace("Getting OnlineApplication with ID " + id + " from database."); List result; - EntityManager session = ConfigurationDBUtils.getCurrentSession(); - - javax.persistence.Query query = session.createQuery(QUERIES.get("getOnlineApplicationWithID")); - //query.setParameter("id", id+"%"); - query.setParameter("id", id); - result = query.getResultList(); + EntityManager session = ConfigurationDBUtils.getCurrentSession(); - Logger.trace("Found entries: " + result.size()); - - if (result.size() == 0) { - Logger.trace("No entries found."); - return null; - } - return (OnlineApplication) result.get(0); + javax.persistence.Query query = session.createQuery(QUERIES.get("getOnlineApplicationWithID")); + //query.setParameter("id", id+"%"); + query.setParameter("id", id); + result = query.getResultList(); + + Logger.trace("Found entries: " + result.size()); + + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + + return (OnlineApplication) result.get(0); } public static MOAIDConfiguration getMOAIDConfiguration() { @@ -57,8 +62,45 @@ public class ConfigurationDBRead { Logger.trace("No entries found."); return null; } + return (MOAIDConfiguration) result.get(0); } + public static List<OnlineApplication> getAllOnlineApplications() { + Logger.trace("Get All OnlineApplications from database."); + + List<OnlineApplication> result; + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + + javax.persistence.Query query = session.createQuery(QUERIES.get("getAllOnlineApplications")); + result = query.getResultList(); + + Logger.trace("Found entries: " + result.size()); + + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return result; + } + public static UserDatabase getUserWithID(long id) { + MiscUtil.assertNotNull(id, "UserID"); + Logger.trace("Getting Userinformation with ID " + id + " from database."); + + List<UserDatabase> result; + EntityManager session = ConfigurationDBUtils.getCurrentSession(); + + javax.persistence.Query query = session.createQuery(QUERIES.get("getUserWithUserID")); + query.setParameter("id", id); + result = query.getResultList(); + + Logger.trace("Found entries: " + result.size()); + + if (result.size() == 0) { + Logger.trace("No entries found."); + return null; + } + return (UserDatabase) result.get(0); + } } diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java index 4bb0a08ea..d3ee1442e 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/ConfigurationDBUtils.java @@ -8,6 +8,7 @@ import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import org.hibernate.HibernateException; +import org.hibernate.Session; import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; @@ -44,10 +45,8 @@ public final class ConfigurationDBUtils { entitymanagerfactory = Persistence.createEntityManagerFactory("at.gv.egovernment.moa.id.commons.db.dao.config", props); - - - - Logger.debug("Initial session factory successfully created."); + + Logger.debug("Initial ConfigDB session factory successfully created."); } catch (Throwable ex) { @@ -72,10 +71,21 @@ public final class ConfigurationDBUtils { } EntityManager session = (EntityManager) THREAD_LOCAL_CONFIG.get(); - // Open a new Session, if this Thread has none yet - if (session == null || !session.isOpen()) { - session = getNewSession(); - } + + 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) { + session = getNewSession(); + } + + } else + session = getNewSession(); + return session; } @@ -88,10 +98,10 @@ public final class ConfigurationDBUtils { } EntityManager session = (EntityManager) THREAD_LOCAL_CONFIG.get(); if (session != null ) { - Logger.warn("Previous session has not been closed; closing session now."); + Logger.warn("Previous session has not been closed; closing ConfigDB session now."); closeSession(); } - Logger.debug("Opening new hibernate session..."); + Logger.debug("Opening new ConfigDB hibernate session..."); try { session = entitymanagerfactory.createEntityManager(); THREAD_LOCAL_CONFIG.set(session); @@ -114,7 +124,7 @@ public final class ConfigurationDBUtils { Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand."); return; } - Logger.debug("Closing current hibernate session..."); + Logger.debug("Closing current ConfigDB hibernate session..."); EntityManager session = (EntityManager) THREAD_LOCAL_CONFIG.get(); THREAD_LOCAL_CONFIG.set(null); if (session != null) { @@ -144,7 +154,7 @@ public final class ConfigurationDBUtils { return true; } catch(HibernateException e) { - Logger.warn("Error during database saveOrUpdate. Rollback.", e); + Logger.warn("Error during Config database saveOrUpdate. Rollback.", e); tx.rollback(); throw new MOADatabaseException(e); } @@ -171,7 +181,7 @@ public final class ConfigurationDBUtils { return true; } catch(HibernateException e) { - Logger.warn("Error during database saveOrUpdate. Rollback.", e); + Logger.warn("Error during Config database saveOrUpdate. Rollback.", e); tx.rollback(); throw new MOADatabaseException(e); } @@ -194,7 +204,7 @@ public final class ConfigurationDBUtils { return true; } catch(HibernateException e) { - Logger.warn("Error during database delete. Rollback.", e); + Logger.warn("Error during Config database delete. Rollback.", e); tx.rollback(); return false; } diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java index a89ede528..5e4ec0f13 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java @@ -38,7 +38,7 @@ public final class MOASessionDBUtils { String scm = StringUtils.trimToNull(hibernateProperties.getProperty(SESSION_HANDLING_KEY)); if (scm != null) { - automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1; + automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1; } Logger.debug("Evaluating hibernate property \"" + SESSION_HANDLING_KEY + "\"."); if (automaticSessionHandling) { @@ -48,15 +48,15 @@ public final class MOASessionDBUtils { } try { //Create the SessionFactory - Logger.debug("Creating initial session factory..."); + Logger.debug("Creating initial MOASession session factory..."); config.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); sessionFactory = config.buildSessionFactory(serviceRegistry); - Logger.debug("Initial session factory successfully created."); + Logger.debug("Initial MOASession session factory successfully created."); } catch (Throwable ex) { - Logger.error("Initial session factory creation failed: " + ex.getMessage()); + Logger.error("Initial MOASession session factory creation failed: " + ex.getMessage()); throw new ExceptionInInitializerError(ex); } } @@ -89,10 +89,10 @@ public final class MOASessionDBUtils { } Session session = (Session) THREAD_LOCAL.get(); if (session != null) { - Logger.warn("Previous session has not been closed; closing session now."); + Logger.warn("Previous MOASession session has not been closed; closing session now."); closeSession(); } - Logger.debug("Opening new hibernate session..."); + Logger.debug("Opening new MOASession hibernate session..."); try { session = sessionFactory.openSession(); THREAD_LOCAL.set(session); @@ -115,7 +115,7 @@ public final class MOASessionDBUtils { Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand."); return; } - Logger.debug("Closing current hibernate session..."); + Logger.debug("Closing current MOASession hibernate session..."); Session session = (Session) THREAD_LOCAL.get(); THREAD_LOCAL.set(null); if (session != null) { @@ -141,7 +141,7 @@ public final class MOASessionDBUtils { return true; } catch(HibernateException e) { - Logger.warn("Error during database saveOrUpdate. Rollback.", e); + Logger.warn("Error during MOASession database saveOrUpdate. Rollback.", e); tx.rollback(); throw new MOADatabaseException(e); } @@ -161,7 +161,7 @@ public final class MOASessionDBUtils { return true; } catch(HibernateException e) { - Logger.warn("Error during database delete. Rollback.", e); + Logger.warn("Error during MOASession database delete. Rollback.", e); tx.rollback(); return false; } diff --git a/id/server/moa-id-commons/src/main/resources/config/persistence_template.xml b/id/server/moa-id-commons/src/main/resources/config/persistence_template.xml index 727be25ec..bd60f5a46 100644 --- a/id/server/moa-id-commons/src/main/resources/config/persistence_template.xml +++ b/id/server/moa-id-commons/src/main/resources/config/persistence_template.xml @@ -2,6 +2,16 @@ <persistence version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <persistence-unit name="##generated"> - <class>at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase</class> + <class>at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase</class> + +<!-- <properties> + C3p0 connection pooling configuration + <property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider"/> + <property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider"/> + <property name="acquireRetryDelay" value="5000"/> + <property name="breakAfterAcquireFailure" value="true"/> + <property name="checkoutTimeout" value="1"/> + <property name="testConnectionOnCheckin" value="1" /> + </properties> --> </persistence-unit> </persistence> |