Skip to content

Commit bf7e428

Browse files
authored
Log failed PG queries (#255)
1 parent 1e7c791 commit bf7e428

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/PostgresQueryExecutor.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@ static org.hypertrace.core.documentstore.query.Query transformAndLog(
2626
protected ResultSet execute(final Connection connection, PostgresQueryParser queryParser)
2727
throws SQLException {
2828
final String sqlQuery = queryParser.parse();
29-
final PreparedStatement preparedStatement =
30-
buildPreparedStatement(sqlQuery, queryParser.getParamsBuilder().build(), connection);
31-
log.debug("Executing executeQueryV1 sqlQuery:{}", preparedStatement.toString());
32-
return preparedStatement.executeQuery();
29+
final Params params = queryParser.getParamsBuilder().build();
30+
// this is closed when the corresponding ResultSet is closed in the iterators
31+
PreparedStatement preparedStatement = buildPreparedStatement(sqlQuery, params, connection);
32+
try {
33+
log.debug("Executing SQL query: {}", sqlQuery);
34+
return preparedStatement.executeQuery();
35+
} catch (SQLException e) {
36+
log.error(
37+
"SQL execution failed. Query: {}, SQLState: {}, ErrorCode: {}",
38+
sqlQuery,
39+
e.getSQLState(),
40+
e.getErrorCode(),
41+
e);
42+
throw e;
43+
}
3344
}
3445

3546
public PreparedStatement buildPreparedStatement(
36-
String sqlQuery, Params params, Connection connection) throws SQLException, RuntimeException {
47+
String sqlQuery, Params params, Connection connection) throws SQLException {
3748
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery);
3849
enrichPreparedStatementWithParams(preparedStatement, params);
3950
return preparedStatement;

0 commit comments

Comments
 (0)