Search in sources :

Example 36 with ISqlTransaction

use of org.jumpmind.db.sql.ISqlTransaction in project symmetric-ds by JumpMind.

the class OutgoingBatchService method updateOutgoingBatch.

public void updateOutgoingBatch(OutgoingBatch outgoingBatch) {
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        updateOutgoingBatch(transaction, outgoingBatch);
        transaction.commit();
    } catch (Error ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } catch (RuntimeException ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } finally {
        close(transaction);
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction)

Example 37 with ISqlTransaction

use of org.jumpmind.db.sql.ISqlTransaction in project symmetric-ds by JumpMind.

the class RegistrationService method markNodeAsRegistered.

/**
 * @see IRegistrationService#markNodeAsRegistered(Node)
 */
public void markNodeAsRegistered(String nodeId) {
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        symmetricDialect.disableSyncTriggers(transaction, nodeId);
        transaction.prepareAndExecute(getSql("registerNodeSecuritySql"), nodeId);
        transaction.commit();
    } catch (Error ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } catch (RuntimeException ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } finally {
        symmetricDialect.enableSyncTriggers(transaction);
        close(transaction);
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction)

Example 38 with ISqlTransaction

use of org.jumpmind.db.sql.ISqlTransaction in project symmetric-ds by JumpMind.

the class IncomingBatchService method insertIncomingBatch.

public void insertIncomingBatch(IncomingBatch batch) {
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        insertIncomingBatch(transaction, batch);
        transaction.commit();
    } catch (Error ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } catch (RuntimeException ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } finally {
        close(transaction);
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction)

Example 39 with ISqlTransaction

use of org.jumpmind.db.sql.ISqlTransaction in project symmetric-ds by JumpMind.

the class Db2v9SymmetricDialect method createRequiredDatabaseObjects.

@Override
public void createRequiredDatabaseObjects() {
    ISqlTransaction transaction = null;
    try {
        transaction = platform.getSqlTemplate().startSqlTransaction();
        enableSyncTriggers(transaction);
        transaction.commit();
    } catch (Exception e) {
        try {
            log.info("Creating environment variables {} and {}", syncTriggersDisabledUserVariable, syncTriggersDisabledNodeVariable);
            ISqlTemplate template = getPlatform().getSqlTemplate();
            template.update("CREATE VARIABLE " + syncTriggersDisabledNodeVariable + " VARCHAR(50)");
            template.update("GRANT READ on VARIABLE " + syncTriggersDisabledNodeVariable + " TO PUBLIC");
            template.update("GRANT WRITE on VARIABLE " + syncTriggersDisabledNodeVariable + " TO PUBLIC");
            template.update("CREATE VARIABLE " + syncTriggersDisabledUserVariable + " INTEGER");
            template.update("GRANT READ on VARIABLE " + syncTriggersDisabledUserVariable + " TO PUBLIC");
            template.update("GRANT WRITE on VARIABLE " + syncTriggersDisabledUserVariable + " TO PUBLIC");
        } catch (Exception ex) {
            log.error("Error while initializing DB2 dialect", ex);
        }
    } finally {
        close(transaction);
    }
    super.createRequiredDatabaseObjects();
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate)

Example 40 with ISqlTransaction

use of org.jumpmind.db.sql.ISqlTransaction in project symmetric-ds by JumpMind.

the class DataGapDetector method beforeRouting.

/**
 * Always make sure sym_data_gap is up to date to make sure that we don't
 * dual route data.
 */
public void beforeRouting() {
    long printStats = System.currentTimeMillis();
    ProcessInfo processInfo = this.statisticManager.newProcessInfo(new ProcessInfoKey(nodeService.findIdentityNodeId(), null, ProcessType.GAP_DETECT));
    try {
        long ts = System.currentTimeMillis();
        processInfo.setStatus(Status.QUERYING);
        final List<DataGap> gaps = dataService.findDataGaps();
        long lastDataId = -1;
        final int dataIdIncrementBy = parameterService.getInt(ParameterConstants.DATA_ID_INCREMENT_BY);
        final long maxDataToSelect = parameterService.getInt(ParameterConstants.ROUTING_LARGEST_GAP_SIZE);
        long databaseTime = symmetricDialect.getDatabaseTime();
        int idsFilled = 0;
        int newGapsInserted = 0;
        int rangeChecked = 0;
        int gapsDeleted = 0;
        for (final DataGap dataGap : gaps) {
            final boolean lastGap = dataGap.equals(gaps.get(gaps.size() - 1));
            String sql = routerService.getSql("selectDistinctDataIdFromDataEventUsingGapsSql");
            ISqlTemplate sqlTemplate = symmetricDialect.getPlatform().getSqlTemplate();
            Object[] params = new Object[] { dataGap.getStartId(), dataGap.getEndId() };
            lastDataId = -1;
            processInfo.setStatus(Status.QUERYING);
            long queryForIdsTs = System.currentTimeMillis();
            List<Number> ids = sqlTemplate.query(sql, new NumberMapper(), params);
            if (System.currentTimeMillis() - queryForIdsTs > Constants.LONG_OPERATION_THRESHOLD) {
                log.info("It took longer than {}ms to run the following sql for gap from {} to {}.  {}", new Object[] { Constants.LONG_OPERATION_THRESHOLD, dataGap.getStartId(), dataGap.getEndId(), sql });
            }
            processInfo.setStatus(Status.PROCESSING);
            idsFilled += ids.size();
            rangeChecked += dataGap.getEndId() - dataGap.getStartId();
            ISqlTransaction transaction = null;
            try {
                transaction = sqlTemplate.startSqlTransaction();
                for (Number number : ids) {
                    long dataId = number.longValue();
                    processInfo.incrementCurrentDataCount();
                    if (lastDataId == -1 && dataGap.getStartId() + dataIdIncrementBy <= dataId) {
                        // there was a new gap at the start
                        dataService.insertDataGap(transaction, new DataGap(dataGap.getStartId(), dataId - 1));
                        newGapsInserted++;
                    } else if (lastDataId != -1 && lastDataId + dataIdIncrementBy != dataId && lastDataId != dataId) {
                        // found a gap somewhere in the existing gap
                        dataService.insertDataGap(transaction, new DataGap(lastDataId + 1, dataId - 1));
                        newGapsInserted++;
                    }
                    lastDataId = dataId;
                }
                /* if we found data in the gap */
                if (lastDataId != -1) {
                    if (!lastGap && lastDataId + dataIdIncrementBy <= dataGap.getEndId()) {
                        dataService.insertDataGap(transaction, new DataGap(lastDataId + dataIdIncrementBy, dataGap.getEndId()));
                        newGapsInserted++;
                    }
                    dataService.deleteDataGap(transaction, dataGap);
                    gapsDeleted++;
                /*
                     * if we did not find data in the gap and it was not the
                     * last gap
                     */
                } else if (!lastGap) {
                    if (dataService.countDataInRange(dataGap.getStartId() - 1, dataGap.getEndId() + 1) == 0) {
                        if (symmetricDialect.supportsTransactionViews()) {
                            long transactionViewClockSyncThresholdInMs = parameterService.getLong(ParameterConstants.DBDIALECT_ORACLE_TRANSACTION_VIEW_CLOCK_SYNC_THRESHOLD_MS, 60000);
                            Date createTime = dataService.findCreateTimeOfData(dataGap.getEndId() + 1);
                            if (createTime != null && !symmetricDialect.areDatabaseTransactionsPendingSince(createTime.getTime() + transactionViewClockSyncThresholdInMs)) {
                                if (dataService.countDataInRange(dataGap.getStartId() - 1, dataGap.getEndId() + 1) == 0) {
                                    if (dataGap.getStartId() == dataGap.getEndId()) {
                                        log.info("Found a gap in data_id at {}.  Skipping it because there are no pending transactions in the database", dataGap.getStartId());
                                    } else {
                                        log.info("Found a gap in data_id from {} to {}.  Skipping it because there are no pending transactions in the database", dataGap.getStartId(), dataGap.getEndId());
                                    }
                                    dataService.deleteDataGap(transaction, dataGap);
                                    gapsDeleted++;
                                }
                            }
                        } else if (isDataGapExpired(dataGap.getEndId() + 1, databaseTime)) {
                            if (dataGap.getStartId() == dataGap.getEndId()) {
                                log.info("Found a gap in data_id at {}.  Skipping it because the gap expired", dataGap.getStartId());
                            } else {
                                log.info("Found a gap in data_id from {} to {}.  Skipping it because the gap expired", dataGap.getStartId(), dataGap.getEndId());
                            }
                            dataService.deleteDataGap(transaction, dataGap);
                            gapsDeleted++;
                        }
                    }
                }
                if (System.currentTimeMillis() - printStats > 30000) {
                    log.info("The data gap detection process has been running for {}ms, detected {} rows that have been previously routed over a total gap range of {}, " + "inserted {} new gaps, and deleted {} gaps", new Object[] { System.currentTimeMillis() - ts, idsFilled, rangeChecked, newGapsInserted, gapsDeleted });
                    printStats = System.currentTimeMillis();
                }
                transaction.commit();
            } catch (Error ex) {
                if (transaction != null) {
                    transaction.rollback();
                }
                throw ex;
            } catch (RuntimeException ex) {
                if (transaction != null) {
                    transaction.rollback();
                }
                throw ex;
            } finally {
                if (transaction != null) {
                    transaction.close();
                }
            }
        }
        if (lastDataId != -1) {
            dataService.insertDataGap(new DataGap(lastDataId + 1, lastDataId + maxDataToSelect));
        }
        long updateTimeInMs = System.currentTimeMillis() - ts;
        if (updateTimeInMs > 10000) {
            log.info("Detecting gaps took {} ms", updateTimeInMs);
        }
        processInfo.setStatus(Status.OK);
    } catch (RuntimeException ex) {
        processInfo.setStatus(Status.ERROR);
        throw ex;
    }
}
Also used : NumberMapper(org.jumpmind.db.sql.mapper.NumberMapper) ProcessInfoKey(org.jumpmind.symmetric.model.ProcessInfoKey) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) Date(java.util.Date) DataGap(org.jumpmind.symmetric.model.DataGap) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction)

Aggregations

ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)53 Node (org.jumpmind.symmetric.model.Node)7 ArrayList (java.util.ArrayList)6 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)6 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)6 List (java.util.List)5 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)5 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)5 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)5 SymmetricException (org.jumpmind.symmetric.SymmetricException)4 Trigger (org.jumpmind.symmetric.model.Trigger)4 Date (java.util.Date)3 Table (org.jumpmind.db.model.Table)3 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)3 Data (org.jumpmind.symmetric.model.Data)3 DataGap (org.jumpmind.symmetric.model.DataGap)3 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)3 INodeService (org.jumpmind.symmetric.service.INodeService)3 Test (org.junit.Test)3 DatabaseInfo (org.jumpmind.db.platform.DatabaseInfo)2