From ad266a347ebbafc4fe22b1dcc7f2e21b88ddf57b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 12 Feb 2009 23:30:28 +0000 Subject: [PATCH] DefaultLobHandler uses explicit Blob/Clob access for reading when "wrapAsLob"=true --- .../jdbc/support/lob/DefaultLobHandler.java | 42 ++++++++++++++++--- .../jdbc/support/lob/LobHandler.java | 21 +++++----- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.java index 308890c7c3..7ba52c53e5 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.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() { diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/LobHandler.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/LobHandler.java index a994220678..f539f8ab8f 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/lob/LobHandler.java +++ b/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. @@ -31,21 +31,22 @@ import java.sql.SQLException; *

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. * *

Most databases/drivers should be able to work with {@link DefaultLobHandler}, - * which by default delegates to JDBC's direct accessor methods, avoiding - * java.sql.Blob and java.sql.Clob completely. - * {@link DefaultLobHandler} can also be configured to populate LOBs using - * PreparedStatement.setBlob/setClob (e.g. for PostgreSQL). + * which by default delegates to JDBC's direct accessor methods, avoiding the + * java.sql.Blob and java.sql.Clob API completely. + * {@link DefaultLobHandler} can also be configured to access LOBs using + * PreparedStatement.setBlob/setClob (e.g. for PostgreSQL), through + * setting the {@link DefaultLobHandler#setWrapAsLob "wrapAsLob"} property. * *

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. * *

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