org.springframework.jdbc.support.lob
Class OracleLobHandler

java.lang.Object
  extended byorg.springframework.jdbc.support.lob.OracleLobHandler
All Implemented Interfaces:
LobHandler

public class OracleLobHandler
extends java.lang.Object
implements LobHandler

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. Developed and tested on Oracle 9i.

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) having different constant values! As it initializes the 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.

Since:
04.12.2003
Author:
Juergen Hoeller
See Also:
setNativeJdbcExtractor(org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor), oracle.sql.BLOB, oracle.sql.CLOB

Nested Class Summary
protected static interface OracleLobHandler.LobCallback
          Internal callback interface for use with createLob.
protected  class OracleLobHandler.OracleLobCreator
          LobCreator implementation for Oracle databases.
 
Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
OracleLobHandler()
          This constructor retrieves the oracle.sql.BLOB and oracle.sql.CLOB classes via reflection, and initializes the values for the DURATION_SESSION and MODE_READWRITE constants defined there.
 
Method Summary
 java.io.InputStream getBlobAsBinaryStream(java.sql.ResultSet rs, int columnIndex)
          Retrieve the given column as binary stream from the given ResultSet.
 byte[] getBlobAsBytes(java.sql.ResultSet rs, int columnIndex)
          Retrieve the given column as bytes from the given ResultSet.
 java.io.InputStream getClobAsAsciiStream(java.sql.ResultSet rs, int columnIndex)
          Retrieve the given column as ASCII stream from the given ResultSet.
 java.io.Reader getClobAsCharacterStream(java.sql.ResultSet rs, int columnIndex)
          Retrieve the given column as character stream from the given ResultSet.
 java.lang.String getClobAsString(java.sql.ResultSet rs, int columnIndex)
          Retrieve the given column as String from the given ResultSet.
 LobCreator getLobCreator()
          Create a new LobCreator instance, i.e.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

OracleLobHandler

public OracleLobHandler()
                 throws java.lang.ClassNotFoundException,
                        java.lang.NoSuchFieldException,
                        java.lang.IllegalAccessException
This constructor retrieves the oracle.sql.BLOB and oracle.sql.CLOB classes via reflection, and initializes the values for the DURATION_SESSION and MODE_READWRITE constants defined there.

See Also:
oracle.sql.BLOB#DURATION_SESSION, oracle.sql.BLOB#MODE_READWRITE, oracle.sql.CLOB#DURATION_SESSION, oracle.sql.CLOB#MODE_READWRITE
Method Detail

setNativeJdbcExtractor

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 such pools need to return wrapped JDBC object handles.

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. So if PreparedStatement.getConnection() returns a native JDBC Connection with your pool, you don't need to specify an extractor.

See Also:
NativeJdbcExtractor.getNativeConnectionFromStatement(java.sql.Statement), oracle.jdbc.OracleConnection

setCache

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

getBlobAsBytes

public byte[] getBlobAsBytes(java.sql.ResultSet rs,
                             int columnIndex)
                      throws java.sql.SQLException
Description copied from interface: LobHandler
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.

Specified by:
getBlobAsBytes in interface LobHandler
Parameters:
rs - the ResultSet to retrieve the content from
columnIndex - the column index to use
Returns:
the content as byte array
Throws:
java.sql.SQLException - if thrown by JDBC methods

getBlobAsBinaryStream

public java.io.InputStream getBlobAsBinaryStream(java.sql.ResultSet rs,
                                                 int columnIndex)
                                          throws java.sql.SQLException
Description copied from interface: LobHandler
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.

Specified by:
getBlobAsBinaryStream in interface LobHandler
Parameters:
rs - the ResultSet to retrieve the content from
columnIndex - the column index to use
Returns:
the content as binary stream
Throws:
java.sql.SQLException - if thrown by JDBC methods

getClobAsString

public java.lang.String getClobAsString(java.sql.ResultSet rs,
                                        int columnIndex)
                                 throws java.sql.SQLException
Description copied from interface: LobHandler
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.

Specified by:
getClobAsString in interface LobHandler
Parameters:
rs - the ResultSet to retrieve the content from
columnIndex - the column index to use
Returns:
the content as String
Throws:
java.sql.SQLException - if thrown by JDBC methods

getClobAsAsciiStream

public java.io.InputStream getClobAsAsciiStream(java.sql.ResultSet rs,
                                                int columnIndex)
                                         throws java.sql.SQLException
Description copied from interface: LobHandler
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.

Specified by:
getClobAsAsciiStream in interface LobHandler
Parameters:
rs - the ResultSet to retrieve the content from
columnIndex - the column index to use
Returns:
the content as ASCII stream
Throws:
java.sql.SQLException - if thrown by JDBC methods

getClobAsCharacterStream

public java.io.Reader getClobAsCharacterStream(java.sql.ResultSet rs,
                                               int columnIndex)
                                        throws java.sql.SQLException
Description copied from interface: LobHandler
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.

Specified by:
getClobAsCharacterStream in interface LobHandler
Parameters:
rs - the ResultSet to retrieve the content from
columnIndex - the column index to use
Returns:
the content as character stream
Throws:
java.sql.SQLException - if thrown by JDBC methods

getLobCreator

public LobCreator getLobCreator()
Description copied from interface: LobHandler
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, i.e. after statement execution or transaction completion.

Specified by:
getLobCreator in interface LobHandler
Returns:
the new LobCreator instance
See Also:
LobCreator.close()


Copyright (C) 2003-2004 The Spring Framework Project.