Top Description Inners Fields Constructors Methods
org.springframework.jdbc.support.lob

public Class DefaultLobHandler

extends AbstractLobHandler
Class Inheritance
Imports
java.io.ByteArrayInputStream, .InputStream, .InputStreamReader, .Reader, .StringReader, java.nio.charset.StandardCharsets, java.sql.Blob, .Clob, .PreparedStatement, .ResultSet, .SQLException, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable

Default implementation of the LobHandler interface. Invokes the direct accessor methods that java.sql.ResultSet and java.sql.PreparedStatement offer.

By default, incoming streams are going to be passed to the appropriate setBinary/Ascii/CharacterStream method on the JDBC driver's PreparedStatement. If the specified content length is negative, this handler will use the JDBC 4.0 variants of the set-stream methods without a length parameter; otherwise, it will pass the specified length on to the driver.

This LobHandler should work for any JDBC driver that is JDBC compliant in terms of the spec's suggestions regarding simple BLOB and CLOB handling. This does not apply to Oracle 9i's drivers at all; as of Oracle 10g, it does work but may still come with LOB size limitations. Consider using recent Oracle drivers even when working against an older database server. See the LobHandler javadoc for the full set of recommendations.

Some JDBC drivers require values with a BLOB/CLOB target column to be explicitly set through the JDBC setBlob / setClob API: for example, PostgreSQL's driver. Switch the "wrapAsLob" property to "true" when operating against such a driver.

On JDBC 4.0, this LobHandler also supports streaming the BLOB/CLOB content via the setBlob / setClob variants that take a stream argument directly. Consider switching the "streamAsLob" property to "true" when operating against a fully compliant JDBC 4.0 driver.

Finally, this LobHandler also supports the creation of temporary BLOB/CLOB objects. Consider switching the "createTemporaryLob" property to "true" when "streamAsLob" happens to run into LOB size limitations.

See the LobHandler interface javadoc for a summary of recommendations.

Author
Juergen Hoeller
Since
04.12.2003
See Also
java.sql.ResultSet#getBytes, java.sql.ResultSet#getBinaryStream, java.sql.ResultSet#getString, java.sql.ResultSet#getAsciiStream, java.sql.ResultSet#getCharacterStream, java.sql.PreparedStatement#setBytes, java.sql.PreparedStatement#setBinaryStream, java.sql.PreparedStatement#setString, java.sql.PreparedStatement#setAsciiStream, java.sql.PreparedStatement#setCharacterStream

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
DefaultLobHandler.DefaultLobCreator

Default LobCreator implementation as an inner class.

Field Summary

Modifier and TypeField and Description
private boolean
protected final Log
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public InputStream
getBlobAsBinaryStream(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Implements org.springframework.jdbc.support.lob.LobHandler.getBlobAsBinaryStream.

Retrieve the given column as binary stream from the given ResultSet.

public byte[]
getBlobAsBytes(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Implements org.springframework.jdbc.support.lob.LobHandler.getBlobAsBytes.

Retrieve the given column as bytes from the given ResultSet.

public InputStream
getClobAsAsciiStream(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Implements org.springframework.jdbc.support.lob.LobHandler.getClobAsAsciiStream.

Retrieve the given column as ASCII stream from the given ResultSet.

public Reader
getClobAsCharacterStream(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Implements org.springframework.jdbc.support.lob.LobHandler.getClobAsCharacterStream.

Retrieve the given column as character stream from the given ResultSet.

public String
getClobAsString(ResultSet
the ResultSet to retrieve the content from
rs
,
int
the column index to use
columnIndex
)

Implements org.springframework.jdbc.support.lob.LobHandler.getClobAsString.

Retrieve the given column as String from the given ResultSet.

public LobCreator
getLobCreator()

Implements org.springframework.jdbc.support.lob.LobHandler.getLobCreator.

Create a new LobCreator instance, i.e. a session for creating BLOBs and CLOBs.

public void
setCreateTemporaryLob(boolean createTemporaryLob)

Specify whether to copy a byte array / String into a temporary JDBC Blob / Clob object created through the JDBC 4.0 createBlob / createClob methods.

public void
setStreamAsLob(boolean streamAsLob)

Specify whether to submit a binary stream / character stream to the JDBC driver as explicit LOB content, using the JDBC 4.0 setBlob / setClob method with a stream argument.

public void
setWrapAsLob(boolean wrapAsLob)

Specify whether to submit a byte array / String to the JDBC driver wrapped in a JDBC Blob / Clob object, using the JDBC setBlob / setClob method with a Blob / Clob argument.

Inherited from org.springframework.jdbc.support.lob.AbstractLobHandler:
getBlobAsBinaryStreamgetBlobAsBytesgetClobAsAsciiStreamgetClobAsCharacterStreamgetClobAsString

Field Detail

createTemporaryLobback to summary
private boolean createTemporaryLob
loggerback to summary
protected final Log logger
streamAsLobback to summary
private boolean streamAsLob
wrapAsLobback to summary
private boolean wrapAsLob

Constructor Detail

DefaultLobHandlerback to summary
public DefaultLobHandler()

Method Detail

getBlobAsBinaryStreamback to summary
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException

Implements org.springframework.jdbc.support.lob.LobHandler.getBlobAsBinaryStream.

Doc from org.springframework.jdbc.support.lob.LobHandler.getBlobAsBinaryStream.

Retrieve the given column as binary stream from the given ResultSet. Might simply invoke ResultSet.getBinaryStream or work with ResultSet.getBlob, depending on the database and driver.

Parameters
rs:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:InputStream

the content as binary stream, or null in case of SQL NULL

Annotations
@Override
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
getBlobAsBytesback to summary
public byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException

Implements org.springframework.jdbc.support.lob.LobHandler.getBlobAsBytes.

Doc from org.springframework.jdbc.support.lob.LobHandler.getBlobAsBytes.

Retrieve the given column as bytes from the given ResultSet. Might simply invoke ResultSet.getBytes or work with ResultSet.getBlob, depending on the database and driver.

Parameters
rs:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:byte[]

the content as byte array, or null in case of SQL NULL

Annotations
@Override
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
getClobAsAsciiStreamback to summary
public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException

Implements org.springframework.jdbc.support.lob.LobHandler.getClobAsAsciiStream.

Doc from org.springframework.jdbc.support.lob.LobHandler.getClobAsAsciiStream.

Retrieve the given column as ASCII stream from the given ResultSet. Might simply invoke ResultSet.getAsciiStream or work with ResultSet.getClob, depending on the database and driver.

Parameters
rs:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:InputStream

the content as ASCII stream, or null in case of SQL NULL

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods
getClobAsCharacterStreamback to summary
public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException

Implements org.springframework.jdbc.support.lob.LobHandler.getClobAsCharacterStream.

Doc from org.springframework.jdbc.support.lob.LobHandler.getClobAsCharacterStream.

Retrieve the given column as character stream from the given ResultSet. Might simply invoke ResultSet.getCharacterStream or work with ResultSet.getClob, depending on the database and driver.

Parameters
rs:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:Reader

the content as character stream

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods
getClobAsStringback to summary
public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException

Implements org.springframework.jdbc.support.lob.LobHandler.getClobAsString.

Doc from org.springframework.jdbc.support.lob.LobHandler.getClobAsString.

Retrieve the given column as String from the given ResultSet. Might simply invoke ResultSet.getString or work with ResultSet.getClob, depending on the database and driver.

Parameters
rs:ResultSet

the ResultSet to retrieve the content from

columnIndex:int

the column index to use

Returns:String

the content as String, or null in case of SQL NULL

Annotations
@Override
@Nullable
Exceptions
SQLException:
if thrown by JDBC methods
getLobCreatorback to summary
public LobCreator getLobCreator()

Implements org.springframework.jdbc.support.lob.LobHandler.getLobCreator.

Doc from org.springframework.jdbc.support.lob.LobHandler.getLobCreator.

Create a new LobCreator instance, i.e. a session for creating BLOBs and CLOBs. Needs to be closed after the created LOBs are not needed anymore - typically after statement execution or transaction completion.

Returns:LobCreator

the new LobCreator instance

Annotations
@Override
setCreateTemporaryLobback to summary
public void setCreateTemporaryLob(boolean createTemporaryLob)

Specify whether to copy a byte array / String into a temporary JDBC Blob / Clob object created through the JDBC 4.0 createBlob / createClob methods.

Default is "false", using the common JDBC 2.0 setBinaryStream / setCharacterStream method for setting the content. Switch this to "true" for explicit Blob / Clob creation using JDBC 4.0.

This setting affects stream arguments as well as byte array / String arguments, requiring JDBC 4.0 support. For supporting LOB content against JDBC 3.0, check out the "wrapAsLob" setting.

See Also
java.sql.Connection#createBlob(), java.sql.Connection#createClob()
setStreamAsLobback to summary
public void setStreamAsLob(boolean streamAsLob)

Specify whether to submit a binary stream / character stream to the JDBC driver as explicit LOB content, using the JDBC 4.0 setBlob / setClob method with a stream argument.

Default is "false", using the common JDBC 2.0 setBinaryStream / setCharacterStream method for setting the content. Switch this to "true" for explicit JDBC 4.0 streaming, provided that your JDBC driver actually supports those JDBC 4.0 operations (e.g. Derby's).

This setting affects stream arguments as well as byte array / String arguments, requiring JDBC 4.0 support. For supporting LOB content against JDBC 3.0, check out the "wrapAsLob" setting.

See Also
java.sql.PreparedStatement#setBlob(int, java.io.InputStream, long), java.sql.PreparedStatement#setClob(int, java.io.Reader, long)
setWrapAsLobback to summary
public void setWrapAsLob(boolean wrapAsLob)

Specify whether to submit a byte array / String to the JDBC driver wrapped in a JDBC Blob / Clob object, using the JDBC setBlob / setClob method with a Blob / Clob argument.

Default is "false", using the common JDBC 2.0 setBinaryStream / setCharacterStream method for setting the content. Switch this to "true" for explicit Blob / Clob wrapping against JDBC drivers that are known to require such wrapping (e.g. PostgreSQL's for access to OID columns, whereas BYTEA columns need to be accessed the standard way).

This setting affects byte array / String arguments as well as stream arguments, unless "streamAsLob" overrides this handling to use JDBC 4.0's new explicit streaming support (if available).

See Also
java.sql.PreparedStatement#setBlob(int, java.sql.Blob), java.sql.PreparedStatement#setClob(int, java.sql.Clob)
org.springframework.jdbc.support.lob back to summary

protected Class DefaultLobHandler.DefaultLobCreator

extends Object
implements LobCreator
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.support.lob.DefaultLobHandler.DefaultLobCreator
All Implemented Interfaces
org.springframework.jdbc.support.lob.LobCreator, java.io.Closeable, java.lang.AutoCloseable

Default LobCreator implementation as an inner class. Can be subclassed in DefaultLobHandler extensions.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements org.springframework.jdbc.support.lob.LobCreator.close.

Close this LobCreator session and free its temporarily created BLOBs and CLOBs.

public void
setBlobAsBinaryStream(PreparedStatement
the PreparedStatement to the set the content on
ps
,
int
the parameter index to use
paramIndex
,
InputStream
the content as binary stream, or null for SQL NULL
binaryStream
,
int contentLength)

Implements org.springframework.jdbc.support.lob.LobCreator.setBlobAsBinaryStream.

Set the given content as binary stream on the given statement, using the given parameter index.

public void
setBlobAsBytes(PreparedStatement
the PreparedStatement to the set the content on
ps
,
int
the parameter index to use
paramIndex
,
byte[]
the content as byte array, or null for SQL NULL
content
)

Implements org.springframework.jdbc.support.lob.LobCreator.setBlobAsBytes.

Set the given content as bytes on the given statement, using the given parameter index.

public void
setClobAsAsciiStream(PreparedStatement
the PreparedStatement to the set the content on
ps
,
int
the parameter index to use
paramIndex
,
InputStream
the content as ASCII stream, or null for SQL NULL
asciiStream
,
int contentLength)

Implements org.springframework.jdbc.support.lob.LobCreator.setClobAsAsciiStream.

Set the given content as ASCII stream on the given statement, using the given parameter index.

public void
setClobAsCharacterStream(PreparedStatement
the PreparedStatement to the set the content on
ps
,
int
the parameter index to use
paramIndex
,
Reader
the content as character stream, or null for SQL NULL
characterStream
,
int contentLength)

Implements org.springframework.jdbc.support.lob.LobCreator.setClobAsCharacterStream.

Set the given content as character stream on the given statement, using the given parameter index.

public void
setClobAsString(PreparedStatement
the PreparedStatement to the set the content on
ps
,
int
the parameter index to use
paramIndex
,
String
the content as String, or null for SQL NULL
content
)

Implements org.springframework.jdbc.support.lob.LobCreator.setClobAsString.

Set the given content as String on the given statement, using the given parameter index.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DefaultLobCreatorback to summary
protected DefaultLobCreator()

Method Detail

closeback to summary
public void close()

Implements org.springframework.jdbc.support.lob.LobCreator.close.

Doc from org.springframework.jdbc.support.lob.LobCreator.close.

Close this LobCreator session and free its temporarily created BLOBs and CLOBs. Will not need to do anything if using PreparedStatement's standard methods, but might be necessary to free database resources if using proprietary means.

Note

Needs to be invoked after the involved PreparedStatements have been executed or the affected O/R mapping sessions have been flushed. Otherwise, the database resources for the temporary BLOBs might stay allocated.

Annotations
@Override
setBlobAsBinaryStreamback to summary
public void setBlobAsBinaryStream(PreparedStatement ps, int paramIndex, InputStream binaryStream, int contentLength) throws SQLException

Implements org.springframework.jdbc.support.lob.LobCreator.setBlobAsBinaryStream.

Doc from org.springframework.jdbc.support.lob.LobCreator.setBlobAsBinaryStream.

Set the given content as binary stream on the given statement, using the given parameter index. Might simply invoke PreparedStatement.setBinaryStream or create a Blob instance for it, depending on the database and driver.

Parameters
ps:PreparedStatement

the PreparedStatement to the set the content on

paramIndex:int

the parameter index to use

binaryStream:InputStream

the content as binary stream, or null for SQL NULL

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods
setBlobAsBytesback to summary
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content) throws SQLException

Implements org.springframework.jdbc.support.lob.LobCreator.setBlobAsBytes.

Doc from org.springframework.jdbc.support.lob.LobCreator.setBlobAsBytes.

Set the given content as bytes on the given statement, using the given parameter index. Might simply invoke PreparedStatement.setBytes or create a Blob instance for it, depending on the database and driver.

Parameters
ps:PreparedStatement

the PreparedStatement to the set the content on

paramIndex:int

the parameter index to use

content:byte[]

the content as byte array, or null for SQL NULL

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods
setClobAsAsciiStreamback to summary
public void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength) throws SQLException

Implements org.springframework.jdbc.support.lob.LobCreator.setClobAsAsciiStream.

Doc from org.springframework.jdbc.support.lob.LobCreator.setClobAsAsciiStream.

Set the given content as ASCII stream on the given statement, using the given parameter index. Might simply invoke PreparedStatement.setAsciiStream or create a Clob instance for it, depending on the database and driver.

Parameters
ps:PreparedStatement

the PreparedStatement to the set the content on

paramIndex:int

the parameter index to use

asciiStream:InputStream

the content as ASCII stream, or null for SQL NULL

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods
setClobAsCharacterStreamback to summary
public void setClobAsCharacterStream(PreparedStatement ps, int paramIndex, Reader characterStream, int contentLength) throws SQLException

Implements org.springframework.jdbc.support.lob.LobCreator.setClobAsCharacterStream.

Doc from org.springframework.jdbc.support.lob.LobCreator.setClobAsCharacterStream.

Set the given content as character stream on the given statement, using the given parameter index. Might simply invoke PreparedStatement.setCharacterStream or create a Clob instance for it, depending on the database and driver.

Parameters
ps:PreparedStatement

the PreparedStatement to the set the content on

paramIndex:int

the parameter index to use

characterStream:Reader

the content as character stream, or null for SQL NULL

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods
setClobAsStringback to summary
public void setClobAsString(PreparedStatement ps, int paramIndex, String content) throws SQLException

Implements org.springframework.jdbc.support.lob.LobCreator.setClobAsString.

Doc from org.springframework.jdbc.support.lob.LobCreator.setClobAsString.

Set the given content as String on the given statement, using the given parameter index. Might simply invoke PreparedStatement.setString or create a Clob instance for it, depending on the database and driver.

Parameters
ps:PreparedStatement

the PreparedStatement to the set the content on

paramIndex:int

the parameter index to use

content:String

the content as String, or null for SQL NULL

Annotations
@Override
Exceptions
SQLException:
if thrown by JDBC methods