Skip to content

Commit

Permalink
Moving creating prepared statement to after blob population
Browse files Browse the repository at this point in the history
  • Loading branch information
glenrobson committed Sep 5, 2023
1 parent a5b3e5d commit 85a993a
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/main/java/edu/illinois/library/cantaloupe/cache/JdbcCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private class ImageBlobOutputStream extends CompletableOutputStream {
private final OutputStream blobOutputStream;
private final OperationList ops;
private final Connection connection;
private final PreparedStatement statement;

/**
* Constructor for writing derivative images.
Expand All @@ -71,28 +70,28 @@ private class ImageBlobOutputStream extends CompletableOutputStream {

connection.setAutoCommit(false);

final Configuration config = Configuration.getInstance();
final String sql = String.format(
"INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)",
config.getString(Key.JDBCCACHE_DERIVATIVE_IMAGE_TABLE),
DERIVATIVE_IMAGE_TABLE_OPERATIONS_COLUMN,
DERIVATIVE_IMAGE_TABLE_IMAGE_COLUMN,
DERIVATIVE_IMAGE_TABLE_LAST_ACCESSED_COLUMN);
LOGGER.debug(sql);

final Blob blob = connection.createBlob();
blobOutputStream = blob.setBinaryStream(1);
statement = connection.prepareStatement(sql);
statement.setString(1, ops.toString());
statement.setBlob(2, blob);
statement.setTimestamp(3, now());
}

@Override
public void close() throws IOException {
LOGGER.debug("Closing stream for {}", ops);
PreparedStatement statement = null;
try {
if (isCompletelyWritten()) {
final Configuration config = Configuration.getInstance();
final String sql = String.format(
"INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)",
config.getString(Key.JDBCCACHE_DERIVATIVE_IMAGE_TABLE),
DERIVATIVE_IMAGE_TABLE_OPERATIONS_COLUMN,
DERIVATIVE_IMAGE_TABLE_IMAGE_COLUMN,
DERIVATIVE_IMAGE_TABLE_LAST_ACCESSED_COLUMN);
LOGGER.debug(sql);
statement = connection.prepareStatement(sql);
statement.setString(1, ops.toString());
statement.setBlob(2, blob);
statement.setTimestamp(3, now());
statement.executeUpdate();
connection.commit();
} else {
Expand All @@ -102,7 +101,9 @@ public void close() throws IOException {
throw new IOException(e.getMessage(), e);
} finally {
try {
statement.close();
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
Expand Down Expand Up @@ -390,6 +391,7 @@ public InputStream newDerivativeImageInputStream(OperationList opList)
try {
return new ImageBlobOutputStream(getConnection(), ops);
} catch (SQLException e) {
LOGGER.error("Throwing Except: {}", e);
throw new IOException(e.getMessage(), e);
}
}
Expand Down

0 comments on commit 85a993a

Please sign in to comment.