-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed a bug where calling length() after obtaining a stream would close the stream for Clobs/NClobs #799
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #799 +/- ##
============================================
+ Coverage 48.53% 48.61% +0.07%
- Complexity 2806 2809 +3
============================================
Files 116 116
Lines 27862 27882 +20
Branches 4641 4645 +4
============================================
+ Hits 13523 13554 +31
+ Misses 12197 12195 -2
+ Partials 2142 2133 -9
Continue to review full report at Codecov.
|
src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsStreamingTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsStreamingTest.java
Outdated
Show resolved
Hide resolved
…trieving stream but before consuming stream
try { | ||
inputStream.reset(); | ||
} catch (IOException e) { | ||
throw new SQLServerException(e.getMessage(), null, 0, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use "SQLServerException.makeFromDriverError" instead. Also applies to other locations in the changes made.
NClob c = rs.getNClob(2); | ||
assertEquals(c.length(), lobs.get(rs.getInt(1)).length()); | ||
lobsFromServer.add(c); | ||
String recieved = getStringFromInputStream(c.getAsciiStream());// streaming string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move Resultsets and Streams to try-with-resources. Avoid hidden streams calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resultsets are automatically closed when statement is closed, and statement is in try-with-resources. Inputstreams are closed when resultset is closed, and I manually call resultset.close() in order to load stream into memory.
InputStream inputStream = (InputStream) activeStreams.get(0); | ||
try { | ||
inputStream.reset(); | ||
getterStream = inputStream; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we close 'inputStream' here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same input stream that the user gets, if we close it, the stream returned to the user gets closed.
} | ||
rs.close(); | ||
} | ||
stmt.execute("DROP TABLE [" + tableName + "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop table in finally block to avoid table leftovers in case exception occurs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stmt was created in try-catch, cannot access in finally block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- There is already
dropTableIfExists()
in Utils.java, consider using it. - Move the
drop table
into afinally
block. You can either create a newStatement
for it or just use regular try-catch blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or a cleaner solution would be to modify dropTableIfExists()
to create and close its own Statement
.
|
||
if (value == null && activeStreams.get(0) instanceof PLPInputStream) { | ||
return (long) ((PLPInputStream) activeStreams.get(0)).payloadLength / 2; | ||
if (value == null && activeStreams.get(0) instanceof BaseInputStream) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null == value
src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java
Outdated
Show resolved
Hide resolved
assertEquals(lobs.get(index), recieved);// compare streamed string to initial string | ||
} | ||
rs.close(); | ||
for (int i = 0; i < lobs.size(); i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is unnecessary now.
assertEquals(c.length(), lobs.get(index).length()); | ||
lobsFromServer.add(c); | ||
String recieved = getStringFromInputStream(c.getAsciiStream(), | ||
java.nio.charset.StandardCharsets.US_ASCII);// streaming string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the comment (with explanation), we are not streaming anymore.
src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java
Show resolved
Hide resolved
+ "Ǥ⚌c♮ƺåYèĢù⚏Ȓ★njäõpƸŃōoƝĤßuÙőƆE♹gLJÜŬȺDZ!Û☵ŦãǁĸNQŰǚǻTÖC]ǶýåÉbɉ☩=\\ȍáźŗǃĻýű☓☄¸T☑ö^k☏I:x☑⚀läiȉ☱☚⚅ǸǎãÂ"; | ||
private static String tableName = null; | ||
|
||
private static enum LoB { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seem to be odd camel casing B here??
while (true) { | ||
int amountRead = r.read(buffer, 0, (int) l); | ||
if (amountRead == -1) { | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm better if we do while ((amountRead = r.read(buffer, 0, (int) l)) != null)? then we don't have to break out of a while which makes me cringe a little when I see it... ha not big deal ;)
Clob c = rs.getClob(2); | ||
Reader r = c.getCharacterStream(); | ||
long clobLength = c.length(); | ||
String recieved = getStringFromReader(r, clobLength);// streaming string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
received - "i before e except after c" ;)
NClob c = rs.getNClob(2); | ||
assertEquals(c.length(), lob_data.get(index).length()); | ||
lobsFromServer.add(c); | ||
String recieved = getStringFromInputStream(c.getAsciiStream());// NClob AsciiStream is never |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
received
Addresses #788. Also addressed an issue where varchar(max)/Clob objects were always being encoded to UTF-16LE instead of using the Collation specified in the Clob object.
List of Changes (the following applies to NClob as well):