org.apache.ignite.spi.checkpoint.jdbc

## Class JdbcCheckpointSpi

- java.lang.Object
- - org.apache.ignite.spi.IgniteSpiAdapter
  - - org.apache.ignite.spi.checkpoint.jdbc.JdbcCheckpointSpi

All Implemented Interfaces:
  CheckpointSpi, JdbcCheckpointSpiMBean, IgniteSpi, IgniteSpiManagementMBean

    @IgniteSpiMultipleInstancesSupport(value=true)
    public class JdbcCheckpointSpi
    extends IgniteSpiAdapter
    implements CheckpointSpi, JdbcCheckpointSpiMBean

This class defines JDBC checkpoint SPI implementation. All checkpoints are stored in the database table and available from all nodes in the grid. Note that every node must have access to the database. The reason of having it is because a job state can be saved on one node and loaded on another (e.g., if a job gets preempted on a different node after node failure).

# Configuration

## Mandatory

This SPI has following mandatory configuration parameters.

- DataSource (see `setDataSource(DataSource)`).

## Optional

This SPI has following optional configuration parameters:

- Checkpoint table name (see `setCheckpointTableName(String)`).
- Checkpoint key field name (see `setKeyFieldName(String)`).
- Checkpoint key field type (see `setKeyFieldType(String)`).
- Checkpoint value field name (see `setValueFieldName(String)`).
- Checkpoint value field type (see `setValueFieldType(String)`).
- Checkpoint expiration date field name (see `setExpireDateFieldName(String)`).
- Checkpoint expiration date field type (see `setExpireDateFieldType(String)`).
- Number of retries in case of any failure (see `setNumberOfRetries(int)`).
- User name (see `setUser(String)`).
- Password (see `setPwd(String)`).

## Apache DBCP

Apache DBCP project provides various wrappers for data sources and connection pools. You can use these wrappers as Spring beans to configure this SPI from Spring configuration file. Refer to `Apache DBCP` project for more information.

## Java Example

`JdbcCheckpointSpi` can be configured as follows:

     GridConfiguration cfg = new GridConfiguration();

     GridJdbcCheckpointSpi checkpointSpi = new GridJdbcCheckpointSpi();

     javax.sql.DataSource ds = ... // Set datasource.

     // Set jdbc checkpoint SPI parameters.
     checkpointSpi.setDataSource(ds);
     checkpointSpi.setUser("test");
     checkpointSpi.setPassword("test");

     // Override default checkpoint SPI.
     cfg.setCheckpointSpi(checkpointSpi);

     // Starts grid.
     G.start(cfg);

## Spring Example

`JdbcCheckpointSpi` can be configured from Spring XML configuration file:

     <bean id="grid.custom.cfg" class="org.apache.ignite.configuration.IgniteConfiguration" singleton="true">
         ...
         <property name="checkpointSpi">
             <bean class="org.apache.ignite.spi.checkpoint.jdbc.JdbcCheckpointSpi">
                 <property name="dataSrc"><ref bean="anyPooledDataSourceBean" /></property>
                 <property name="checkpointTableName" value="GRID_CHECKPOINTS" />
                 <property name="user" value="test" />
                 <property name="password" value="test" />
             </bean>
         </property>
         ...
     </bean>

For information about Spring framework visit www.springframework.org

### Field Summary

Fields
`static String` `DFLT_CHECKPOINT_TABLE_NAME` Default checkpoint table name (value is `CHECKPOINTS`).
`static String` `DFLT_EXPIRE_DATE_FIELD_NAME` Default expiration date field name (value is `EXPIRE_DATE`).
`static String` `DFLT_EXPIRE_DATE_FIELD_TYPE` Default expiration date field type (value is `DATETIME`).
`static String` `DFLT_KEY_FIELD_NAME` Default checkpoint key field name (value is `NAME`).
`static String` `DFLT_KEY_FIELD_TYPE` Default checkpoint key field type (value is `VARCHAR(256)`).
`static int` `DFLT_NUMBER_OF_RETRIES` Default number of retries in case of errors (value is `2`).
`static String` `DFLT_VALUE_FIELD_NAME` Default checkpoint value field name (value is `VALUE`).
`static String` `DFLT_VALUE_FIELD_TYPE` Default checkpoint value field type (value is `BLOB`).

### Fields inherited from class org.apache.ignite.spi.IgniteSpiAdapter

`ignite`

### Constructor Summary

Constructors
Constructor and Description
`JdbcCheckpointSpi()`

### Method Summary

Methods
`String` `getCheckpointTableName()` Gets checkpoint table name.
`String` `getDataSourceInfo()` Gets data source description.
`String` `getExpireDateFieldName()` Gets expiration date field name for checkpoint table.
`String` `getExpireDateFieldType()` Gets expiration date field type for checkpoint table.
`String` `getKeyFieldName()` Gets key field name for checkpoint table.
`String` `getKeyFieldType()` Gets key field type for checkpoint table.
`int` `getNumberOfRetries()` Gets number of retries in case of DB failure.
`String` `getPwd()` Gets checkpoint jdbc password.
`String` `getUser()` Gets checkpoint jdbc user name.
`String` `getValueFieldName()` Gets value field name for checkpoint table.
`String` `getValueFieldType()` Gets value field type for checkpoint table.
`byte[]` `loadCheckpoint(String key)` Loads checkpoint from storage by its unique key.
`protected void` `onContextDestroyed0()` Method to be called in the beginning of onContextDestroyed() method.
`boolean` `removeCheckpoint(String key)` This method instructs the checkpoint provider to clean saved data for a given `key`.
`boolean` `saveCheckpoint(String key, byte[] state, long timeout, boolean overwrite)` Saves checkpoint to the storage.
`void` `setCheckpointListener(CheckpointListener lsnr)` Sets the checkpoint listener.
`void` `setCheckpointTableName(String tblName)` Sets checkpoint table name.
`void` `setDataSource(DataSource dataSrc)` Sets DataSource to use for database access.
`void` `setExpireDateFieldName(String expDateName)` Sets checkpoint expiration date field name.
`void` `setExpireDateFieldType(String expDateType)` Sets checkpoint expiration date field type.
`void` `setKeyFieldName(String keyName)` Sets checkpoint key field name.
`void` `setKeyFieldType(String keyType)` Sets checkpoint key field type.
`void` `setNumberOfRetries(int retryNum)` Sets number of retries in case of any database errors.
`void` `setPwd(String pwd)` Sets checkpoint database password.
`void` `setUser(String user)` Sets checkpoint database user name.
`void` `setValueFieldName(String valName)` Sets checkpoint value field name.
`void` `setValueFieldType(String valType)` Sets checkpoint value field type.
`void` `spiStart(String gridName)` This method is called to start SPI.
`void` `spiStop()` This method is called to stop SPI.

### Methods inherited from class org.apache.ignite.spi.IgniteSpiAdapter

`assertParameter, checkConfigurationConsistency0, configInfo, createSpiAttributeName, getConsistentAttributeNames, getExceptionRegistry, getIgniteHome, getLocalNodeId, getName, getNodeAttributes, getSpiContext, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, injectables, onContextDestroyed, onContextInitialized, onContextInitialized0, registerMBean, setName, startInfo, startStopwatch, stopInfo, unregisterMBean`

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

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

### Methods inherited from interface org.apache.ignite.spi.IgniteSpi

`getName, getNodeAttributes, onContextDestroyed, onContextInitialized`

### Methods inherited from interface org.apache.ignite.spi.IgniteSpiManagementMBean

`getIgniteHome, getLocalNodeId, getName, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted`

### Field Detail

#### DFLT_NUMBER_OF_RETRIES

    public static final int DFLT_NUMBER_OF_RETRIES

Default number of retries in case of errors (value is `2`).

See Also:
  Constant Field Values

#### DFLT_EXPIRE_DATE_FIELD_TYPE

    public static final String DFLT_EXPIRE_DATE_FIELD_TYPE

Default expiration date field type (value is `DATETIME`).

See Also:
  Constant Field Values

#### DFLT_EXPIRE_DATE_FIELD_NAME

    public static final String DFLT_EXPIRE_DATE_FIELD_NAME

Default expiration date field name (value is `EXPIRE_DATE`).

See Also:
  Constant Field Values

#### DFLT_VALUE_FIELD_TYPE

    public static final String DFLT_VALUE_FIELD_TYPE

Default checkpoint value field type (value is `BLOB`).

See Also:
  Constant Field Values

#### DFLT_VALUE_FIELD_NAME

    public static final String DFLT_VALUE_FIELD_NAME

Default checkpoint value field name (value is `VALUE`).

See Also:
  Constant Field Values

#### DFLT_KEY_FIELD_TYPE

    public static final String DFLT_KEY_FIELD_TYPE

Default checkpoint key field type (value is `VARCHAR(256)`).

See Also:
  Constant Field Values

#### DFLT_KEY_FIELD_NAME

    public static final String DFLT_KEY_FIELD_NAME

Default checkpoint key field name (value is `NAME`).

See Also:
  Constant Field Values

#### DFLT_CHECKPOINT_TABLE_NAME

    public static final String DFLT_CHECKPOINT_TABLE_NAME

Default checkpoint table name (value is `CHECKPOINTS`).

See Also:
  Constant Field Values

### Constructor Detail

#### JdbcCheckpointSpi

    public JdbcCheckpointSpi()

### Method Detail

#### getNumberOfRetries

    public int getNumberOfRetries()

Gets number of retries in case of DB failure.

Specified by:
  `getNumberOfRetries` in interface `JdbcCheckpointSpiMBean`
Returns:
  Number of retries.

#### getDataSourceInfo

    public String getDataSourceInfo()

Gets data source description.

Specified by:
  `getDataSourceInfo` in interface `JdbcCheckpointSpiMBean`
Returns:
  Description for data source.

#### getUser

    public String getUser()

Gets checkpoint jdbc user name.

Specified by:
  `getUser` in interface `JdbcCheckpointSpiMBean`
Returns:
  User name for checkpoint jdbc.

#### getPwd

    public String getPwd()

Gets checkpoint jdbc password.

Specified by:
  `getPwd` in interface `JdbcCheckpointSpiMBean`
Returns:
  Password for checkpoint jdbc.

#### getCheckpointTableName

    public String getCheckpointTableName()

Gets checkpoint table name.

Specified by:
  `getCheckpointTableName` in interface `JdbcCheckpointSpiMBean`
Returns:
  Checkpoint table name.

#### getKeyFieldName

    public String getKeyFieldName()

Gets key field name for checkpoint table.

Specified by:
  `getKeyFieldName` in interface `JdbcCheckpointSpiMBean`
Returns:
  Key field name for checkpoint table.

#### getKeyFieldType

    public String getKeyFieldType()

Gets key field type for checkpoint table.

Specified by:
  `getKeyFieldType` in interface `JdbcCheckpointSpiMBean`
Returns:
  Key field type for checkpoint table.

#### getValueFieldName

    public String getValueFieldName()

Gets value field name for checkpoint table.

Specified by:
  `getValueFieldName` in interface `JdbcCheckpointSpiMBean`
Returns:
  Value field name for checkpoint table.

#### getValueFieldType

    public String getValueFieldType()

Gets value field type for checkpoint table.

Specified by:
  `getValueFieldType` in interface `JdbcCheckpointSpiMBean`
Returns:
  Value field type for checkpoint table.

#### getExpireDateFieldName

    public String getExpireDateFieldName()

Gets expiration date field name for checkpoint table.

Specified by:
  `getExpireDateFieldName` in interface `JdbcCheckpointSpiMBean`
Returns:
  Create date field name for checkpoint table.

#### getExpireDateFieldType

    public String getExpireDateFieldType()

Gets expiration date field type for checkpoint table.

Specified by:
  `getExpireDateFieldType` in interface `JdbcCheckpointSpiMBean`
Returns:
  Expiration date field type for checkpoint table.

#### setDataSource

    @IgniteSpiConfiguration(optional=false)
    public void setDataSource(DataSource dataSrc)

Sets DataSource to use for database access. This parameter is mandatory and must be provided for this SPI to be able to start.

Apache DBCP project provides various wrappers for data sources and connection pools. You can use these wrappers as Spring beans to configure this SPI from Spring configuration file. Refer to `Apache DBCP` project for more information.

Parameters:
  `dataSrc` - DataSource object to set.

#### setNumberOfRetries

    @IgniteSpiConfiguration(optional=true)
    public void setNumberOfRetries(int retryNum)

Sets number of retries in case of any database errors. By default the value is `DFLT_NUMBER_OF_RETRIES`.

Parameters:
  `retryNum` - Number of retries in case of any database errors.

#### setUser

    @IgniteSpiConfiguration(optional=true)
    public void setUser(String user)

Sets checkpoint database user name. Note that authentication will be performed only if both, `user` and `password` are set.

Parameters:
  `user` - Checkpoint database user name to set.
See Also:
  `setPwd(String)`

#### setPwd

    @IgniteSpiConfiguration(optional=true)
    public void setPwd(String pwd)

Sets checkpoint database password. Note that authentication will be performed only if both, `user` and `password` are set.

Parameters:
  `pwd` - Checkpoint database password to set.
See Also:
  `setUser(String)`

#### setCheckpointTableName

    @IgniteSpiConfiguration(optional=true)
    public void setCheckpointTableName(String tblName)

Sets checkpoint table name. By default `DFLT_CHECKPOINT_TABLE_NAME` is used.

Parameters:
  `tblName` - Checkpoint table name to set.

#### setKeyFieldName

    @IgniteSpiConfiguration(optional=true)
    public void setKeyFieldName(String keyName)

Sets checkpoint key field name. By default, `DFLT_KEY_FIELD_NAME` is used. Note that you may also want to change key field type (see `setKeyFieldType(String)`).

Parameters:
  `keyName` - Checkpoint key field name to set.

#### setKeyFieldType

    @IgniteSpiConfiguration(optional=true)
    public void setKeyFieldType(String keyType)

Sets checkpoint key field type. The field should have corresponding SQL string type (`VARCHAR`, for example). By default `DFLT_EXPIRE_DATE_FIELD_TYPE` is used.

Parameters:
  `keyType` - Checkpoint key field type to set.

#### setValueFieldName

    @IgniteSpiConfiguration(optional=true)
    public void setValueFieldName(String valName)

Sets checkpoint value field name. By default `DFLT_VALUE_FIELD_NAME` is used. Note that you may also want to change the value type (see `setValueFieldType(String)`).

Parameters:
  `valName` - Checkpoint value field name to set.

#### setValueFieldType

    @IgniteSpiConfiguration(optional=true)
    public void setValueFieldType(String valType)

Sets checkpoint value field type. Note, that the field should have corresponding SQL `BLOB` type, and the default value of `DFLT_VALUE_FIELD_TYPE`, which is `BLOB`, won't work for all databases. For example, if using `HSQL DB`, then the type should be `longvarbinary`.

Parameters:
  `valType` - Checkpoint value field type to set.

#### setExpireDateFieldName

    @IgniteSpiConfiguration(optional=true)
    public void setExpireDateFieldName(String expDateName)

Sets checkpoint expiration date field name. By default `DFLT_EXPIRE_DATE_FIELD_NAME` is used. Note that you may also want to change the expiration date field type (see `setExpireDateFieldType(String)`).

Parameters:
  `expDateName` - Checkpoint expiration date field name to set.

#### setExpireDateFieldType

    @IgniteSpiConfiguration(optional=true)
    public void setExpireDateFieldType(String expDateType)

Sets checkpoint expiration date field type. By default `DFLT_EXPIRE_DATE_FIELD_TYPE` is used. The field should have corresponding SQL `DATETIME` type.

Parameters:
  `expDateType` - Checkpoint expiration date field type to set.

#### spiStart

    public void spiStart(String gridName)
                  throws IgniteSpiException

This method is called to start SPI. After this method returns successfully kernel assumes that SPI is fully operational.

Specified by:
  `spiStart` in interface `IgniteSpi`
Parameters:
  `gridName` - Name of grid instance this SPI is being started for (`null` for default grid).
Throws:
  `IgniteSpiException` - Throws in case of any error during SPI start.

#### spiStop

    public void spiStop()
                 throws IgniteSpiException

This method is called to stop SPI. After this method returns kernel assumes that this SPI is finished and all resources acquired by it are released.

Note that this method can be called at any point including during recovery of failed start. It should make no assumptions on what state SPI will be in when this method is called.

Specified by:
  `spiStop` in interface `IgniteSpi`
Throws:
  `IgniteSpiException` - Thrown in case of any error during SPI stop.

#### onContextDestroyed0

    protected void onContextDestroyed0()

Method to be called in the beginning of onContextDestroyed() method.

Overrides:
  `onContextDestroyed0` in class `IgniteSpiAdapter`

#### loadCheckpoint

    public byte[] loadCheckpoint(String key)
                          throws IgniteSpiException

Loads checkpoint from storage by its unique key.

Specified by:
  `loadCheckpoint` in interface `CheckpointSpi`
Parameters:
  `key` - Checkpoint key.
Returns:
  Loaded data or `null` if there is no data for a given key.
Throws:
  `IgniteSpiException` - Thrown in case of any error while loading checkpoint data. Note that in case when given `key` is not found this method will return `null`.

#### removeCheckpoint

    public boolean removeCheckpoint(String key)

This method instructs the checkpoint provider to clean saved data for a given `key`.

Specified by:
  `removeCheckpoint` in interface `CheckpointSpi`
Parameters:
  `key` - Key for the checkpoint to remove.
Returns:
  `true` if data has been actually removed, `false` otherwise.

#### saveCheckpoint

    public boolean saveCheckpoint(String key,
                         byte[] state,
                         long timeout,
                         boolean overwrite)
                           throws IgniteSpiException

Saves checkpoint to the storage.

Specified by:
  `saveCheckpoint` in interface `CheckpointSpi`
Parameters:
  `key` - Checkpoint unique key.
  `state` - Saved data.
  `timeout` - Every intermediate data stored by checkpoint provider should have a timeout. Timeout allows for effective resource management by checkpoint provider by cleaning saved data that are not needed anymore. Generally, the user should choose the minimum possible timeout to avoid long-term resource acquisition by checkpoint provider. Value `0` means that timeout will never expire.
  `overwrite` - Whether or not overwrite checkpoint if it already exists.
Returns:
  `true` if checkpoint has been actually saved, `false` otherwise.
Throws:
  `IgniteSpiException` - Thrown in case of any error while saving checkpoint data.

#### setCheckpointListener

    public void setCheckpointListener(CheckpointListener lsnr)

Sets the checkpoint listener.

Specified by:
  `setCheckpointListener` in interface `CheckpointSpi`
Parameters:
  `lsnr` - The listener to set or `null`.

2015 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.0.0-RC3
Release Date | : March 24 2015