3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-12 10:15:49 +00:00

More HSQLDB debugging

This commit is contained in:
catbref 2019-04-26 08:07:57 +01:00
parent 0296c4bbb1
commit b7687bf326
2 changed files with 36 additions and 24 deletions

View File

@ -177,8 +177,10 @@ public class HSQLDBRepository implements Repository {
boolean inTransaction = resultSet.getBoolean(1); boolean inTransaction = resultSet.getBoolean(1);
int transactionCount = resultSet.getInt(2); int transactionCount = resultSet.getInt(2);
if (inTransaction && transactionCount != 0) if (inTransaction && transactionCount != 0) {
LOGGER.warn("Uncommitted changes (" + transactionCount + ") during repository close", new Exception("Uncommitted repository changes")); LOGGER.warn("Uncommitted changes (" + transactionCount + ") during repository close", new Exception("Uncommitted repository changes"));
logStatements();
}
} }
// give connection back to the pool // give connection back to the pool
@ -198,6 +200,32 @@ public class HSQLDBRepository implements Repository {
this.debugState = debugState; this.debugState = debugState;
} }
/**
* Returns prepared statement using passed SQL, logging query if necessary.
*/
public PreparedStatement prepareStatement(String sql) throws SQLException {
if (this.debugState)
LOGGER.debug(sql);
PreparedStatement preparedStatement = this.connection.prepareStatement(sql);
if (this.queries != null)
this.queries.add(sql);
return preparedStatement;
}
/**
* Logs this transaction's SQL statements, if enabled.
*/
public void logStatements() {
if (this.queries == null)
return;
for (String query : this.queries)
LOGGER.info(query);
}
/** /**
* Execute SQL and return ResultSet with but added checking. * Execute SQL and return ResultSet with but added checking.
* <p> * <p>
@ -210,10 +238,7 @@ public class HSQLDBRepository implements Repository {
*/ */
@SuppressWarnings("resource") @SuppressWarnings("resource")
public ResultSet checkedExecute(String sql, Object... objects) throws SQLException { public ResultSet checkedExecute(String sql, Object... objects) throws SQLException {
if (this.debugState) PreparedStatement preparedStatement = this.prepareStatement(sql);
LOGGER.debug(sql);
PreparedStatement preparedStatement = this.connection.prepareStatement(sql);
// Close the PreparedStatement when the ResultSet is closed otherwise there's a potential resource leak. // Close the PreparedStatement when the ResultSet is closed otherwise there's a potential resource leak.
// We can't use try-with-resources here as closing the PreparedStatement on return would also prematurely close the ResultSet. // We can't use try-with-resources here as closing the PreparedStatement on return would also prematurely close the ResultSet.
@ -227,14 +252,9 @@ public class HSQLDBRepository implements Repository {
if (this.slowQueryThreshold != null && queryTime > this.slowQueryThreshold) { if (this.slowQueryThreshold != null && queryTime > this.slowQueryThreshold) {
LOGGER.info(String.format("HSQLDB query took %d ms: %s", queryTime, sql)); LOGGER.info(String.format("HSQLDB query took %d ms: %s", queryTime, sql));
if (this.queries != null) logStatements();
for (String query : this.queries)
LOGGER.info(query);
} }
if (this.queries != null)
this.queries.add(sql);
return resultSet; return resultSet;
} }
@ -315,6 +335,7 @@ public class HSQLDBRepository implements Repository {
* @throws SQLException * @throws SQLException
*/ */
public Long callIdentity() throws SQLException { public Long callIdentity() throws SQLException {
// We don't need to use HSQLDBRepository.prepareStatement for this as it's so trivial
try (PreparedStatement preparedStatement = this.connection.prepareStatement("CALL IDENTITY()"); try (PreparedStatement preparedStatement = this.connection.prepareStatement("CALL IDENTITY()");
ResultSet resultSet = this.checkedExecuteResultSet(preparedStatement)) { ResultSet resultSet = this.checkedExecuteResultSet(preparedStatement)) {
if (resultSet == null) if (resultSet == null)
@ -344,11 +365,8 @@ public class HSQLDBRepository implements Repository {
public boolean exists(String tableName, String whereClause, Object... objects) throws SQLException { public boolean exists(String tableName, String whereClause, Object... objects) throws SQLException {
String sql = "SELECT TRUE FROM " + tableName + " WHERE " + whereClause + " LIMIT 1"; String sql = "SELECT TRUE FROM " + tableName + " WHERE " + whereClause + " LIMIT 1";
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql); try (PreparedStatement preparedStatement = this.prepareStatement(sql);
ResultSet resultSet = this.checkedExecuteResultSet(preparedStatement, objects)) { ResultSet resultSet = this.checkedExecuteResultSet(preparedStatement, objects)) {
if (this.queries != null)
this.queries.add(sql);
if (resultSet == null) if (resultSet == null)
return false; return false;
@ -367,10 +385,7 @@ public class HSQLDBRepository implements Repository {
public int delete(String tableName, String whereClause, Object... objects) throws SQLException { public int delete(String tableName, String whereClause, Object... objects) throws SQLException {
String sql = "DELETE FROM " + tableName + " WHERE " + whereClause; String sql = "DELETE FROM " + tableName + " WHERE " + whereClause;
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) { try (PreparedStatement preparedStatement = this.prepareStatement(sql)) {
if (this.queries != null)
this.queries.add(sql);
return this.checkedExecuteUpdateCount(preparedStatement, objects); return this.checkedExecuteUpdateCount(preparedStatement, objects);
} }
} }
@ -384,10 +399,7 @@ public class HSQLDBRepository implements Repository {
public int delete(String tableName) throws SQLException { public int delete(String tableName) throws SQLException {
String sql = "DELETE FROM " + tableName; String sql = "DELETE FROM " + tableName;
try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) { try (PreparedStatement preparedStatement = this.prepareStatement(sql)) {
if (this.queries != null)
this.queries.add(sql);
return this.checkedExecuteUpdateCount(preparedStatement); return this.checkedExecuteUpdateCount(preparedStatement);
} }
} }

View File

@ -58,7 +58,7 @@ public class HSQLDBSaver {
*/ */
public boolean execute(HSQLDBRepository repository) throws SQLException { public boolean execute(HSQLDBRepository repository) throws SQLException {
String sql = this.formatInsertWithPlaceholders(); String sql = this.formatInsertWithPlaceholders();
try (PreparedStatement preparedStatement = repository.connection.prepareStatement(sql)) { try (PreparedStatement preparedStatement = repository.prepareStatement(sql)) {
this.bindValues(preparedStatement); this.bindValues(preparedStatement);
return preparedStatement.execute(); return preparedStatement.execute();