Package org.openbravo.dal.core
Class SessionHandler
- java.lang.Object
-
- org.openbravo.dal.core.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 Summary
Constructors Constructor Description SessionHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
begin()
Starts a transaction.void
beginNewTransaction()
Begins a new Transaction on the current HibernateSession and assigns it to the SessionHandler.void
cleanUpSessions()
Commits all remaining sessions and closes themprotected void
closeSession()
void
commitAndClose()
Commits the transaction and closes the session, should normally be called at the end of all the work.void
commitAndClose(String pool)
Commits the transaction and closes the session, should normally be called at the end of all the work.void
commitAndStart()
Commits the transaction and starts a new transaction.<T> org.hibernate.query.Query<T>
createQuery(String qryStr)
Create a query object from the current getSession().<T> org.hibernate.query.Query<T>
createQuery(String qryStr, Class<T> clazz)
Create a query object from the current getSession().protected org.hibernate.Session
createSession()
void
delete(Object obj)
Delete the object from the db.void
delete(String pool, Object obj)
Delete the object from the db.static void
deleteSessionHandler()
Removes the current SessionHandler from the ThreadLocal.boolean
doSessionInViewPatter()
Returns true if the session-in-view pattern should be supported.static boolean
existsOpenedSessions()
Checks if the session handler has available sessions which are not closed yet.<T> T
find(Class<T> clazz, Object id)
Queries for a certain object using the class and id.<T> T
find(String pool, Class<T> clazz, Object id)
Queries for a certain object using the class and id.BaseOBObject
find(String entityName, Object id)
Queries for a certain object using the entity name and id.BaseOBObject
find(String pool, String entityName, Object id)
Queries for a certain object using the entity name and id.Connection
getConnection()
Gets current session'sConnection
if it's set,null
if not.boolean
getDoRollback()
static SessionHandler
getInstance()
Returns the SessionHandler of this thread.Connection
getNewConnection()
Gets a newConnection
from the connection pool.Connection
getNewConnection(String pool)
Gets a newConnection
from the connection pool.org.hibernate.Session
getSession()
org.hibernate.Session
getSession(String pool)
Gets aSession
from the connection pool whose name is passed as parameter.boolean
isCurrentTransactionActive()
Returns true when the current SessionHandler has a transaction and it is active.boolean
isSessionDirty()
Checks dirtiness for default session.boolean
isSessionDirty(String pool)
Checks whether current session is dirty (there are remaining changes to be sent to DB).static boolean
isSessionHandlerPresent()
Checks whether a session handler is present for this thread and also available for the default pool.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.void
rollback()
Rolls back the transaction and closes the session.void
rollback(String pool)
Rolls back the transaction and closes the session.void
save(Object obj)
Saves the object in this getSession().void
save(String pool, Object obj)
Saves the object in the session of the pool whose name is passed as parameter.void
setConnection(Connection newConnection)
Sets the connection of the default pool to be used by the handler.void
setDoRollback(boolean setRollback)
Registers that the transaction should be rolled back.protected void
setSession(org.hibernate.Session thisSession)
-
-
-
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 aSession
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). NoteSession.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() throws SQLException
Gets a newConnection
from the connection pool.- Throws:
SQLException
-
getNewConnection
public Connection getNewConnection(String pool) throws SQLException
Gets a newConnection
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'sConnection
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 savedobj
- 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 deletedobj
- 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 queryid
- 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 queryclazz
- the class to queryid
- 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 queryid
- 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 queryentityName
- the name of the entity to queryid
- 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 queryclazz
- 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 theDalThreadHandler
.- 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
-
-