Class JdbcBulkLoadingContextSupport<T>
- Type Parameters:
T- the entity type
- All Implemented Interfaces:
AutoCloseable,BulkLoadingDao.LoadingContext<T>,org.springframework.jdbc.core.SqlProvider
BulkLoadingDao.LoadingContext for JDBC stored
procedure based implementations.
This class handles the low-level transaction semantics for the load
operation, and relies on extending classes to implement the
doLoad(Object, PreparedStatement, long) method to save each entity
into the backend database.
- Version:
- 1.0
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default batch size.protected final org.slf4j.LoggerA class-level logger. -
Constructor Summary
ConstructorsConstructorDescriptionJdbcBulkLoadingContextSupport(org.springframework.transaction.PlatformTransactionManager txManager, DataSource dataSource, String sql, BulkLoadingDao.LoadingOptions options, BulkLoadingDao.LoadingExceptionHandler<T> exceptionHandler) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close any temporary resources.voidcommit()Commit the current transaction.voidCreate a checkpoint that can be rolled back to.protected PreparedStatementCreate the JDBC statement to use.protected abstract booleandoLoad(T entity, PreparedStatement stmt, long index) Load a single entity.longGet the count of entities committed thus far using this context.protected ConnectionGet the JDBC connection.Get the JDBC data source.Get the exception handler.Get the entity that was last passed to theBulkLoadingDao.LoadingContext.load(Object)method.longGet the count of entities loaded thus far using this context.Get the loading options used to create the context.getSql()Get the JDBC statement to use for bulk loading.org.springframework.transaction.PlatformTransactionManagerGet the transaction manager.final voidLoad an entity.voidrollback()Discard the entities loaded within the current transaction.
-
Field Details
-
DEFAULT_BATCH_SIZE
public static final int DEFAULT_BATCH_SIZEThe default batch size.- See Also:
-
log
protected final org.slf4j.Logger logA class-level logger.
-
-
Constructor Details
-
JdbcBulkLoadingContextSupport
public JdbcBulkLoadingContextSupport(org.springframework.transaction.PlatformTransactionManager txManager, DataSource dataSource, String sql, BulkLoadingDao.LoadingOptions options, BulkLoadingDao.LoadingExceptionHandler<T> exceptionHandler) Constructor.- Parameters:
txManager- the transaction managerdataSource- the data sourcesql- the SQL statement (or JDBC SQL call) to executeoptions- the optionsexceptionHandler- an exception handler
-
-
Method Details
-
getOptions
Description copied from interface:BulkLoadingDao.LoadingContextGet the loading options used to create the context.- Specified by:
getOptionsin interfaceBulkLoadingDao.LoadingContext<T>- Returns:
- the loading options
-
getLoadedCount
public long getLoadedCount()Description copied from interface:BulkLoadingDao.LoadingContextGet the count of entities loaded thus far using this context.If
BulkLoadingDao.LoadingContext.rollback()is called, this value will reset back to the count of currently committed entities.- Specified by:
getLoadedCountin interfaceBulkLoadingDao.LoadingContext<T>- Returns:
- the loaded count
-
getCommittedCount
public long getCommittedCount()Description copied from interface:BulkLoadingDao.LoadingContextGet the count of entities committed thus far using this context.How this value increments depends on the
BulkLoadingDao.LoadingTransactionModedefined in the options that were used to create this context:BulkLoadingDao.LoadingTransactionMode.NoTransaction- This count will match
BulkLoadingDao.LoadingContext.getLoadedCount()and increment as each entity is loaded. BulkLoadingDao.LoadingTransactionMode.BatchTransactions- This count will increment only after each batch of loaded entity
have been committed, and thus can lag behind
BulkLoadingDao.LoadingContext.getLoadedCount(). BulkLoadingDao.LoadingTransactionMode.TransactionCheckpoints- This count will only increment after calls to
BulkLoadingDao.LoadingContext.createCheckpoint()are made. IfBulkLoadingDao.LoadingContext.rollback()is called, this count will reset back to the count at the previous timeBulkLoadingDao.LoadingContext.createCheckpoint()was called. BulkLoadingDao.LoadingTransactionMode.SingleTransaction- This count will remain at 0 until
BulkLoadingDao.LoadingContext.commit()is called, at which point it will matchBulkLoadingDao.LoadingContext.getLoadedCount().
- Specified by:
getCommittedCountin interfaceBulkLoadingDao.LoadingContext<T>- Returns:
- the committed entity count
-
getConnection
Get the JDBC connection.- Returns:
- the connection
- Throws:
SQLException- if any SQL error occurs
-
getLastLoadedEntity
Description copied from interface:BulkLoadingDao.LoadingContextGet the entity that was last passed to theBulkLoadingDao.LoadingContext.load(Object)method.- Specified by:
getLastLoadedEntityin interfaceBulkLoadingDao.LoadingContext<T>- Returns:
- the last loaded entity
-
load
Description copied from interface:BulkLoadingDao.LoadingContextLoad an entity.- Specified by:
loadin interfaceBulkLoadingDao.LoadingContext<T>- Parameters:
entity- the entity to load
-
doLoad
Load a single entity.Extending classes must implement this method to perform the actual saving of the entity to JDBC.
- Parameters:
entity- the entity to loadstmt- the statement to useindex- the loading index- Returns:
- true if loaded
- Throws:
SQLException- if any SQL error occurs
-
createCheckpoint
public void createCheckpoint()Description copied from interface:BulkLoadingDao.LoadingContextCreate a checkpoint that can be rolled back to.The
BulkLoadingDao.LoadingTransactionMode.TransactionCheckpointsmode must have been set in the options used to create this context.- Specified by:
createCheckpointin interfaceBulkLoadingDao.LoadingContext<T>
-
commit
public void commit()Description copied from interface:BulkLoadingDao.LoadingContextCommit the current transaction.The nature of the current transaction depends on the transaction mode set in the options used to create this context:
TransactionCheckpointsorSingleTransaction- All entities loaded via
BulkLoadingDao.LoadingContext.load(Object)are committed. BatchTransactions- The entities loaded via
BulkLoadingDao.LoadingContext.load(Object)since the last automatic batch commit are committed.
- Specified by:
commitin interfaceBulkLoadingDao.LoadingContext<T>
-
rollback
public void rollback()Description copied from interface:BulkLoadingDao.LoadingContextDiscard the entities loaded within the current transaction.The nature of the current transaction depends on the transaction mode set in the options used to create this context:
SingleTransaction- All entities loaded are discarded.
TransactionCheckpoints- All entities loaded via
BulkLoadingDao.LoadingContext.load(Object)since the last call toBulkLoadingDao.LoadingContext.createCheckpoint()are discarded. BatchTransactions- The entities loaded via
BulkLoadingDao.LoadingContext.load(Object)since the last automatic batch commit are discarded.
- Specified by:
rollbackin interfaceBulkLoadingDao.LoadingContext<T>
-
close
public void close()Description copied from interface:BulkLoadingDao.LoadingContextClose any temporary resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBulkLoadingDao.LoadingContext<T>
-
createJdbcStatement
Create the JDBC statement to use.This implementation invokes
Connection.prepareCall(String), passinggetSql(). Extending classes can override to customize this behavior.- Parameters:
con- the JDBC connection- Returns:
- the statement
- Throws:
SQLException- if any SQL error occurs
-
getTransactionManager
public org.springframework.transaction.PlatformTransactionManager getTransactionManager()Get the transaction manager.- Returns:
- the manager
-
getDataSource
Get the JDBC data source.- Returns:
- the dataSource
-
getSql
Get the JDBC statement to use for bulk loading.- Specified by:
getSqlin interfaceorg.springframework.jdbc.core.SqlProvider
-
getExceptionHandler
Get the exception handler.- Returns:
- the exceptionHandler
-