public class CacheHibernateStoreSessionListener extends Object implements CacheStoreSessionListener, LifecycleAware
This listener creates a new Hibernate session for each store session. If there is an ongoing cache transaction, a corresponding Hibernate transaction is created as well.
The Hibernate session is saved as a store session
attachment
.
The listener guarantees that the session will be
available for any store operation. If there is an
ongoing cache transaction, all operations within this
transaction will share a DB transaction.
As an example, here is how the CacheWriter.write(javax.cache.Cache.Entry)
method can be implemented if CacheHibernateStoreSessionListener
is configured:
private static class Store extends CacheStoreAdapter<Integer, Integer> { @CacheStoreSessionResource private CacheStoreSession ses; @Override public void write(Cache.Entry<? extends Integer, ? extends Integer> entry) throws CacheWriterException { // Get Hibernate session from the current store session. Session hibSes = ses.attachment(); // Persist the value. hibSes.persist(entry.getValue()); } }Hibernate session will be automatically created by the listener at the start of the session and closed when it ends.
CacheHibernateStoreSessionListener
requires that either
setSessionFactory(SessionFactory)
session factory}
or Hibernate configuration file
is provided. If non of them is set, exception is thrown. Is both are provided,
session factory will be used.
Constructor and Description |
---|
CacheHibernateStoreSessionListener() |
Modifier and Type | Method and Description |
---|---|
String |
getHibernateConfigurationPath()
Gets hibernate configuration path.
|
org.hibernate.SessionFactory |
getSessionFactory()
Gets Hibernate session factory.
|
void |
onSessionEnd(CacheStoreSession ses,
boolean commit)
On session end callback.
|
void |
onSessionStart(CacheStoreSession ses)
On session start callback.
|
void |
setHibernateConfigurationPath(String hibernateCfgPath)
Sets hibernate configuration path.
|
void |
setSessionFactory(org.hibernate.SessionFactory sesFactory)
Sets Hibernate session factory.
|
void |
start()
Starts grid component, called on grid start.
|
void |
stop()
Stops grid component, called on grid shutdown.
|
public void setSessionFactory(org.hibernate.SessionFactory sesFactory)
Either session factory or configuration file is required. If none is provided, exception will be thrown on startup.
sesFactory
- Session factory.public org.hibernate.SessionFactory getSessionFactory()
public void setHibernateConfigurationPath(String hibernateCfgPath)
Either session factory or configuration file is required. If none is provided, exception will be thrown on startup.
hibernateCfgPath
- Hibernate configuration path.public String getHibernateConfigurationPath()
public void start() throws IgniteException
start
in interface LifecycleAware
IgniteException
- If failed.public void stop() throws IgniteException
stop
in interface LifecycleAware
IgniteException
- If failed.public void onSessionStart(CacheStoreSession ses)
Called before any store operation within a session is invoked.
onSessionStart
in interface CacheStoreSessionListener
ses
- Current session.public void onSessionEnd(CacheStoreSession ses, boolean commit)
Called after all operations within a session are invoked.
onSessionEnd
in interface CacheStoreSessionListener
ses
- Current session.commit
- True
if persistence store transaction
should commit, false
for rollback.
GridGain In-Memory Computing Platform : ver. 8.9.19 Release Date : April 10 2025