org.apache.ignite.cache.store

## Interface CacheStoreSessionListener

- All Known Implementing Classes:
    CacheJdbcStoreSessionListener, CacheSpringStoreSessionListener

      public interface CacheStoreSessionListener

  Cache store session listener that allows to implement callbacks for session lifecycle.

  The most common use case for session listeners is database connection and transaction management. Store can be invoked one or several times during one session, depending on whether it's executed within cache transaction or not. In any case, you have to create a connection when session is started and commit it or rollback when session is finished.

  Cache store session listener allows to implement this and other scenarios providing two callback methods:

  - `onSessionStart(CacheStoreSession)` - called when a session is created prior to all operations within his session.
  - `onSessionEnd(CacheStoreSession, boolean)` - called after all operations within a session are invoked.

  ## Implementations

  Ignite provides several out-of-the-box implementations of session listener (refer to individual JavaDocs for more details):
  - `CacheJdbcStoreSessionListener` - JDBC-based session listener. For each session it gets a new JDBC connection from provided `DataSource` and commits (or rolls back) it when session ends.
  - `CacheSpringStoreSessionListener` - session listener based on Spring transaction management. It starts a new DB transaction for each session and commits (or rolls back) it when session ends. If there is no ongoing cache transaction, this listener is no-op.
  - `CacheHibernateStoreSessionListener` - Hibernate-based session listener. It creates a new Hibernate session for each Ignite session. If there is an ongoing cache transaction, a corresponding Hibernate transaction is created as well.

  ## Configuration

  There are two ways to configure a session listener:
  - Provide a global listener for all caches via `IgniteConfiguration.setCacheStoreSessionListenerFactories(Factory[])` configuration property. This will we called for any store session, not depending on what caches participate in transaction.
  - Provide a listener for a particular cache via `CacheConfiguration.setCacheStoreSessionListenerFactories(Factory[])` configuration property. This will be called only if the cache participates in transaction.
  For example, here is how global `CacheJdbcStoreSessionListener` can be configured in Spring XML configuration file:

       <bean class="org.apache.ignite.configuration.IgniteConfiguration">
           ...

           <property name="CacheStoreSessionListenerFactories">
               <list>
                   <bean class="javax.cache.configuration.FactoryBuilder$SingletonFactory">
                       <constructor-arg>
                           <bean class="org.apache.ignite.cache.store.jdbc.CacheJdbcStoreSessionListener">
                               <!-- Inject external data source. -->
                               <property name="dataSource" ref="jdbc-data-source"/>
                           </bean>
                       </constructor-arg>
                   </bean>
               </list>
           </property>
       </bean>

- - ### Method Summary

    All Methods Instance Methods Abstract Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `onSessionEnd(CacheStoreSession ses, boolean commit)` On session end callback.
    `void` | `onSessionStart(CacheStoreSession ses)` On session start callback.

- - ### Method Detail

    - #### onSessionStart

          void onSessionStart(CacheStoreSession ses)

      On session start callback.

      Called before any store operation within a session is invoked.

      Parameters:
        `ses` - Current session.

    - #### onSessionEnd

          void onSessionEnd(CacheStoreSession ses,
                            boolean commit)

      On session end callback.

      Called after all operations within a session are invoked.

      Parameters:
        `ses` - Current session.
        `commit` - `True` if persistence store transaction should commit, `false` for rollback.

2021 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.10.0
Release Date | : March 10 2021