Move logSQL calls into finally blocks, to happen upon success or exception.

This commit is contained in:
2024-07-08 10:20:45 -05:00
parent 385f4c20e5
commit bce9af06fb
6 changed files with 56 additions and 34 deletions

View File

@ -168,8 +168,10 @@ public class RDBMSAggregateAction extends AbstractRDBMSAction implements Aggrega
}), params);
}
logSQL(sql, params, mark);
finally
{
logSQL(sql, params, mark);
}
return rs;
}

View File

@ -84,10 +84,10 @@ public class RDBMSCountAction extends AbstractRDBMSAction implements CountInterf
setSqlAndJoinsInQueryStat(sql, joinsContext);
CountOutput rs = new CountOutput();
long mark = System.currentTimeMillis();
try(Connection connection = getConnection(countInput))
{
long mark = System.currentTimeMillis();
statement = connection.prepareStatement(sql);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -116,7 +116,9 @@ public class RDBMSCountAction extends AbstractRDBMSAction implements CountInterf
setQueryStatFirstResultTime();
}), params);
}
finally
{
logSQL(sql, params, mark);
}

View File

@ -212,13 +212,16 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
// LOG.debug("rowCount 0 trying to delete [" + tableName + "][" + primaryKey + "]");
// deleteOutput.addRecordWithError(new QRecord(table, primaryKey).withError("Record was not deleted (but no error was given from the database)"));
// }
logSQL(sql, List.of(primaryKey), mark);
}
catch(Exception e)
{
LOG.debug("Exception trying to delete [" + tableName + "][" + primaryKey + "]", e);
deleteOutput.addRecordWithError(new QRecord(table, primaryKey).withError(new SystemErrorStatusMessage("Record was not deleted: " + e.getMessage())));
}
finally
{
logSQL(sql, List.of(primaryKey), mark);
}
}
@ -228,13 +231,14 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
*******************************************************************************/
public void doDeleteList(Connection connection, QTableMetaData table, List<Serializable> primaryKeys, DeleteOutput deleteOutput) throws QException
{
long mark = System.currentTimeMillis();
String sql = null;
try
{
long mark = System.currentTimeMillis();
String tableName = getTableName(table);
String primaryKeyName = getColumnName(table.getField(table.getPrimaryKeyField()));
String sql = "DELETE FROM "
sql = "DELETE FROM "
+ escapeIdentifier(tableName)
+ " WHERE "
+ escapeIdentifier(primaryKeyName)
@ -246,13 +250,15 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
Integer rowCount = QueryManager.executeUpdateForRowCount(connection, sql, primaryKeys);
deleteOutput.addToDeletedRecordCount(rowCount);
logSQL(sql, primaryKeys, mark);
}
catch(Exception e)
{
throw new QException("Error executing delete: " + e.getMessage(), e);
}
finally
{
logSQL(sql, primaryKeys, mark);
}
}
@ -282,12 +288,14 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
{
int rowCount = QueryManager.executeUpdateForRowCount(connection, sql, params);
deleteOutput.setDeletedRecordCount(rowCount);
logSQL(sql, params, mark);
}
catch(Exception e)
{
throw new QException("Error executing delete with filter: " + e.getMessage(), e);
}
finally
{
logSQL(sql, params, mark);
}
}
}

View File

@ -57,9 +57,13 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
InsertOutput rs = new InsertOutput();
QTableMetaData table = insertInput.getTable();
Connection connection = null;
Connection connection = null;
boolean needToCloseConnection = false;
StringBuilder sql = null;
List<Object> params = null;
Long mark = null;
try
{
List<QFieldMetaData> insertableFields = table.getFields().values().stream()
@ -88,10 +92,10 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
for(List<QRecord> page : CollectionUtils.getPages(insertInput.getRecords(), QueryManager.PAGE_SIZE))
{
String tableName = escapeIdentifier(getTableName(table));
StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append("(").append(columns).append(") VALUES");
List<Object> params = new ArrayList<>();
int recordIndex = 0;
String tableName = escapeIdentifier(getTableName(table));
sql = new StringBuilder("INSERT INTO ").append(tableName).append("(").append(columns).append(") VALUES");
params = new ArrayList<>();
int recordIndex = 0;
//////////////////////////////////////////////////////
// for each record in the page: //
@ -133,7 +137,7 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
continue;
}
Long mark = System.currentTimeMillis();
mark = System.currentTimeMillis();
///////////////////////////////////////////////////////////
// execute the insert, then foreach record in the input, //
@ -163,6 +167,7 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
}
catch(Exception e)
{
logSQL(sql, params, mark);
throw new QException("Error executing insert: " + e.getMessage(), e);
}
finally

View File

@ -223,17 +223,12 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
}), params);
logSQL(sql, params, mark);
return queryOutput;
}
catch(Exception e)
{
logSQL(sql, params, mark);
throw (e);
}
finally
{
logSQL(sql, params, mark);
if(actionTimeoutHelper != null)
{
/////////////////////////////////////////

View File

@ -179,10 +179,15 @@ public class RDBMSUpdateAction extends AbstractRDBMSAction implements UpdateInte
////////////////////////////////////////////////////////////////////////////////
// let query manager do the batch updates - note that it will internally page //
////////////////////////////////////////////////////////////////////////////////
QueryManager.executeBatchUpdate(connection, sql, values);
incrementStatus(updateInput, recordList.size());
logSQL(sql, values, mark);
try
{
QueryManager.executeBatchUpdate(connection, sql, values);
incrementStatus(updateInput, recordList.size());
}
finally
{
logSQL(sql, values, mark);
}
}
@ -249,10 +254,15 @@ public class RDBMSUpdateAction extends AbstractRDBMSAction implements UpdateInte
/////////////////////////////////////
// let query manager do the update //
/////////////////////////////////////
QueryManager.executeUpdate(connection, sql, params);
incrementStatus(updateInput, page.size());
logSQL(sql, params, mark);
try
{
QueryManager.executeUpdate(connection, sql, params);
incrementStatus(updateInput, page.size());
}
finally
{
logSQL(sql, params, mark);
}
}
}