Search in sources :

Example 26 with ISqlTransaction

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

the class AbstractTriggerRouterServiceTest method test10DisableTriggers.

@Test
public void test10DisableTriggers() throws Exception {
    ISymmetricDialect dbDialect = getDbDialect();
    ISqlTemplate jdbcTemplate = getSqlTemplate();
    ISqlTransaction transaction = jdbcTemplate.startSqlTransaction();
    try {
        dbDialect.disableSyncTriggers(transaction);
        int count = insert(INSERT1_VALUES, transaction, dbDialect);
        dbDialect.enableSyncTriggers(transaction);
        transaction.commit();
        assertTrue(count == 1);
        String csvString = getNextDataRow();
        // DB2 captures decimal differently
        csvString = csvString.replaceFirst("\"00001\\.\"", "\"1\"");
        // Informix captures decimal differently
        csvString = csvString.replaceFirst("\"1.0000000000000000\"", "\"1\"");
        // ASA captures decimal differently
        csvString = csvString.replaceFirst("\"1.000000\"", "\"1\"");
        boolean match = csvString.endsWith(EXPECTED_INSERT2_CSV_ENDSWITH);
        assertTrue(match, "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT2_CSV_ENDSWITH);
    } finally {
        transaction.close();
    }
}
Also used : ISymmetricDialect(org.jumpmind.symmetric.db.ISymmetricDialect) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Test(org.junit.Test)

Example 27 with ISqlTransaction

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

the class AbstractTriggerRouterServiceTest method test12BinaryColumnTypesForPostgres.

@Test
public void test12BinaryColumnTypesForPostgres() {
    ISymmetricDialect dialect = getDbDialect();
    if (DatabaseNamesConstants.POSTGRESQL.equals(dialect.getName())) {
        getSqlTemplate().update(DROP_POSTGRES_BINARY_TYPE);
        getSqlTemplate().update(CREATE_POSTGRES_BINARY_TYPE);
        TriggerRouter trouter = new TriggerRouter();
        Trigger trigger = trouter.getTrigger();
        trigger.setSourceTableName("test_postgres_binary_types");
        trigger.setChannelId(TestConstants.TEST_CHANNEL_ID);
        Router router = trouter.getRouter();
        router.getNodeGroupLink().setSourceNodeGroupId(TestConstants.TEST_ROOT_NODE_GROUP);
        router.getNodeGroupLink().setTargetNodeGroupId(TestConstants.TEST_ROOT_NODE_GROUP);
        getTriggerRouterService().saveTriggerRouter(trouter);
        ITriggerRouterService triggerService = getTriggerRouterService();
        triggerService.syncTriggers();
        Assert.assertEquals("Some triggers must have failed to build.", 0, triggerService.getFailedTriggers().size());
        // new SerialBlob("test 1 2 3".getBytes())
        ISqlTransaction transaction = getSqlTemplate().startSqlTransaction();
        try {
            getSqlTemplate().update(INSERT_POSTGRES_BINARY_TYPE_1, "test 1 2 3".getBytes());
            transaction.commit();
        } finally {
            transaction.close();
        }
        String csvString = getNextDataRow();
        Assert.assertEquals(EXPECTED_INSERT_POSTGRES_BINARY_TYPE_1, csvString);
    }
}
Also used : ISymmetricDialect(org.jumpmind.symmetric.db.ISymmetricDialect) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Router(org.jumpmind.symmetric.model.Router) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Test(org.junit.Test)

Example 28 with ISqlTransaction

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

the class SQLDatabaseWriterFilter method processLoadFilters.

@Override
protected boolean processLoadFilters(DataContext context, Table table, CsvData data, Exception error, WriteMethod writeMethod, List<LoadFilter> loadFiltersForTable) {
    boolean writeRow = true;
    LoadFilter currentFilter = null;
    List<Boolean> values = null;
    try {
        LinkedCaseInsensitiveMap<Object> namedParams = null;
        for (LoadFilter filter : loadFiltersForTable) {
            currentFilter = filter;
            values = null;
            if (filter.isFilterOnDelete() && data.getDataEventType().equals(DataEventType.DELETE) || filter.isFilterOnInsert() && data.getDataEventType().equals(DataEventType.INSERT) || filter.isFilterOnUpdate() && data.getDataEventType().equals(DataEventType.UPDATE)) {
                String sql = null;
                if (writeMethod.equals(WriteMethod.BEFORE_WRITE) && filter.getBeforeWriteScript() != null) {
                    sql = doTokenReplacementOnSql(context, filter.getBeforeWriteScript());
                } else if (writeMethod.equals(WriteMethod.AFTER_WRITE) && filter.getAfterWriteScript() != null) {
                    sql = doTokenReplacementOnSql(context, filter.getAfterWriteScript());
                } else if (writeMethod.equals(WriteMethod.HANDLE_ERROR) && filter.getHandleErrorScript() != null) {
                    sql = doTokenReplacementOnSql(context, filter.getHandleErrorScript());
                }
                if (sql != null && !sql.trim().isEmpty()) {
                    if (namedParams == null) {
                        namedParams = getVariablesMap(table, data);
                    }
                    ISqlTransaction transaction = context.findTransaction();
                    values = transaction.query(sql, lookupColumnRowMapper, namedParams);
                }
                if (values != null && values.size() > 0) {
                    writeRow = values.get(0);
                }
            }
        }
    } catch (Exception ex) {
        processError(currentFilter, table, ex);
    }
    return writeRow;
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 29 with ISqlTransaction

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

the class DataGapFastDetector method fixOverlappingGaps.

protected void fixOverlappingGaps(List<DataGap> gaps, ProcessInfo processInfo) {
    try {
        ISqlTransaction transaction = null;
        log.debug("Looking for overlapping gaps");
        try {
            ISqlTemplate sqlTemplate = symmetricDialect.getPlatform().getSqlTemplate();
            transaction = sqlTemplate.startSqlTransaction();
            DataGap prevGap = null, lastGap = null;
            for (int i = 0; i < gaps.size(); i++) {
                DataGap curGap = gaps.get(i);
                if (lastGap != null) {
                    log.warn("Removing gap found after last gap: " + curGap);
                    dataService.deleteDataGap(transaction, curGap);
                    gaps.remove(i--);
                } else {
                    if (lastGap == null && curGap.gapSize() >= maxDataToSelect - 1) {
                        lastGap = curGap;
                    }
                    if (prevGap != null) {
                        if (prevGap.overlaps(curGap)) {
                            log.warn("Removing overlapping gaps: " + prevGap + ", " + curGap);
                            dataService.deleteDataGap(transaction, prevGap);
                            dataService.deleteDataGap(transaction, curGap);
                            DataGap newGap = null;
                            if (curGap.equals(lastGap)) {
                                newGap = new DataGap(prevGap.getStartId(), prevGap.getStartId() + maxDataToSelect - 1);
                            } else {
                                newGap = new DataGap(prevGap.getStartId(), prevGap.getEndId() > curGap.getEndId() ? prevGap.getEndId() : curGap.getEndId());
                            }
                            log.warn("Inserting new gap to fix overlap: " + newGap);
                            dataService.insertDataGap(transaction, newGap);
                            gaps.remove(i--);
                            gaps.set(i, newGap);
                            curGap = newGap;
                        }
                    }
                }
                prevGap = curGap;
            }
            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();
            }
        }
    } catch (RuntimeException ex) {
        processInfo.setStatus(Status.ERROR);
        throw ex;
    }
}
Also used : DataGap(org.jumpmind.symmetric.model.DataGap) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate)

Example 30 with ISqlTransaction

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

the class DataExtractorService method resetExtractRequest.

@Override
public void resetExtractRequest(OutgoingBatch batch) {
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        batch.setStatus(Status.RQ);
        outgoingBatchService.updateOutgoingBatch(transaction, batch);
        transaction.prepareAndExecute(getSql("resetExtractRequestStatus"), ExtractStatus.NE.name(), batch.getBatchId(), batch.getBatchId(), batch.getNodeId());
        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)

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