Class SessionHandler

  • All Implemented Interfaces:
    OBNotSingleton, OBProvidable

    public class SessionHandler
    extends Object
    implements OBNotSingleton
    Keeps the Hibernate Session and Transaction in a ThreadLocal so that it is available throughout the application. This class provides convenience methods to get a Session and to create/commit/rollback a transaction.
    Author:
    mtaal
    • Constructor Detail

      • SessionHandler

        public SessionHandler()
    • Method Detail

      • deleteSessionHandler

        public static void deleteSessionHandler()
        Removes the current SessionHandler from the ThreadLocal. A call to getInstance will create a new SessionHandler. A call to getSession will create a session and a transaction.
      • isSessionHandlerPresent

        public static boolean isSessionHandlerPresent()
        Checks whether a session handler is present for this thread and also available for the default pool.
        Returns:
        true if a session handler is present for this thread and available for the default pool, false otherwise
      • isSessionHandlerPresent

        public static boolean isSessionHandlerPresent​(String pool)
        Checks whether a session handler is present for this thread and also available for the pool whose name is passed as parameter.
        Parameters:
        pool - the name of the pool
        Returns:
        true if a session handler is present for this thread and available for the specified pool, false otherwise
      • existsOpenedSessions

        public static boolean existsOpenedSessions()
        Checks if the session handler has available sessions which are not closed yet.
        Returns:
        true if there are sessions not closed, false otherwise
      • getInstance

        public static SessionHandler getInstance()
        Returns the SessionHandler of this thread. If there is none then a new one is created.
        Returns:
        the sessionhandler for this thread
      • getSession

        public org.hibernate.Session getSession()
        Returns:
        the session
      • getSession

        public org.hibernate.Session getSession​(String pool)
        Gets a Session from the connection pool whose name is passed as parameter. If it was not created previously, this methods returns a newly created session from that pool.
        Parameters:
        pool - the name of the pool used to retrieve the session
        Returns:
        the session
      • isSessionDirty

        public boolean isSessionDirty​(String pool)
        Checks whether current session is dirty (there are remaining changes to be sent to DB). Note Session.isDirty() should not be directly invoked because it triggers Entity Persistence Observers to be executed for modified entities. This method handles it so that they are not called.
      • isSessionDirty

        public boolean isSessionDirty()
        Checks dirtiness for default session.
        See Also:
        isSessionDirty()
      • setSession

        protected void setSession​(org.hibernate.Session thisSession)
      • createSession

        protected org.hibernate.Session createSession()
      • closeSession

        protected void closeSession()
      • cleanUpSessions

        public void cleanUpSessions()
        Commits all remaining sessions and closes them
      • getNewConnection

        public Connection getNewConnection​(String pool)
                                    throws SQLException
        Gets a new Connection from the connection pool.
        Parameters:
        pool - the name of the pool used to retrieve the connection
        Returns:
        a Connection from the specified pool
        Throws:
        SQLException
      • getConnection

        public Connection getConnection()
        Gets current session's Connection if it's set, null if not.
      • setConnection

        public void setConnection​(Connection newConnection)
        Sets the connection of the default pool to be used by the handler.
        Parameters:
        newConnection - the connection of the default pool.
      • isCurrentTransactionActive

        public boolean isCurrentTransactionActive()
        Returns true when the current SessionHandler has a transaction and it is active.
      • beginNewTransaction

        public void beginNewTransaction()
                                 throws OBException
        Begins a new Transaction on the current HibernateSession and assigns it to the SessionHandler.
        Throws:
        OBException - if there is already an available active transaction.
      • save

        public void save​(Object obj)
        Saves the object in this getSession().
        Parameters:
        obj - the object to persist
      • save

        public void save​(String pool,
                         Object obj)
        Saves the object in the session of the pool whose name is passed as parameter.
        Parameters:
        pool - the name of the pool used to retrieve the session where the object will be saved
        obj - the object to persist
      • delete

        public void delete​(Object obj)
        Delete the object from the db.
        Parameters:
        obj - the object to remove
      • delete

        public void delete​(String pool,
                           Object obj)
        Delete the object from the db.
        Parameters:
        pool - the name of the pool used to retrieve the session where the object will be deleted
        obj - the object to remove
      • find

        public <T> T find​(Class<T> clazz,
                          Object id)
        Queries for a certain object using the class and id. If not found then null is returned.
        Parameters:
        clazz - the class to query
        id - the id to use for querying
        Returns:
        the retrieved object, can be null
      • find

        public <T> T find​(String pool,
                          Class<T> clazz,
                          Object id)
        Queries for a certain object using the class and id. If not found then null is returned.
        Parameters:
        pool - the name of the pool used to obtain the connection to execute the query
        clazz - the class to query
        id - the id to use for querying
        Returns:
        the retrieved object, can be null
      • find

        public BaseOBObject find​(String entityName,
                                 Object id)
        Queries for a certain object using the entity name and id. If not found then null is returned.
        Parameters:
        entityName - the name of the entity to query
        id - the id to use for querying
        Returns:
        the retrieved object, can be null
        See Also:
        Entity
      • find

        public BaseOBObject find​(String pool,
                                 String entityName,
                                 Object id)
        Queries for a certain object using the entity name and id. If not found then null is returned.
        Parameters:
        pool - the name of the pool used to obtain the connection to execute the query
        entityName - the name of the entity to query
        id - the id to use for querying
        Returns:
        the retrieved object, can be null
        See Also:
        Entity
      • createQuery

        public <T> org.hibernate.query.Query<T> createQuery​(String qryStr,
                                                            Class<T> clazz)
        Create a query object from the current getSession().
        Parameters:
        qryStr - the HQL query
        clazz - the class of the query's resulting objects
        Returns:
        a new Query object
      • createQuery

        public <T> org.hibernate.query.Query<T> createQuery​(String qryStr)
        Create a query object from the current getSession().
        Parameters:
        qryStr - the HQL query
        Returns:
        a new Query object
      • begin

        protected void begin()
        Starts a transaction.
      • commitAndClose

        public void commitAndClose()
        Commits the transaction and closes the session, should normally be called at the end of all the work.
      • commitAndClose

        public void commitAndClose​(String pool)
        Commits the transaction and closes the session, should normally be called at the end of all the work.
        Parameters:
        pool - the name of the pool which the transaction belongs.
      • commitAndStart

        public void commitAndStart()
        Commits the transaction and starts a new transaction.
      • rollback

        public void rollback()
        Rolls back the transaction and closes the session.
      • rollback

        public void rollback​(String pool)
        Rolls back the transaction and closes the session.
        Parameters:
        pool - the name of the pool which the transaction belongs.
      • setDoRollback

        public void setDoRollback​(boolean setRollback)
        Registers that the transaction should be rolled back. Is used by the DalThreadHandler.
        Parameters:
        setRollback - if true then the transaction will be rolled back at the end of the thread.
      • getDoRollback

        public boolean getDoRollback()
        Returns:
        the doRollback value
      • doSessionInViewPatter

        public boolean doSessionInViewPatter()
        Returns true if the session-in-view pattern should be supported. That is that the session is closed and committed at the end of the request.
        Returns:
        always true in this implementation