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) @@ -509,9 +509,11 @@ } InputStream result = null; InputStream stream = rs.getBinaryStream(2); + DbResources dbResources; if (stream == null) { // If the stream is null, go ahead and close resources result = new ByteArrayInputStream(new byte[0]); + dbResources = new DbResources(result); DatabaseHelper.closeSilently(rs); putBack(conn); } else { @@ -519,10 +521,11 @@ if (copyWhenReading) { File temp = moveToTempFile(result); result = new TempFileInputStream(temp); + dbResources = new DbResources(result); + } else { + dbResources = new DbResources(conn, rs, result, this); } } - - DbResources dbResources = new DbResources(conn, rs, prep, result, this); return dbResources; } catch (Exception e) { DatabaseHelper.closeSilently(rs);