Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UpdateDateTracker.java: Remove deprecated finalize method #3647

Merged
merged 7 commits into from
May 14, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 51 additions & 55 deletions import/index_java/src/org/vufind/index/UpdateDateTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public class UpdateDateTracker
private Timestamp lastRecordChange;
private Timestamp deleted;

PreparedStatement insertSql;
PreparedStatement selectSql;
PreparedStatement updateSql;

private static ThreadLocal<UpdateDateTracker> trackerCache =
new ThreadLocal<UpdateDateTracker>()
{
Expand Down Expand Up @@ -69,37 +65,49 @@ private void createRow(Timestamp newRecordChange) throws SQLException
lastRecordChange = newRecordChange;

// Save new values to the database:
insertSql.setString(1, core);
insertSql.setString(2, id);
insertSql.setTimestamp(3, firstIndexed);
insertSql.setTimestamp(4, lastIndexed);
insertSql.setTimestamp(5, lastRecordChange);
insertSql.executeUpdate();
try (
PreparedStatement insertSql = db.prepareStatement(
"INSERT INTO change_tracker(core, id, first_indexed, last_indexed, last_record_change) " +
"VALUES(?, ?, ?, ?, ?);"
);
) {
insertSql.setString(1, core);
insertSql.setString(2, id);
insertSql.setTimestamp(3, firstIndexed);
insertSql.setTimestamp(4, lastIndexed);
insertSql.setTimestamp(5, lastRecordChange);
insertSql.executeUpdate();
}
}

/* Private support method: read a row from the change_tracker table.
*/
private boolean readRow() throws SQLException
{
selectSql.setString(1, core);
selectSql.setString(2, id);
ResultSet result = selectSql.executeQuery();

// No results? Free resources and return false:
if (!result.first()) {
result.close();
return false;
boolean returnValue = true;
try (
PreparedStatement selectSql = db.prepareStatement(
"SELECT first_indexed, last_indexed, last_record_change, deleted " +
"FROM change_tracker WHERE core = ? AND id = ?;",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY
)
) {
selectSql.setString(1, core);
selectSql.setString(2, id);
try (ResultSet result = selectSql.executeQuery()) {
// No results? Return false:
if (!result.first()) {
returnValue = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you should still be safe just calling return false here; the close would still work. https://stackoverflow.com/questions/22947755/try-with-resources-and-return-statements-in-java

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've simplified as you suggested and it still seems to be working as expected for me.

} else {
// If we got this far, we have results -- load them into the object:
firstIndexed = result.getTimestamp(1);
lastIndexed = result.getTimestamp(2);
lastRecordChange = result.getTimestamp(3);
deleted = result.getTimestamp(4);
}
}
}

// If we got this far, we have results -- load them into the object:
firstIndexed = result.getTimestamp(1);
lastIndexed = result.getTimestamp(2);
lastRecordChange = result.getTimestamp(3);
deleted = result.getTimestamp(4);

// Free resources and report success:
result.close();
return true;
return returnValue;
}

/* Private support method: update a row in the change_tracker table.
Expand All @@ -116,40 +124,28 @@ private void updateRow(Timestamp newRecordChange) throws SQLException
lastRecordChange = newRecordChange;

// Save new values to the database:
updateSql.setTimestamp(1, firstIndexed);
updateSql.setTimestamp(2, lastIndexed);
updateSql.setTimestamp(3, lastRecordChange);
updateSql.setNull(4, java.sql.Types.NULL);
updateSql.setString(5, core);
updateSql.setString(6, id);
updateSql.executeUpdate();
try (
PreparedStatement updateSql = db.prepareStatement(
"UPDATE change_tracker " +
"SET first_indexed = ?, last_indexed = ?, last_record_change = ?, deleted = ? " +
"WHERE core = ? AND id = ?;"
)
) {
updateSql.setTimestamp(1, firstIndexed);
updateSql.setTimestamp(2, lastIndexed);
updateSql.setTimestamp(3, lastRecordChange);
updateSql.setNull(4, java.sql.Types.NULL);
updateSql.setString(5, core);
updateSql.setString(6, id);
updateSql.executeUpdate();
}
}

/* Constructor:
*/
public UpdateDateTracker(Connection dbConnection) throws SQLException
{
db = dbConnection;
insertSql = db.prepareStatement(
"INSERT INTO change_tracker(core, id, first_indexed, last_indexed, last_record_change) " +
"VALUES(?, ?, ?, ?, ?);");
selectSql = db.prepareStatement(
"SELECT first_indexed, last_indexed, last_record_change, deleted " +
"FROM change_tracker WHERE core = ? AND id = ?;",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
updateSql = db.prepareStatement("UPDATE change_tracker " +
"SET first_indexed = ?, last_indexed = ?, last_record_change = ?, deleted = ? " +
"WHERE core = ? AND id = ?;");
}

/* Finalizer
*/
protected void finalize() throws SQLException, Throwable
{
insertSql.close();
selectSql.close();
updateSql.close();
super.finalize();
}

/* Get the first indexed date (IMPORTANT: index() must be called before this method)
Expand Down
Loading