org.apache.ignite.cache.store.jdbc

## Class CacheJdbcStoreSessionListener

- java.lang.Object
- - org.apache.ignite.cache.store.jdbc.CacheJdbcStoreSessionListener

- All Implemented Interfaces:
    CacheStoreSessionListener, LifecycleAware

      public class CacheJdbcStoreSessionListener
      extends Object
      implements CacheStoreSessionListener, LifecycleAware

  Cache store session listener based on JDBC connection.

  For each session this listener gets a new JDBC connection from provided `DataSource` and commits (or rolls back) it when session ends.

  The connection is saved as a store session `attachment`. The listener guarantees that the connection will be available for any store operation. If there is an ongoing cache transaction, all operations within this transaction will be committed or rolled back only when the session ends.

  As an example, here is how the `CacheWriter.write(Cache.Entry)` method can be implemented if `CacheJdbcStoreSessionListener` 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 connection from the current session.
               Connection conn = ses.attachment();

               // Execute update SQL query.
               try {
                   conn.createStatement().executeUpdate("...");
               }
               catch (SQLException e) {
                   throw new CacheWriterException("Failed to update the store.", e);
               }
           }
       }

  JDBC connection will be automatically created by the listener at the start of the session and closed when it ends.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CacheJdbcStoreSessionListener()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `DataSource` | `getDataSource()` Gets data source.
    `void` | `onSessionEnd(CacheStoreSession ses, boolean commit)` On session end callback.
    `void` | `onSessionStart(CacheStoreSession ses)` On session start callback.
    `void` | `setDataSource(DataSource dataSrc)` Sets data source.
    `void` | `start()` Starts grid component, called on grid start.
    `void` | `stop()` Stops grid component, called on grid shutdown.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CacheJdbcStoreSessionListener

          public CacheJdbcStoreSessionListener()

  - ### Method Detail

    - #### setDataSource

          public void setDataSource(DataSource dataSrc)

      Sets data source.

      This is a required parameter. If data source is not set, exception will be thrown on startup.

      Parameters:
        `dataSrc` - Data source.

    - #### getDataSource

          public DataSource getDataSource()

      Gets data source.
      Returns:
        Data source.

    - #### start

          public void start()
                     throws IgniteException

      Starts grid component, called on grid start.
      Specified by:
        `start` in interface `LifecycleAware`
      Throws:
        `IgniteException` - If failed.

    - #### stop

          public void stop()
                    throws IgniteException

      Stops grid component, called on grid shutdown.
      Specified by:
        `stop` in interface `LifecycleAware`
      Throws:
        `IgniteException` - If failed.

    - #### onSessionStart

          public void onSessionStart(CacheStoreSession ses)

      On session start callback.

      Called before any store operation within a session is invoked.

      Specified by:
        `onSessionStart` in interface `CacheStoreSessionListener`
      Parameters:
        `ses` - Current session.

    - #### onSessionEnd

          public void onSessionEnd(CacheStoreSession ses,
                                   boolean commit)

      On session end callback.

      Called after all operations within a session are invoked.

      Specified by:
        `onSessionEnd` in interface `CacheStoreSessionListener`
      Parameters:
        `ses` - Current session.
        `commit` - `True` if persistence store transaction should commit, `false` for rollback.

2020 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.9.0
Release Date | : October 15 2020