Search in sources :

Example 1 with TemporaryTableDialect

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());
}
Also used : TemporaryTableDialect(com.evolveum.midpoint.repo.sql.util.TemporaryTableDialect) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 2 with TemporaryTableDialect

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();
    }
}
Also used : TemporaryTableDialect(com.evolveum.midpoint.repo.sql.util.TemporaryTableDialect) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Aggregations

TemporaryTableDialect (com.evolveum.midpoint.repo.sql.util.TemporaryTableDialect)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1