Top Description Fields Constructors Methods
org.springframework.jdbc.object

public abstract Class RdbmsOperation

extends Object
implements InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean
Known Direct Subclasses
org.springframework.jdbc.object.SqlCall, org.springframework.jdbc.object.SqlOperation
Imports
java.sql.ResultSet, .Types, java.util.ArrayList, .Arrays, .Collections, .List, .Map, javax.sql.DataSource, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.InitializingBean, org.springframework.dao.InvalidDataAccessApiUsageException, org.springframework.jdbc.core.JdbcTemplate, .SqlParameter, org.springframework.lang.Nullable, org.springframework.util.Assert

An "RDBMS operation" is a multithreaded, reusable object representing a query, update, or stored procedure call. An RDBMS operation is not a command, as a command is not reusable. However, execute methods may take commands as arguments. Subclasses should be JavaBeans, allowing easy configuration.

This class and subclasses throw runtime exceptions, defined in the org.springframework.dao package (and as thrown by the org.springframework.jdbc.core package, which the classes in this package use under the hood to perform raw JDBC operations).

Subclasses should set SQL and add parameters before invoking the compile() method. The order in which parameters are added is significant. The appropriate execute or update method can then be invoked.

Authors
Rod Johnson, Juergen Hoeller
See Also
SqlQuery, SqlUpdate, StoredProcedure, org.springframework.jdbc.core.JdbcTemplate

Field Summary

Modifier and TypeField and Description
private volatile boolean
compiled

Has this operation been compiled?

private final List<SqlParameter>
private String[]
private JdbcTemplate
jdbcTemplate

Lower-level class used to execute SQL.

protected final Log
logger

Logger available to subclasses.

private int
private boolean
private String
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Ensures compilation if used in a bean factory.

protected boolean
allowsUnusedParameters()

Return whether this operation accepts additional parameters that are given but not actually used.

protected void
checkCompiled()

Check whether this operation has been compiled already; lazily compile it if not already compiled.

public final void
compile()

Compile this query.

protected abstract void
compileInternal()

Subclasses must implement this template method to perform their own compilation.

public void
declareParameter(SqlParameter
the SqlParameter to add. This will specify SQL type and (optionally) the parameter's name. Note that you typically use the SqlParameter class itself here, not any of its subclasses.
param
)

Declare a parameter for this operation.

protected List<SqlParameter>
getDeclaredParameters()

Return a list of the declared SqlParameter objects.

public String[]
getGeneratedKeysColumnNames()

Return the column names of the auto generated keys.

public JdbcTemplate
getJdbcTemplate()

Return the JdbcTemplate used by this operation object.

public int
getResultSetType()

Return whether statements will return a specific type of ResultSet.

public String
getSql()

Subclasses can override this to supply dynamic SQL if they wish, but SQL is normally set by calling the setSql method or in a subclass constructor.

public boolean

Returns:

whether this operation is compiled and ready to use
isCompiled
()

Is this operation "compiled"?

public boolean
isReturnGeneratedKeys()

Return whether statements should be capable of returning auto-generated keys.

public boolean
isUpdatableResults()

Return whether statements will return updatable ResultSets.

protected String

Returns:

the SQL (never null)
resolveSql
()

Resolve the configured SQL for actual use.

public void
setDataSource(DataSource dataSource)

Set the JDBC DataSource to obtain connections from.

public void
setFetchSize(int fetchSize)

Set the fetch size for this RDBMS operation.

public void
setGeneratedKeysColumnNames(String... names)

Set the column names of the auto-generated keys.

public void
setJdbcTemplate(JdbcTemplate jdbcTemplate)

An alternative to the more commonly used setDataSource when you want to use the same JdbcTemplate in multiple RdbmsOperations.

public void
setMaxRows(int maxRows)

Set the maximum number of rows for this RDBMS operation.

public void
setParameters(SqlParameter...
an array containing the declared SqlParameter objects
parameters
)

Add one or more declared parameters.

public void
setQueryTimeout(int queryTimeout)

Set the query timeout for statements that this RDBMS operation executes.

public void
setResultSetType(int
the ResultSet type
resultSetType
)

Set whether to use statements that return a specific type of ResultSet.

public void
setReturnGeneratedKeys(boolean returnGeneratedKeys)

Set whether prepared statements should be capable of returning auto-generated keys.

public void
setSql(String sql)

Set the SQL executed by this operation.

public void
setTypes(int[]
array of SQL types as defined in the java.sql.Types class
types
)

Add anonymous parameters, specifying only their SQL types as defined in the java.sql.Types class.

public void
setUpdatableResults(boolean updatableResults)

Set whether to use statements that are capable of returning updatable ResultSets.

protected boolean
supportsLobParameters()

Return whether BLOB/CLOB parameters are supported for this kind of operation.

protected void
validateNamedParameters(Map<String, ?>
parameter Map supplied (may be null)
parameters
)

Validate the named parameters passed to an execute method based on declared parameters.

private void
validateParameterCount(int
the number of actual parameters given
suppliedParamCount
,
int
the number of input parameters declared
declaredInParamCount
)

Validate the given parameter count against the given declared parameters.

protected void
validateParameters(Object[]
the parameters supplied (may be null)
parameters
)

Validate the parameters passed to an execute method based on declared parameters.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

compiledback to summary
private volatile boolean compiled

Has this operation been compiled? Compilation means at least checking that a DataSource and sql have been provided, but subclasses may also implement their own custom validation.

declaredParametersback to summary
private final List<SqlParameter> declaredParameters
generatedKeysColumnNamesback to summary
private String[] generatedKeysColumnNames
Annotations
@Nullable
jdbcTemplateback to summary
private JdbcTemplate jdbcTemplate

Lower-level class used to execute SQL.

loggerback to summary
protected final Log logger

Logger available to subclasses.

resultSetTypeback to summary
private int resultSetType
returnGeneratedKeysback to summary
private boolean returnGeneratedKeys
sqlback to summary
private String sql
Annotations
@Nullable
updatableResultsback to summary
private boolean updatableResults

Constructor Detail

RdbmsOperationback to summary
public RdbmsOperation()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Ensures compilation if used in a bean factory.

Annotations
@Override
allowsUnusedParametersback to summary
protected boolean allowsUnusedParameters()

Return whether this operation accepts additional parameters that are given but not actually used. Applies in particular to parameter Maps.

The default is false.

See Also
StoredProcedure
checkCompiledback to summary
protected void checkCompiled()

Check whether this operation has been compiled already; lazily compile it if not already compiled.

Automatically called by validateParameters.

See Also
validateParameters
compileback to summary
public final void compile() throws InvalidDataAccessApiUsageException

Compile this query. Ignores subsequent attempts to compile.

Exceptions
InvalidDataAccessApiUsageException:
if the object hasn't been correctly initialized, for example if no DataSource has been provided
compileInternalback to summary
protected abstract void compileInternal() throws InvalidDataAccessApiUsageException

Subclasses must implement this template method to perform their own compilation. Invoked after this base class's compilation is complete.

Subclasses can assume that SQL and a DataSource have been supplied.

Exceptions
InvalidDataAccessApiUsageException:
if the subclass hasn't been properly configured
declareParameterback to summary
public void declareParameter(SqlParameter param) throws InvalidDataAccessApiUsageException

Declare a parameter for this operation.

The order in which this method is called is significant when using positional parameters. It is not significant when using named parameters with named SqlParameter objects here; it remains significant when using named parameters in combination with unnamed SqlParameter objects here.

Parameters
param:SqlParameter

the SqlParameter to add. This will specify SQL type and (optionally) the parameter's name. Note that you typically use the SqlParameter class itself here, not any of its subclasses.

Exceptions
InvalidDataAccessApiUsageException:
if the operation is already compiled, and hence cannot be configured further
getDeclaredParametersback to summary
protected List<SqlParameter> getDeclaredParameters()

Return a list of the declared SqlParameter objects.

getGeneratedKeysColumnNamesback to summary
public String[] getGeneratedKeysColumnNames()

Return the column names of the auto generated keys.

Annotations
@Nullable
getJdbcTemplateback to summary
public JdbcTemplate getJdbcTemplate()

Return the JdbcTemplate used by this operation object.

getResultSetTypeback to summary
public int getResultSetType()

Return whether statements will return a specific type of ResultSet.

getSqlback to summary
public String getSql()

Subclasses can override this to supply dynamic SQL if they wish, but SQL is normally set by calling the setSql method or in a subclass constructor.

Annotations
@Nullable
isCompiledback to summary
public boolean isCompiled()

Is this operation "compiled"? Compilation, as in JDO, means that the operation is fully configured, and ready to use. The exact meaning of compilation will vary between subclasses.

Returns:boolean

whether this operation is compiled and ready to use

isReturnGeneratedKeysback to summary
public boolean isReturnGeneratedKeys()

Return whether statements should be capable of returning auto-generated keys.

isUpdatableResultsback to summary
public boolean isUpdatableResults()

Return whether statements will return updatable ResultSets.

resolveSqlback to summary
protected String resolveSql()

Resolve the configured SQL for actual use.

Returns:String

the SQL (never null)

Since
5.0
setDataSourceback to summary
public void setDataSource(DataSource dataSource)

Set the JDBC DataSource to obtain connections from.

See Also
org.springframework.jdbc.core.JdbcTemplate#setDataSource
setFetchSizeback to summary
public void setFetchSize(int fetchSize)

Set the fetch size for this RDBMS operation. This is important for processing large result sets: Setting this higher than the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid transferring row data that will never be read by the application.

Default is -1, indicating to use the driver's default.

See Also
org.springframework.jdbc.core.JdbcTemplate#setFetchSize
setGeneratedKeysColumnNamesback to summary
public void setGeneratedKeysColumnNames(String... names)

Set the column names of the auto-generated keys.

See Also
java.sql.Connection#prepareStatement(String, String[])
setJdbcTemplateback to summary
public void setJdbcTemplate(JdbcTemplate jdbcTemplate)

An alternative to the more commonly used setDataSource when you want to use the same JdbcTemplate in multiple RdbmsOperations. This is appropriate if the JdbcTemplate has special configuration such as a org.springframework.jdbc.support.SQLExceptionTranslator to be reused.

setMaxRowsback to summary
public void setMaxRows(int maxRows)

Set the maximum number of rows for this RDBMS operation. This is important for processing subsets of large result sets, avoiding to read and hold the entire result set in the database or in the JDBC driver.

Default is -1, indicating to use the driver's default.

See Also
org.springframework.jdbc.core.JdbcTemplate#setMaxRows
setParametersback to summary
public void setParameters(SqlParameter... parameters)

Add one or more declared parameters. Used for configuring this operation when used in a bean factory. Each parameter will specify SQL type and (optionally) the parameter's name.

Parameters
parameters:SqlParameter[]

an array containing the declared SqlParameter objects

See Also
declaredParameters
setQueryTimeoutback to summary
public void setQueryTimeout(int queryTimeout)

Set the query timeout for statements that this RDBMS operation executes.

Default is -1, indicating to use the JDBC driver's default.

Note

Any timeout specified here will be overridden by the remaining transaction timeout when executing within a transaction that has a timeout specified at the transaction level.

setResultSetTypeback to summary
public void setResultSetType(int resultSetType)

Set whether to use statements that return a specific type of ResultSet.

Parameters
resultSetType:int

the ResultSet type

See Also
java.sql.ResultSet#TYPE_FORWARD_ONLY, java.sql.ResultSet#TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet#TYPE_SCROLL_SENSITIVE, java.sql.Connection#prepareStatement(String, int, int)
setReturnGeneratedKeysback to summary
public void setReturnGeneratedKeys(boolean returnGeneratedKeys)

Set whether prepared statements should be capable of returning auto-generated keys.

See Also
java.sql.Connection#prepareStatement(String, int)
setSqlback to summary
public void setSql(String sql)

Set the SQL executed by this operation.

setTypesback to summary
public void setTypes(int[] types) throws InvalidDataAccessApiUsageException

Add anonymous parameters, specifying only their SQL types as defined in the java.sql.Types class.

Parameter ordering is significant. This method is an alternative to the declareParameter method, which should normally be preferred.

Parameters
types:int[]

array of SQL types as defined in the java.sql.Types class

Exceptions
InvalidDataAccessApiUsageException:
if the operation is already compiled
setUpdatableResultsback to summary
public void setUpdatableResults(boolean updatableResults)

Set whether to use statements that are capable of returning updatable ResultSets.

See Also
java.sql.Connection#prepareStatement(String, int, int)
supportsLobParametersback to summary
protected boolean supportsLobParameters()

Return whether BLOB/CLOB parameters are supported for this kind of operation.

The default is true.

validateNamedParametersback to summary
protected void validateNamedParameters(Map<String, ?> parameters) throws InvalidDataAccessApiUsageException

Validate the named parameters passed to an execute method based on declared parameters. Subclasses should invoke this method before every executeQuery() or update() method.

Parameters
parameters:Map<String, ?>

parameter Map supplied (may be null)

Exceptions
InvalidDataAccessApiUsageException:
if the parameters are invalid
validateParameterCountback to summary
private void validateParameterCount(int suppliedParamCount, int declaredInParamCount)

Validate the given parameter count against the given declared parameters.

Parameters
suppliedParamCount:int

the number of actual parameters given

declaredInParamCount:int

the number of input parameters declared

validateParametersback to summary
protected void validateParameters(Object[] parameters) throws InvalidDataAccessApiUsageException

Validate the parameters passed to an execute method based on declared parameters. Subclasses should invoke this method before every executeQuery() or update() method.

Parameters
parameters:Object[]

the parameters supplied (may be null)

Exceptions
InvalidDataAccessApiUsageException:
if the parameters are invalid