public class

OracleLobHandler

extends AbstractLobHandler
java.lang.Object
   ↳ org.springframework.jdbc.support.lob.AbstractLobHandler
     ↳ org.springframework.jdbc.support.lob.OracleLobHandler

Class Overview

LobHandler implementation for Oracle databases. Uses proprietary API to create oracle.sql.BLOB and oracle.sql.CLOB instances, as necessary when working with Oracle's JDBC driver. Note that this LobHandler requires Oracle JDBC driver 9i or higher!

While most databases are able to work with DefaultLobHandler, Oracle just accepts Blob/Clob instances created via its own proprietary BLOB/CLOB API, and additionally doesn't accept large streams for PreparedStatement's corresponding setter methods. Therefore, you need to use a strategy like this LobHandler implementation.

Needs to work on a native JDBC Connection, to be able to cast it to oracle.jdbc.OracleConnection. If you pass in Connections from a connection pool (the usual case in a J2EE environment), you need to set an appropriate NativeJdbcExtractor to allow for automatical retrieval of the underlying native JDBC Connection. LobHandler and NativeJdbcExtractor are separate concerns, therefore they are represented by separate strategy interfaces.

Coded via reflection to avoid dependencies on Oracle classes. Even reads in Oracle constants via reflection because of different Oracle drivers (classes12, ojdbc14, ojdbc5, ojdbc6) having different constant values! As this LobHandler initializes Oracle classes on instantiation, do not define this as eager-initializing singleton if you do not want to depend on the Oracle JAR being in the class path: use "lazy-init=true" to avoid this issue.

See Also

Summary

Nested Classes
interface OracleLobHandler.LobCallback Internal callback interface for use with createLob. 
class OracleLobHandler.OracleLobCreator LobCreator implementation for Oracle databases. 
Fields
protected final Log logger
Public Constructors
OracleLobHandler()
Public Methods
InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex)
byte[] getBlobAsBytes(ResultSet rs, int columnIndex)
InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex)
Reader getClobAsCharacterStream(ResultSet rs, int columnIndex)
String getClobAsString(ResultSet rs, int columnIndex)
LobCreator getLobCreator()
void setCache(boolean cache)
Set whether to cache the temporary LOB in the buffer cache.
void setNativeJdbcExtractor(NativeJdbcExtractor nativeJdbcExtractor)
Set an appropriate NativeJdbcExtractor to be able to retrieve the underlying native oracle.jdbc.OracleConnection.
void setReleaseResourcesAfterRead(boolean releaseResources)
Set whether to agressively release any resources used by the LOB.
Protected Methods
synchronized void initOracleDriverClasses(Connection con)
Retrieve the oracle.sql.BLOB and oracle.sql.CLOB classes via reflection, and initialize the values for the DURATION_SESSION, MODE_READWRITE and MODE_READONLY constants defined there.
void initializeResourcesBeforeRead(Connection con, Object lob)
Initialize any LOB resources before a read is done.
void releaseResourcesAfterRead(Connection con, Object lob)
Release any LOB resources after read is complete.
[Expand]
Inherited Methods
From class org.springframework.jdbc.support.lob.AbstractLobHandler
From class java.lang.Object
From interface org.springframework.jdbc.support.lob.LobHandler

Fields

protected final Log logger

Public Constructors

public OracleLobHandler ()

Public Methods

public InputStream getBlobAsBinaryStream (ResultSet rs, int columnIndex)

Throws
SQLException

public byte[] getBlobAsBytes (ResultSet rs, int columnIndex)

Throws
SQLException

public InputStream getClobAsAsciiStream (ResultSet rs, int columnIndex)

Throws
SQLException

public Reader getClobAsCharacterStream (ResultSet rs, int columnIndex)

Throws
SQLException

public String getClobAsString (ResultSet rs, int columnIndex)

Throws
SQLException

public LobCreator getLobCreator ()

public void setCache (boolean cache)

Set whether to cache the temporary LOB in the buffer cache. This value will be passed into BLOB/CLOB.createTemporary.

Default is true.

See Also
  • oracle.sql.BLOB#createTemporary
  • oracle.sql.CLOB#createTemporary

public void setNativeJdbcExtractor (NativeJdbcExtractor nativeJdbcExtractor)

Set an appropriate NativeJdbcExtractor to be able to retrieve the underlying native oracle.jdbc.OracleConnection. This is necessary for DataSource-based connection pools, as those need to return wrapped JDBC Connection handles that cannot be cast to a native Connection implementation.

Effectively, this LobHandler just invokes a single NativeJdbcExtractor method, namely getNativeConnectionFromStatement with a PreparedStatement argument (falling back to a PreparedStatement.getConnection() call if no extractor is set).

A common choice is SimpleNativeJdbcExtractor, whose Connection unwrapping (which is what OracleLobHandler needs) will work with many connection pools. See SimpleNativeJdbcExtractor's javadoc for details.

public void setReleaseResourcesAfterRead (boolean releaseResources)

Set whether to agressively release any resources used by the LOB. If set to true then you can only read the LOB values once. Any subsequent reads will fail since the resources have been closed.

Setting this property to true can be useful when your queries generates large temporary LOBs that occupy space in the TEMPORARY tablespace or when you want to free up any memory allocated by the driver for the LOB reading.

Default is false.

See Also
  • oracle.sql.BLOB#freeTemporary
  • oracle.sql.CLOB#freeTemporary
  • oracle.sql.BLOB#open
  • oracle.sql.CLOB#open
  • oracle.sql.BLOB#close
  • oracle.sql.CLOB#close

Protected Methods

protected synchronized void initOracleDriverClasses (Connection con)

Retrieve the oracle.sql.BLOB and oracle.sql.CLOB classes via reflection, and initialize the values for the DURATION_SESSION, MODE_READWRITE and MODE_READONLY constants defined there.

Parameters
con the Oracle Connection, for using the exact same class loader that the Oracle driver was loaded with
See Also
  • oracle.sql.BLOB#DURATION_SESSION
  • oracle.sql.BLOB#MODE_READWRITE
  • oracle.sql.BLOB#MODE_READONLY
  • oracle.sql.CLOB#DURATION_SESSION
  • oracle.sql.CLOB#MODE_READWRITE
  • oracle.sql.CLOB#MODE_READONLY

protected void initializeResourcesBeforeRead (Connection con, Object lob)

Initialize any LOB resources before a read is done.

This implementation calls BLOB.open(BLOB.MODE_READONLY) or CLOB.open(CLOB.MODE_READONLY) on any non-temporary LOBs if releaseResourcesAfterRead property is set to true.

This method can be overridden by sublcasses if different behavior is desired.

Parameters
con the connection to be usde for initilization
lob the LOB to initialize

protected void releaseResourcesAfterRead (Connection con, Object lob)

Release any LOB resources after read is complete.

If releaseResourcesAfterRead property is set to true then this implementation calls BLOB.close() or CLOB.close() on any non-temporary LOBs that are open or BLOB.freeTemporary() or CLOB.freeTemporary() on any temporary LOBs.

This method can be overridden by sublcasses if different behavior is desired.

Parameters
con the connection to be usde for initilization
lob the LOB to initialize