Permalink
Browse files

fix: don't attempt to read a SQLXML more than once (#965)

fixes #964
  • Loading branch information...
bbodnar authored and vlsi committed Oct 25, 2017
1 parent 266ed61 commit 8f5e2454185a929f1bc6ef66813d6681bb38e736
@@ -1525,10 +1525,11 @@ public void setNClob(int parameterIndex, Reader reader) throws SQLException {
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
checkClosed();
if (xmlObject == null || xmlObject.getString() == null) {
String stringValue = xmlObject == null ? null : xmlObject.getString();
if (stringValue == null) {
setNull(parameterIndex, Types.SQLXML);
} else {
setString(parameterIndex, xmlObject.getString(), Oid.XML);
setString(parameterIndex, stringValue, Oid.XML);
}
}
@@ -21,13 +21,19 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Types;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Result;
@@ -255,6 +261,55 @@ public void testGetObject() throws SQLException {
SQLXML xml = (SQLXML) rs.getObject(1);
}
private SQLXML newConsumableSQLXML(String content) throws Exception {
SQLXML xml = (SQLXML) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { SQLXML.class }, new InvocationHandler() {
SQLXML xml = con.createSQLXML();
boolean consumed = false;
Set<Method> consumingMethods = new HashSet<Method>(Arrays.asList(
SQLXML.class.getMethod("getBinaryStream"),
SQLXML.class.getMethod("getCharacterStream"),
SQLXML.class.getMethod("getString")
));
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (consumingMethods.contains(method)) {
if (consumed) {
fail("SQLXML-object already consumed");
} else {
consumed = true;
}
}
return method.invoke(xml, args);
}
});
xml.setString(content);
return xml;
}
@Test
public void testSet() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("DELETE FROM xmltest");
stmt.close();
PreparedStatement ps = con.prepareStatement("INSERT INTO xmltest VALUES (?,?)");
ps.setInt(1, 1);
ps.setSQLXML(2, newConsumableSQLXML(_xmlDocument));
assertEquals(1, ps.executeUpdate());
ps.setInt(1, 2);
ps.setObject(2, newConsumableSQLXML(_xmlDocument));
assertEquals(1, ps.executeUpdate());
ResultSet rs = getRS();
assertTrue(rs.next());
Object o = rs.getObject(1);
assertTrue(o instanceof SQLXML);
assertEquals(_xmlDocument, ((SQLXML) o).getString());
assertTrue(rs.next());
assertEquals(_xmlDocument, rs.getSQLXML(1).getString());
assertTrue(!rs.next());
}
@Test
public void testSetNull() throws SQLException {
Statement stmt = con.createStatement();

0 comments on commit 8f5e245

Please sign in to comment.