Skip to content

Commit

Permalink
Blob fix
Browse files Browse the repository at this point in the history
Fills the contents of a blob and makes it availible for use after the RS or statement has been closed. Addresses the TDS issue from microsoft#567.
  • Loading branch information
rene-ye committed Jan 3, 2018
1 parent 4bd1e48 commit 4758485
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ public long length() throws SQLException {
return value.length;
}

/**
* Public function for the result set to maintain blobs it has created
* @throws SQLException
*/
public void fillByteArray() throws SQLException {
if(!isClosed)
getBytesFromStream();
}

/**
* Converts stream to byte[]
* @throws SQLServerException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
Expand Down Expand Up @@ -126,6 +128,7 @@ final void setCurrentRowType(RowType rowType) {
* occurs
*/
private Closeable activeStream;
private List<Object> streamObjects = new ArrayList<Object>();

/**
* A window of fetchSize quickly accessible rows for scrollable result sets
Expand Down Expand Up @@ -576,7 +579,10 @@ private void closeInternal() {

// Mark this ResultSet as closed, then clean up.
isClosed = true;


//fill all unclosed objects which rely on the stream
fillBlobs();

// Discard the current fetch buffer contents.
discardFetchBuffer();

Expand Down Expand Up @@ -2664,6 +2670,7 @@ public Blob getBlob(int i) throws SQLServerException {
checkClosed();
Blob value = (Blob) getValue(i, JDBCType.BLOB);
loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
streamObjects.add(value);
return value;
}

Expand All @@ -2672,6 +2679,7 @@ public Blob getBlob(String colName) throws SQLServerException {
checkClosed();
Blob value = (Blob) getValue(findColumn(colName), JDBCType.BLOB);
loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
streamObjects.add(value);
return value;
}

Expand Down Expand Up @@ -6514,6 +6522,23 @@ final void doServerFetch(int fetchType,
scrollWindow.reset();
}
}

/*
* Iterates through the list of objects which rely on the stream that's about to be closed, filling them with their data
* Will skip over closed blobs, implemented in SQLServerBlob
*/
private void fillBlobs() {
for(Object o : streamObjects) {
if(o instanceof SQLServerBlob) {
try {
((SQLServerBlob) o).fillByteArray();
} catch (SQLException e) {
if (logger.isLoggable(java.util.logging.Level.FINER))
logger.finer(toString() + "Filling blobs before closing: " + e.getMessage());
}
}
}
}

/**
* Discards the contents of the current fetch buffer.
Expand Down

0 comments on commit 4758485

Please sign in to comment.