Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java (revision 702455) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java (working copy) @@ -479,13 +479,4 @@ } } - public void closeSilently(ResultSet rs) { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - // ignore - } - } - } } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java (revision 706586) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java (working copy) @@ -18,7 +18,6 @@ import java.io.InputStream; import java.sql.ResultSet; -import java.sql.Statement; import org.apache.jackrabbit.core.persistence.bundle.util.ConnectionRecoveryManager; import org.slf4j.Logger; @@ -33,36 +32,52 @@ protected final ConnectionRecoveryManager conn; protected final ResultSet rs; - protected final Statement stmt; protected final InputStream in; protected final DbDataStore store; protected boolean closed; + + /** + * Construct a db resource using the specified input stream. + * + * @param in the input stream + */ + public DbResources(InputStream in) { + this(null, null, in, null); + } - public DbResources(ConnectionRecoveryManager conn, ResultSet rs, Statement stmt, InputStream in, DbDataStore store) { + /** + * Construct a db resource using the specified connection. The connection + * will be returned to the data store once the resource is fully read. If + * the connection is null, then this class is just a container for the input + * stream. This is to support other kinds of input streams as well. + * + * @param conn the connection (may be null) + * @param rs the result set (may be null) + * @param in the input stream + * @param store the data store + */ + public DbResources(ConnectionRecoveryManager conn, ResultSet rs, InputStream in, DbDataStore store) { this.conn = conn; this.rs = rs; - this.stmt = stmt; this.in = in; this.store = store; - this.closed = false; + if (conn == null) { + closed = true; + } } - public ConnectionRecoveryManager getConnection() { - return conn; - } - + /** + * Get the input stream. + * + * @return the input stream + */ public InputStream getInputStream() { return in; } - public ResultSet getResultSet() { - return rs; - } - - public Statement getStatement() { - return stmt; - } - + /** + * Close the stream, and return the connection to the data store. + */ public void close() { if (!closed) { closed = true; Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java (revision 706586) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java (working copy) @@ -39,10 +39,11 @@ protected DataIdentifier identifier; /** - * @param in the stream obtained by a call to ResultSet.getBinaryStream(). - * @param con the connection to the DB. It must not be closed. - * @param rs the result set from wich the stream is obtained. It must not be closed. - * @param stmt the statemen that produced the result set. It must not be closed. + * Create a database input stream for the given identifier. + * Database access is delayed until the first byte is read from the stream. + * + * @param store the database data store + * @param identifier the data identifier */ protected DbInputStream(DbDataStore store, DataIdentifier identifier) { super(null); @@ -88,15 +89,7 @@ * When the stream is consumed, the database resources held by the instance are closed. */ public int read(byte[] b) throws IOException { - if (streamFinished) { - return -1; - } - int c = read(b, 0, b.length); - if (c == -1) { - streamFinished = true; - close(); - } - return c; + return read(b, 0, b.length); } /** @@ -127,6 +120,7 @@ streamClosed = true; // It may be null (see constructor) if (in != null) { + in.close(); super.close(); } // resources may be null (if getStream() was not called) Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java (revision 706586) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java (working copy) @@ -366,7 +366,7 @@ } catch (Exception e) { throw convert("Can not insert new record", e); } finally { - conn.closeSilently(rs); + DatabaseHelper.closeSilently(rs); putBack(conn); if (fileInput != null) { try { @@ -437,7 +437,7 @@ } catch (Exception e) { throw convert("Can not read records", e); } finally { - conn.closeSilently(rs); + DatabaseHelper.closeSilently(rs); putBack(conn); } } @@ -481,7 +481,7 @@ } catch (Exception e) { throw convert("Can not read identifier " + identifier, e); } finally { - conn.closeSilently(rs); + DatabaseHelper.closeSilently(rs); putBack(conn); } } @@ -507,23 +507,26 @@ if (!rs.next()) { throw new DataStoreException("Record not found: " + identifier); } - InputStream result = null; InputStream stream = rs.getBinaryStream(2); + DbResources dbResource = null; if (stream == null) { // If the stream is null, go ahead and close resources - result = new ByteArrayInputStream(new byte[0]); + stream = new ByteArrayInputStream(new byte[0]); + dbResource = new DbResources(stream); DatabaseHelper.closeSilently(rs); putBack(conn); + } else if (copyWhenReading) { + // If we copy while reading, create a temp file and close the stream + File temp = moveToTempFile(stream); + stream = new TempFileInputStream(temp); + dbResource = new DbResources(stream); + DatabaseHelper.closeSilently(rs); + putBack(conn); } else { - result = new BufferedInputStream(stream); - if (copyWhenReading) { - File temp = moveToTempFile(result); - result = new TempFileInputStream(temp); - } + stream = new BufferedInputStream(stream); + dbResource = new DbResources(conn, rs, stream, this); } - - DbResources dbResources = new DbResources(conn, rs, prep, result, this); - return dbResources; + return dbResource; } catch (Exception e) { DatabaseHelper.closeSilently(rs); putBack(conn);