Skip to content

Commit

Permalink
JDBC: Fix stackoverflow on getObject and timestamp conversion (#31735)
Browse files Browse the repository at this point in the history
StackOverflowError fix in JdbcResultSet getObject method.
Fix Timestamp conversion bug when getting the value of a time column.
  • Loading branch information
astefan committed Jul 3, 2018
1 parent 2fb2bb2 commit 04cd704
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
throw new SQLException("type is null");
}

return getObject(columnIndex, type);
return convert(columnIndex, type);
}

private <T> T convert(int columnIndex, Class<T> type) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static Object convert(Object v, JDBCType columnType) throws SQLException {
case REAL:
return floatValue(v); // Float might be represented as string for infinity and NaN values
case TIMESTAMP:
return ((Number) v).longValue();
return new Timestamp(((Number) v).longValue());
default:
throw new SQLException("Unexpected column type [" + columnType.getName() + "]");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.joda.time.DateTime;

import java.sql.JDBCType;
import java.sql.Timestamp;

import static org.hamcrest.Matchers.instanceOf;

Expand Down Expand Up @@ -42,8 +43,8 @@ public void testDoubleAsNative() throws Exception {

public void testTimestampAsNative() throws Exception {
DateTime now = DateTime.now();
assertThat(convertAsNative(now, JDBCType.TIMESTAMP), instanceOf(Long.class));
assertEquals(now.getMillis(), convertAsNative(now, JDBCType.TIMESTAMP));
assertThat(convertAsNative(now, JDBCType.TIMESTAMP), instanceOf(Timestamp.class));
assertEquals(now.getMillis(), ((Timestamp) convertAsNative(now, JDBCType.TIMESTAMP)).getTime());
}

private Object convertAsNative(Object value, JDBCType type) throws Exception {
Expand Down

0 comments on commit 04cd704

Please sign in to comment.