DefaultLobHandler uses explicit Blob/Clob access for reading when "wrapAsLob"=true

master
Juergen Hoeller 16 years ago
parent 08dd18df58
commit ad266a347e
  1. 42
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.java
  2. 21
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/LobHandler.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -115,27 +115,57 @@ public class DefaultLobHandler extends AbstractLobHandler {
public byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException {
logger.debug("Returning BLOB as bytes");
return rs.getBytes(columnIndex);
if (this.wrapAsLob) {
Blob blob = rs.getBlob(columnIndex);
return blob.getBytes(1, (int) blob.length());
}
else {
return rs.getBytes(columnIndex);
}
}
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
logger.debug("Returning BLOB as binary stream");
return rs.getBinaryStream(columnIndex);
if (this.wrapAsLob) {
Blob blob = rs.getBlob(columnIndex);
return blob.getBinaryStream();
}
else {
return rs.getBinaryStream(columnIndex);
}
}
public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException {
logger.debug("Returning CLOB as string");
return rs.getString(columnIndex);
if (this.wrapAsLob) {
Clob clob = rs.getClob(columnIndex);
return clob.getSubString(1, (int) clob.length());
}
else {
return rs.getString(columnIndex);
}
}
public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException {
logger.debug("Returning CLOB as ASCII stream");
return rs.getAsciiStream(columnIndex);
if (this.wrapAsLob) {
Clob clob = rs.getClob(columnIndex);
return clob.getAsciiStream();
}
else {
return rs.getAsciiStream(columnIndex);
}
}
public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException {
logger.debug("Returning CLOB as character stream");
return rs.getCharacterStream(columnIndex);
if (this.wrapAsLob) {
Clob clob = rs.getClob(columnIndex);
return clob.getCharacterStream();
}
else {
return rs.getCharacterStream(columnIndex);
}
}
public LobCreator getLobCreator() {

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,21 +31,22 @@ import java.sql.SQLException;
* <p>Provides accessor methods for BLOBs and CLOBs, and acts as factory for
* LobCreator instances, to be used as sessions for creating BLOBs or CLOBs.
* LobCreators are typically instantiated for each statement execution or for
* each transaction. They are not thread-safe because they might track
* allocated database resources to be able to free them after execution.
* each transaction; they are not thread-safe because they might track
* allocated database resources in order to free them after execution.
*
* <p>Most databases/drivers should be able to work with {@link DefaultLobHandler},
* which by default delegates to JDBC's direct accessor methods, avoiding
* <code>java.sql.Blob</code> and <code>java.sql.Clob</code> completely.
* {@link DefaultLobHandler} can also be configured to populate LOBs using
* <code>PreparedStatement.setBlob/setClob</code> (e.g. for PostgreSQL).
* which by default delegates to JDBC's direct accessor methods, avoiding the
* <code>java.sql.Blob</code> and <code>java.sql.Clob</code> API completely.
* {@link DefaultLobHandler} can also be configured to access LOBs using
* <code>PreparedStatement.setBlob/setClob</code> (e.g. for PostgreSQL), through
* setting the {@link DefaultLobHandler#setWrapAsLob "wrapAsLob"} property.
*
* <p>Unfortunately, Oracle 9i 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
* {@link OracleLobHandler} there, which uses Oracle's BLOB/CLOB API for both all access.
* The Oracle 10g JDBC driver should basically work with {@link DefaultLobHandler}
* as well, with some limitations in terms of LOB sizes.
* {@link OracleLobHandler} there, which uses Oracle's BLOB/CLOB API for both types
* of access. The Oracle 10g JDBC driver should basically work with
* {@link DefaultLobHandler} as well, with some limitations in terms of LOB sizes.
*
* <p>Of course, you need to declare different field types for each database.
* In Oracle, any binary content needs to go into a BLOB, and all character content

Loading…
Cancel
Save