use of com.evolveum.midpoint.repo.sql.util.TemporaryTableDialect in project midpoint by Evolveum.
the class SqlAuditServiceImpl method createTemporaryTable.
/**
* This method creates temporary table for cleanup audit method.
*/
private void createTemporaryTable(JdbcSession jdbcSession, final String tempTable) {
// check if table exists
if (!sqlConfiguration().isUsingPostgreSQL()) {
try {
jdbcSession.executeStatement("select id from " + tempTable + " where id = 1");
// table already exists
return;
} catch (Exception ex) {
// we expect this on the first time
}
}
TemporaryTableDialect ttDialect = TemporaryTableDialect.getTempTableDialect(sqlConfiguration().getDatabaseType());
jdbcSession.executeStatement(ttDialect.getCreateTemporaryTableString() + ' ' + tempTable + " (id " + jdbcSession.getNativeTypeName(Types.BIGINT) + " not null)" + ttDialect.getCreateTemporaryTablePostfix());
}
use of com.evolveum.midpoint.repo.sql.util.TemporaryTableDialect in project midpoint by Evolveum.
the class SqlAuditServiceImpl method batchDeletionAttempt.
/**
* Deletes one batch of records using recordsSelector to select records
* according to particular cleanup policy.
*/
private int batchDeletionAttempt(BiFunction<JdbcSession, String, Integer> recordsSelector, Holder<Integer> totalCountHolder, long batchStart, OperationResult parentResult) {
OperationResult result = parentResult.createSubresult("batchDeletionAttempt");
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try {
TemporaryTableDialect ttDialect = TemporaryTableDialect.getTempTableDialect(sqlConfiguration().getDatabaseType());
// create temporary table
final String tempTable = ttDialect.generateTemporaryTableName(QAuditEventRecord.TABLE_NAME);
createTemporaryTable(jdbcSession, tempTable);
LOGGER.trace("Created temporary table '{}'.", tempTable);
int count = recordsSelector.apply(jdbcSession, tempTable);
LOGGER.trace("Inserted {} audit record ids ready for deleting.", count);
// drop records from m_audit_item, m_audit_event, m_audit_delta, and others
jdbcSession.executeStatement(createDeleteQuery(QAuditItem.TABLE_NAME, tempTable, QAuditItem.RECORD_ID));
jdbcSession.executeStatement(createDeleteQuery(QAuditDelta.TABLE_NAME, tempTable, QAuditDelta.RECORD_ID));
jdbcSession.executeStatement(createDeleteQuery(QAuditPropertyValue.TABLE_NAME, tempTable, QAuditPropertyValue.RECORD_ID));
jdbcSession.executeStatement(createDeleteQuery(QAuditRefValue.TABLE_NAME, tempTable, QAuditRefValue.RECORD_ID));
jdbcSession.executeStatement(createDeleteQuery(QAuditResource.TABLE_NAME, tempTable, QAuditResource.RECORD_ID));
jdbcSession.executeStatement(createDeleteQuery(QAuditEventRecord.TABLE_NAME, tempTable, QAuditEventRecord.ID));
// drop temporary table
if (ttDialect.dropTemporaryTableAfterUse()) {
LOGGER.debug("Dropping temporary table.");
jdbcSession.executeStatement(ttDialect.getDropTemporaryTableString() + ' ' + tempTable);
}
jdbcSession.commit();
// commit would happen automatically, but if it fails, we don't change the numbers
int totalCount = totalCountHolder.getValue() + count;
totalCountHolder.setValue(totalCount);
LOGGER.debug("Audit cleanup batch finishing successfully in {} milliseconds; total count = {}", System.currentTimeMillis() - batchStart, totalCount);
return count;
} catch (RuntimeException ex) {
LOGGER.debug("Audit cleanup batch finishing with exception in {} milliseconds; exception = {}", System.currentTimeMillis() - batchStart, ex.getMessage());
baseHelper.handleGeneralRuntimeException(ex, jdbcSession, result);
throw new AssertionError("We shouldn't get here.");
}
} catch (Throwable t) {
result.recordFatalError(t);
throw t;
} finally {
result.computeStatusIfUnknown();
}
}
Aggregations