Skip to content

Commit d65db65

Browse files
committed
DefaultLobCreator supports JDBC 4.0 set-stream variants without length parameter
Issue: SPR-12265
1 parent 7f9baa3 commit d65db65

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/DefaultLobHandler.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,13 @@
3636
* Invokes the direct accessor methods that {@code java.sql.ResultSet}
3737
* and {@code java.sql.PreparedStatement} offer.
3838
*
39+
* <p>By default, incoming streams are going to be passed to the appropriate
40+
* {@code setBinary/Ascii/CharacterStream} method on the JDBC driver's
41+
* {@link PreparedStatement}. If the specified content length is negative,
42+
* this handler will use the JDBC 4.0 variants of the set-stream methods
43+
* without a length parameter; otherwise, it will pass the specified length
44+
* on to the driver.
45+
*
3946
* <p>This LobHandler should work for any JDBC driver that is JDBC compliant
4047
* in terms of the spec's suggestions regarding simple BLOB and CLOB handling.
4148
* This does not apply to Oracle 9i's drivers at all; as of Oracle 10g,
@@ -262,9 +269,12 @@ else if (wrapAsLob) {
262269
ps.setBlob(paramIndex, (Blob) null);
263270
}
264271
}
265-
else {
272+
else if (contentLength >= 0) {
266273
ps.setBinaryStream(paramIndex, binaryStream, contentLength);
267274
}
275+
else {
276+
ps.setBinaryStream(paramIndex, binaryStream);
277+
}
268278
if (logger.isDebugEnabled()) {
269279
logger.debug(binaryStream != null ? "Set binary stream for BLOB with length " + contentLength :
270280
"Set BLOB to null");
@@ -326,9 +336,12 @@ else if (wrapAsLob) {
326336
ps.setClob(paramIndex, (Clob) null);
327337
}
328338
}
329-
else {
339+
else if (contentLength >= 0) {
330340
ps.setAsciiStream(paramIndex, asciiStream, contentLength);
331341
}
342+
else {
343+
ps.setAsciiStream(paramIndex, asciiStream);
344+
}
332345
if (logger.isDebugEnabled()) {
333346
logger.debug(asciiStream != null ? "Set ASCII stream for CLOB with length " + contentLength :
334347
"Set CLOB to null");
@@ -356,9 +369,12 @@ else if (wrapAsLob) {
356369
ps.setClob(paramIndex, (Clob) null);
357370
}
358371
}
359-
else {
372+
else if (contentLength >= 0) {
360373
ps.setCharacterStream(paramIndex, characterStream, contentLength);
361374
}
375+
else {
376+
ps.setCharacterStream(paramIndex, characterStream);
377+
}
362378
if (logger.isDebugEnabled()) {
363379
logger.debug(characterStream != null ? "Set character stream for CLOB with length " + contentLength :
364380
"Set CLOB to null");

spring-jdbc/src/test/java/org/springframework/jdbc/support/DefaultLobHandlerTests.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import java.sql.SQLException;
2727

2828
import org.junit.Test;
29+
2930
import org.springframework.jdbc.support.lob.DefaultLobHandler;
3031
import org.springframework.jdbc.support.lob.LobCreator;
3132
import org.springframework.jdbc.support.lob.LobHandler;
@@ -39,10 +40,14 @@
3940
public class DefaultLobHandlerTests {
4041

4142
private ResultSet rs = mock(ResultSet.class);
43+
4244
private PreparedStatement ps = mock(PreparedStatement.class);
45+
4346
private LobHandler lobHandler = new DefaultLobHandler();
47+
4448
private LobCreator lobCreator = lobHandler.getLobCreator();
4549

50+
4651
@Test
4752
public void testGetBlobAsBytes() throws SQLException {
4853
lobHandler.getBlobAsBytes(rs, 1);
@@ -82,12 +87,18 @@ public void testSetBlobAsBytes() throws SQLException {
8287

8388
@Test
8489
public void testSetBlobAsBinaryStream() throws SQLException, IOException {
85-
8690
InputStream bis = new ByteArrayInputStream("testContent".getBytes());
8791
lobCreator.setBlobAsBinaryStream(ps, 1, bis, 11);
8892
verify(ps).setBinaryStream(1, bis, 11);
8993
}
9094

95+
@Test
96+
public void testSetBlobAsBinaryStreamWithoutLength() throws SQLException, IOException {
97+
InputStream bis = new ByteArrayInputStream("testContent".getBytes());
98+
lobCreator.setBlobAsBinaryStream(ps, 1, bis, -1);
99+
verify(ps).setBinaryStream(1, bis);
100+
}
101+
91102
@Test
92103
public void testSetClobAsString() throws SQLException, IOException {
93104
String content = "testContent";
@@ -102,11 +113,25 @@ public void testSetClobAsAsciiStream() throws SQLException, IOException {
102113
verify(ps).setAsciiStream(1, bis, 11);
103114
}
104115

116+
@Test
117+
public void testSetClobAsAsciiStreamWithoutLength() throws SQLException, IOException {
118+
InputStream bis = new ByteArrayInputStream("testContent".getBytes());
119+
lobCreator.setClobAsAsciiStream(ps, 1, bis, -1);
120+
verify(ps).setAsciiStream(1, bis);
121+
}
122+
105123
@Test
106124
public void testSetClobAsCharacterStream() throws SQLException, IOException {
107125
Reader str = new StringReader("testContent");
108126
lobCreator.setClobAsCharacterStream(ps, 1, str, 11);
109127
verify(ps).setCharacterStream(1, str, 11);
110128
}
111129

130+
@Test
131+
public void testSetClobAsCharacterStreamWithoutLength() throws SQLException, IOException {
132+
Reader str = new StringReader("testContent");
133+
lobCreator.setClobAsCharacterStream(ps, 1, str, -1);
134+
verify(ps).setCharacterStream(1, str);
135+
}
136+
112137
}

0 commit comments

Comments
 (0)