Class SessionFactoryManagerImpl
java.lang.Object
ch.tocco.nice2.persist.core.impl.hibernate.session.SessionFactoryManagerImpl
- All Implemented Interfaces:
ThreadHandlerListener
,SessionFactoryManager
@Component
public class SessionFactoryManagerImpl
extends Object
implements SessionFactoryManager, ThreadHandlerListener
This class manages the
Session
lifecycle for both the old (ContextManagerAdapter
) and
new (PersistenceServiceImpl
) persistence API.
This makes it possible to use both APIs together.
This class contains the 'implicit' session in a ThreadLocal
. The implicit session
is created automatically when the persistence API is accessed for the first time in a thread
(unless another session is opened explicitly before that).
At the end of each request, the implicit session is closed and removed using a ThreadHandlerListener
.
Sessions created by this class are always bound to the current thread. Also a SessionEventListener
is added
which detaches the new session from the thread and re-attached the previous session (if present) when the new session
is closed.-
Constructor Summary
ConstructorDescriptionSessionFactoryManagerImpl
(org.slf4j.Logger logger, org.hibernate.SessionFactory sessionFactory, ThreadHandlerManager threadHandlerManager, CacheControlFactory cacheControlFactory) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(SessionFactoryManagerListener listener) void
Bind the givenSession
to the current thread.void
void
InvokesSessionFactoryManagerListener
instances and then closes theSession
.Creates and returns a newSession
.createNewSession
(@Nullable Integer queryTimeout) createNewSession
(@Nullable Connection connection) createNewSession
(@Nullable Connection connection, @Nullable Integer queryTimeout) void
Detach the givenSession
from the current thread.Returns theSession
that is bound to the current thread.@Nullable PersistenceSessionImplementor
Returns the implicitSession
for the current thread.boolean
loadEntityClassByName
(String entityName) void
removeListener
(SessionFactoryManagerListener listener) void
setDefaultQueryTimeout
(int defaultQueryTimeout) void
setListeners
(org.springframework.beans.factory.ObjectProvider<SessionFactoryManagerListener> listeners) void
-
Constructor Details
-
SessionFactoryManagerImpl
public SessionFactoryManagerImpl(org.slf4j.Logger logger, org.hibernate.SessionFactory sessionFactory, ThreadHandlerManager threadHandlerManager, CacheControlFactory cacheControlFactory)
-
-
Method Details
-
setListeners
@Autowired public void setListeners(org.springframework.beans.factory.ObjectProvider<SessionFactoryManagerListener> listeners) -
threadIsCleaningUp
public void threadIsCleaningUp()- Specified by:
threadIsCleaningUp
in interfaceThreadHandlerListener
-
getCurrentSession
Description copied from interface:SessionFactoryManager
Returns theSession
that is bound to the current thread. If no session is bound to the current thread, the implicit session is created (if it does not exist yet) and returned.- Specified by:
getCurrentSession
in interfaceSessionFactoryManager
-
clearImplicitSession
public void clearImplicitSession()- Specified by:
clearImplicitSession
in interfaceSessionFactoryManager
-
getImplicitSession
Description copied from interface:SessionFactoryManager
Returns the implicitSession
for the current thread. This may return null if the implicit session has not been initialized yet.- Specified by:
getImplicitSession
in interfaceSessionFactoryManager
-
createNewSession
Description copied from interface:SessionFactoryManager
Creates and returns a newSession
. The new session will automatically bound to the current thread. If this session will be closed, it will automatically be detached from the current thread and it is attempted to re-attach the previous session.- Specified by:
createNewSession
in interfaceSessionFactoryManager
-
createNewSession
- Specified by:
createNewSession
in interfaceSessionFactoryManager
-
createNewSession
- Specified by:
createNewSession
in interfaceSessionFactoryManager
-
createNewSession
public PersistenceSessionImplementor createNewSession(@Nullable @Nullable Connection connection, @Nullable @Nullable Integer queryTimeout) - Specified by:
createNewSession
in interfaceSessionFactoryManager
-
attachSessionToThread
Description copied from interface:SessionFactoryManager
Bind the givenSession
to the current thread.- Specified by:
attachSessionToThread
in interfaceSessionFactoryManager
-
detachSessionFromThread
Description copied from interface:SessionFactoryManager
Detach the givenSession
from the current thread.- Specified by:
detachSessionFromThread
in interfaceSessionFactoryManager
-
isAttachedToThread
- Specified by:
isAttachedToThread
in interfaceSessionFactoryManager
- Returns:
- true if the given
Session
is bound to the current thread.
-
closeSession
Description copied from interface:SessionFactoryManager
InvokesSessionFactoryManagerListener
instances and then closes theSession
. All sessions should be closed through this method.- Specified by:
closeSession
in interfaceSessionFactoryManager
-
addListener
- Specified by:
addListener
in interfaceSessionFactoryManager
-
removeListener
- Specified by:
removeListener
in interfaceSessionFactoryManager
-
loadEntityClassByName
- Specified by:
loadEntityClassByName
in interfaceSessionFactoryManager
-
setDefaultQueryTimeout
@Value("${nice2.persist.defaultQueryTimeout}") public void setDefaultQueryTimeout(int defaultQueryTimeout)
-