Search in sources :

Example 6 with ISqlTransaction

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

the class AbstractSymmetricDialect method createTrigger.

/*
     * Create the configured trigger. The catalog will be changed to the source
     * schema if the source schema is configured.
     */
public void createTrigger(final StringBuilder sqlBuffer, final DataEventType dml, final Trigger trigger, final TriggerHistory hist, final Channel channel, final String tablePrefix, final Table table) {
    log.info("Creating {} trigger for {}", hist.getTriggerNameForDmlType(dml), table.getFullyQualifiedTableName());
    String previousCatalog = null;
    String sourceCatalogName = table.getCatalog();
    String defaultCatalog = platform.getDefaultCatalog();
    String defaultSchema = platform.getDefaultSchema();
    String triggerSql = triggerTemplate.createTriggerDDL(dml, trigger, hist, channel, tablePrefix, table, defaultCatalog, defaultSchema);
    String postTriggerDml = createPostTriggerDDL(dml, trigger, hist, channel, tablePrefix, table);
    if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
        ISqlTransaction transaction = null;
        try {
            transaction = this.platform.getSqlTemplate().startSqlTransaction(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
            previousCatalog = switchCatalogForTriggerInstall(sourceCatalogName, transaction);
            try {
                log.debug("Running: {}", triggerSql);
                transaction.execute(triggerSql);
            } catch (SqlException ex) {
                log.info("Failed to create trigger: {}", triggerSql);
                throw ex;
            }
            if (StringUtils.isNotBlank(postTriggerDml)) {
                try {
                    transaction.execute(postTriggerDml);
                } catch (SqlException ex) {
                    log.info("Failed to create post trigger: {}", postTriggerDml);
                    throw ex;
                }
            }
            transaction.commit();
        } catch (SqlException ex) {
            transaction.rollback();
            throw ex;
        } finally {
            try {
                if (sourceCatalogName != null && !sourceCatalogName.equalsIgnoreCase(previousCatalog)) {
                    switchCatalogForTriggerInstall(previousCatalog, transaction);
                }
            } finally {
                transaction.close();
            }
        }
    }
    logSql(triggerSql, sqlBuffer);
    logSql(postTriggerDml, sqlBuffer);
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) SqlException(org.jumpmind.db.sql.SqlException)

Example 7 with ISqlTransaction

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

the class FileSyncService method save.

public void save(List<FileSnapshot> changes) {
    if (changes != null) {
        ISqlTransaction sqlTransaction = null;
        try {
            sqlTransaction = sqlTemplate.startSqlTransaction();
            for (FileSnapshot fileSnapshot : changes) {
                save(sqlTransaction, fileSnapshot);
            }
            sqlTransaction.commit();
        } catch (Error ex) {
            if (sqlTransaction != null) {
                sqlTransaction.rollback();
            }
            throw ex;
        } catch (RuntimeException ex) {
            if (sqlTransaction != null) {
                sqlTransaction.rollback();
            }
            throw ex;
        } finally {
            close(sqlTransaction);
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) TargetError(bsh.TargetError) EvalError(bsh.EvalError)

Example 8 with ISqlTransaction

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

the class DataLoaderService method insertIncomingError.

public void insertIncomingError(IncomingError incomingError) {
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        insertIncomingError(transaction, incomingError);
        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) IncomingError(org.jumpmind.symmetric.model.IncomingError)

Example 9 with ISqlTransaction

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

the class DataService method reloadTable.

public String reloadTable(String nodeId, String catalogName, String schemaName, String tableName, String overrideInitialLoadSelect) {
    Node sourceNode = engine.getNodeService().findIdentity();
    Node targetNode = engine.getNodeService().findNode(nodeId);
    if (targetNode == null) {
        return "Unknown node " + nodeId;
    }
    ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
    List<TriggerHistory> triggerHistories = triggerRouterService.findTriggerHistories(catalogName, schemaName, tableName);
    Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = triggerRouterService.fillTriggerRoutersByHistIdAndSortHist(sourceNode.getNodeGroupId(), targetNode.getNodeGroupId(), triggerHistories);
    int eventCount = 0;
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        for (TriggerHistory triggerHistory : triggerHistories) {
            List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
            if (triggerRouters != null && triggerRouters.size() > 0) {
                for (TriggerRouter triggerRouter : triggerRouters) {
                    eventCount++;
                    insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, overrideInitialLoadSelect, false, -1, "reloadTable", Status.NE);
                }
            }
        }
        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);
    }
    if (eventCount > 0) {
        return "Successfully created " + (eventCount > 1 ? eventCount + " events" : "event") + " to reload table " + tableName + " for node " + targetNode.getNodeId();
    } else {
        return "Trigger for table " + tableName + " does not exist for source node group of " + sourceNode.getNodeGroupId();
    }
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 10 with ISqlTransaction

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

the class DataService method insertReloadEvent.

public boolean insertReloadEvent(TableReloadRequest request, boolean deleteAtClient) {
    boolean successful = false;
    if (request != null && request.isReloadEnabled()) {
        ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
        INodeService nodeService = engine.getNodeService();
        Node targetNode = nodeService.findNode(request.getTargetNodeId());
        if (targetNode != null) {
            TriggerRouter triggerRouter = triggerRouterService.getTriggerRouterForCurrentNode(request.getTriggerId(), request.getRouterId(), false);
            if (triggerRouter != null) {
                Trigger trigger = triggerRouter.getTrigger();
                Router router = triggerRouter.getRouter();
                NodeGroupLink link = router.getNodeGroupLink();
                Node me = nodeService.findIdentity();
                if (link.getSourceNodeGroupId().equals(me.getNodeGroupId())) {
                    if (link.getTargetNodeGroupId().equals(targetNode.getNodeGroupId())) {
                        TriggerHistory triggerHistory = lookupTriggerHistory(trigger);
                        ISqlTransaction transaction = null;
                        try {
                            transaction = sqlTemplate.startSqlTransaction();
                            if (parameterService.is(ParameterConstants.INITIAL_LOAD_DELETE_BEFORE_RELOAD)) {
                                String overrideDeleteStatement = StringUtils.isNotBlank(request.getReloadDeleteStmt()) ? request.getReloadDeleteStmt() : null;
                                insertPurgeEvent(transaction, targetNode, triggerRouter, triggerHistory, false, overrideDeleteStatement, -1, null);
                            }
                            insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, request.getReloadSelect(), false, -1, null, Status.NE);
                            if (!targetNode.requires13Compatiblity() && deleteAtClient) {
                                insertSqlEvent(transaction, triggerHistory, trigger.getChannelId(), targetNode, String.format("delete from %s where target_node_id='%s' and source_node_id='%s' and trigger_id='%s' and router_id='%s'", TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST), request.getTargetNodeId(), request.getSourceNodeId(), request.getTriggerId(), request.getRouterId()), false, -1, null);
                            }
                            deleteTableReloadRequest(transaction, request);
                            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);
                        }
                    } else {
                        log.error("Could not reload table for node {} because the router {} target node group id {} did not match", new Object[] { request.getTargetNodeId(), request.getRouterId(), link.getTargetNodeGroupId() });
                    }
                } else {
                    log.error("Could not reload table for node {} because the router {} source node group id {} did not match", new Object[] { request.getTargetNodeId(), request.getRouterId(), link.getSourceNodeGroupId() });
                }
            } else {
                log.error("Could not reload table for node {} because the trigger router ({}, {}) could not be found", new Object[] { request.getTargetNodeId(), request.getTriggerId(), request.getRouterId() });
            }
        } else {
            log.error("Could not reload table for node {} because the node could not be found", request.getTargetNodeId());
        }
    }
    return successful;
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Router(org.jumpmind.symmetric.model.Router) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) INodeService(org.jumpmind.symmetric.service.INodeService) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink)

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