Skip to content

Commit

Permalink
Fixed problem with storing partially formed objects in the cache because
Browse files Browse the repository at this point in the history
of select arguments.  Had to change DatabaseConnection and
DatabaseResults slightly.
  • Loading branch information
Gray Watson committed Jun 8, 2016
1 parent 599f92a commit c62555a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public class JdbcCompiledStatement implements CompiledStatement {

private final PreparedStatement preparedStatement;
private final StatementType type;
private final boolean cacheStore;
private ResultSetMetaData metaData = null;

public JdbcCompiledStatement(PreparedStatement preparedStatement, StatementType type) {
public JdbcCompiledStatement(PreparedStatement preparedStatement, StatementType type, boolean cacheStore) {
this.preparedStatement = preparedStatement;
this.type = type;
this.cacheStore = cacheStore;
}

@Override
Expand Down Expand Up @@ -58,7 +60,7 @@ public DatabaseResults runQuery(ObjectCache objectCache) throws SQLException {
if (!type.isOkForQuery()) {
throw new IllegalArgumentException("Cannot call query on a " + type + " statement");
}
return new JdbcDatabaseResults(preparedStatement, preparedStatement.executeQuery(), objectCache);
return new JdbcDatabaseResults(preparedStatement, preparedStatement.executeQuery(), objectCache, cacheStore);
}

@Override
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/j256/ormlite/jdbc/JdbcDatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ public int executeStatement(String statementStr, int resultFlags) throws SQLExce

@Override
public CompiledStatement compileStatement(String statement, StatementType type, FieldType[] argFieldTypes,
int resultFlags) throws SQLException {
int resultFlags, boolean cacheStore) throws SQLException {
if (resultFlags == DatabaseConnection.DEFAULT_RESULT_FLAGS) {
resultFlags = ResultSet.TYPE_FORWARD_ONLY;
}
JdbcCompiledStatement compiledStatement =
new JdbcCompiledStatement(connection.prepareStatement(statement, resultFlags,
ResultSet.CONCUR_READ_ONLY), type);
JdbcCompiledStatement compiledStatement = new JdbcCompiledStatement(
connection.prepareStatement(statement, resultFlags, ResultSet.CONCUR_READ_ONLY), type, cacheStore);
logger.trace("compiled statement: {}", statement);
return compiledStatement;
}
Expand Down Expand Up @@ -302,7 +301,7 @@ private <T> Object queryForOne(String statement, Object[] args, FieldType[] argF
DatabaseResults results = null;
try {
statementSetArgs(stmt, args, argFieldTypes);
results = new JdbcDatabaseResults(stmt, stmt.executeQuery(), objectCache);
results = new JdbcDatabaseResults(stmt, stmt.executeQuery(), objectCache, false);
logger.trace("{} statement is prepared and executed: {}", label, statement);
if (!results.first()) {
// no results at all
Expand All @@ -327,21 +326,22 @@ private Number getIdColumnData(ResultSet resultSet, ResultSetMetaData metaData,
throws SQLException {
int typeVal = metaData.getColumnType(columnIndex);
switch (typeVal) {
case Types.BIGINT :
case Types.DECIMAL :
case Types.NUMERIC :
case Types.BIGINT:
case Types.DECIMAL:
case Types.NUMERIC:
return (Number) resultSet.getLong(columnIndex);
case Types.INTEGER :
case Types.INTEGER:
return (Number) resultSet.getInt(columnIndex);
default :
default:
String columnName = metaData.getColumnName(columnIndex);
throw new SQLException("Unexpected ID column type " + TypeValMapper.getSqlTypeForTypeVal(typeVal)
+ " (typeVal " + typeVal + ") in column " + columnName + "(#" + columnIndex
+ ") is not a number");
throw new SQLException(
"Unexpected ID column type " + TypeValMapper.getSqlTypeForTypeVal(typeVal) + " (typeVal "
+ typeVal + ") in column " + columnName + "(#" + columnIndex + ") is not a number");
}
}

private void statementSetArgs(PreparedStatement stmt, Object[] args, FieldType[] argFieldTypes) throws SQLException {
private void statementSetArgs(PreparedStatement stmt, Object[] args, FieldType[] argFieldTypes)
throws SQLException {
if (args == null) {
return;
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/j256/ormlite/jdbc/JdbcDatabaseResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ public class JdbcDatabaseResults implements DatabaseResults {
private final ResultSet resultSet;
private final ResultSetMetaData metaData;
private final ObjectCache objectCache;
private final boolean cacheStore;
private boolean first = true;

public JdbcDatabaseResults(PreparedStatement preparedStmt, ResultSet resultSet, ObjectCache objectCache)
throws SQLException {
public JdbcDatabaseResults(PreparedStatement preparedStmt, ResultSet resultSet, ObjectCache objectCache,
boolean cacheStore) throws SQLException {
this.preparedStmt = preparedStmt;
this.resultSet = resultSet;
this.metaData = resultSet.getMetaData();
this.objectCache = objectCache;
this.cacheStore = cacheStore;
}

@Override
Expand Down Expand Up @@ -189,10 +191,19 @@ public boolean wasNull(int columnIndex) throws SQLException {
}

@Override
public ObjectCache getObjectCache() {
public ObjectCache getObjectCacheForRetrieve() {
return objectCache;
}

@Override
public ObjectCache getObjectCacheForStore() {
if (cacheStore) {
return objectCache;
} else {
return null;
}
}

@Override
public void close() throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testGetColumnName() throws Exception {
expect(preparedStatement.getMetaData()).andReturn(metadata);
preparedStatement.close();
replay(metadata, preparedStatement);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT, false);
assertEquals("TEST_COLUMN1", stmt.getColumnName(0));
stmt.close();
verify(preparedStatement, metadata);
Expand All @@ -38,7 +38,7 @@ public void testGetMoreResults() throws Exception {
expect(preparedStatement.getMoreResults()).andReturn(Boolean.TRUE);
preparedStatement.close();
replay(preparedStatement);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT, false);
stmt.getMoreResults();
stmt.close();
verify(preparedStatement);
Expand All @@ -51,7 +51,7 @@ public void testSetNull() throws Exception {
EasyMock.expectLastCall();
preparedStatement.close();
replay(preparedStatement);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT, false);
stmt.setObject(0, null, SqlType.STRING);
stmt.close();
verify(preparedStatement);
Expand All @@ -60,15 +60,15 @@ public void testSetNull() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void testExecuteUpdateWithSelectType() throws Exception {
PreparedStatement preparedStatement = createMock(PreparedStatement.class);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.SELECT, false);
stmt.runUpdate();
stmt.close();
}

@Test(expected = IllegalArgumentException.class)
public void testExecuteQueryWithNonSelectType() throws Exception {
PreparedStatement preparedStatement = createMock(PreparedStatement.class);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.EXECUTE);
JdbcCompiledStatement stmt = new JdbcCompiledStatement(preparedStatement, StatementType.EXECUTE, false);
stmt.runQuery(null);
stmt.close();
}
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/com/j256/ormlite/jdbc/JdbcDatabaseResultsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testGetBlobStream() throws Exception {
expect(resultSet.getBlob(1)).andReturn(blob);
resultSet.close();
replay(preparedStatement, blob, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertTrue(results.getBlobStream(0) == is);
results.close();
verify(preparedStatement, blob, resultSet);
Expand All @@ -48,7 +48,7 @@ public void testGetBlobStreamNull() throws Exception {
expect(resultSet.getBlob(1)).andReturn(null);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertNull(results.getBlobStream(0));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -64,7 +64,7 @@ public void testFindColumn() throws Exception {
expect(resultSet.findColumn(name)).andReturn(colN);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(colN - 1, results.findColumn(name));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -80,7 +80,7 @@ public void testGetColumnCount() throws Exception {
expect(metaData.getColumnCount()).andReturn(colN);
resultSet.close();
replay(preparedStatement, resultSet, metaData);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(colN, results.getColumnCount());
results.close();
verify(preparedStatement, resultSet, metaData);
Expand All @@ -95,7 +95,7 @@ public void testIsNull() throws Exception {
expect(resultSet.wasNull()).andReturn(true);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertTrue(results.wasNull(colN));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -114,7 +114,7 @@ public void testNext() throws Exception {
expect(preparedStatement.getMoreResults()).andReturn(false);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertTrue(results.next());
assertTrue(results.next());
assertFalse(results.next());
Expand All @@ -132,7 +132,7 @@ public void testGetBoolean() throws Exception {
expect(resultSet.getBoolean(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getBoolean(colN));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -148,7 +148,7 @@ public void testGetByte() throws Exception {
expect(resultSet.getByte(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getByte(colN));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -164,7 +164,7 @@ public void testGetBytes() throws Exception {
expect(resultSet.getBytes(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertTrue(results.getBytes(colN).equals(val));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -180,7 +180,7 @@ public void testGetDouble() throws Exception {
expect(resultSet.getDouble(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getDouble(colN), 0.0F);
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -196,7 +196,7 @@ public void testGetFloat() throws Exception {
expect(resultSet.getFloat(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getFloat(colN), 0.0F);
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -212,7 +212,7 @@ public void testGetInt() throws Exception {
expect(resultSet.getInt(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getInt(colN));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -228,7 +228,7 @@ public void testGetShort() throws Exception {
expect(resultSet.getShort(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getShort(colN));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -244,7 +244,7 @@ public void testGetString() throws Exception {
expect(resultSet.getString(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getString(colN));
results.close();
verify(preparedStatement, resultSet);
Expand All @@ -260,7 +260,7 @@ public void testGetTimestamp() throws Exception {
expect(resultSet.getTimestamp(colN + 1)).andReturn(val);
resultSet.close();
replay(preparedStatement, resultSet);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null);
JdbcDatabaseResults results = new JdbcDatabaseResults(preparedStatement, resultSet, null, false);
assertEquals(val, results.getTimestamp(colN));
results.close();
verify(preparedStatement, resultSet);
Expand Down

0 comments on commit c62555a

Please sign in to comment.