Search in sources :

Example 1 with ISqlTransaction

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

the class DataService method insertReloadEvents.

public void insertReloadEvents(Node targetNode, boolean reverse, List<TableReloadRequest> reloadRequests, ProcessInfo processInfo) {
    if (engine.getClusterService().lock(ClusterConstants.SYNCTRIGGERS)) {
        try {
            synchronized (engine.getTriggerRouterService()) {
                engine.getClusterService().lock(ClusterConstants.SYNCTRIGGERS);
                boolean isFullLoad = reloadRequests == null || (reloadRequests.size() == 1 && reloadRequests.get(0).isFullLoadRequest());
                if (!reverse) {
                    log.info("Queueing up " + (isFullLoad ? "an initial" : "a") + " load to node " + targetNode.getNodeId());
                } else {
                    log.info("Queueing up a reverse " + (isFullLoad ? "initial" : "") + " load to node " + targetNode.getNodeId());
                }
                /*
                     * Outgoing data events are pointless because we are
                     * reloading all data
                     */
                if (isFullLoad) {
                    engine.getOutgoingBatchService().markAllAsSentForNode(targetNode.getNodeId(), false);
                }
                INodeService nodeService = engine.getNodeService();
                ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
                Node sourceNode = nodeService.findIdentity();
                boolean transactional = parameterService.is(ParameterConstants.DATA_RELOAD_IS_BATCH_INSERT_TRANSACTIONAL);
                String nodeIdRecord = reverse ? nodeService.findIdentityNodeId() : targetNode.getNodeId();
                NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeIdRecord);
                ISqlTransaction transaction = null;
                try {
                    transaction = platform.getSqlTemplate().startSqlTransaction();
                    long loadId = engine.getSequenceService().nextVal(transaction, Constants.SEQUENCE_OUTGOING_BATCH_LOAD_ID);
                    processInfo.setCurrentLoadId(loadId);
                    String createBy = reverse ? nodeSecurity.getRevInitialLoadCreateBy() : nodeSecurity.getInitialLoadCreateBy();
                    List<TriggerHistory> triggerHistories = new ArrayList<TriggerHistory>();
                    if (isFullLoad) {
                        triggerHistories = triggerRouterService.getActiveTriggerHistories();
                    } else {
                        for (TableReloadRequest reloadRequest : reloadRequests) {
                            triggerHistories.addAll(engine.getTriggerRouterService().getActiveTriggerHistories(new Trigger(reloadRequest.getTriggerId(), null)));
                        }
                    }
                    processInfo.setDataCount(triggerHistories.size());
                    Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = triggerRouterService.fillTriggerRoutersByHistIdAndSortHist(sourceNode.getNodeGroupId(), targetNode.getNodeGroupId(), triggerHistories);
                    if (isFullLoad) {
                        callReloadListeners(true, targetNode, transactional, transaction, loadId);
                        insertCreateSchemaScriptPriorToReload(targetNode, nodeIdRecord, loadId, createBy, transactional, transaction);
                    }
                    Map<String, TableReloadRequest> mapReloadRequests = convertReloadListToMap(reloadRequests);
                    String symNodeSecurityReloadChannel = null;
                    try {
                        symNodeSecurityReloadChannel = triggerRoutersByHistoryId.get(triggerHistories.get(0).getTriggerHistoryId()).get(0).getTrigger().getReloadChannelId();
                    } catch (Exception e) {
                    }
                    if (isFullLoad || (reloadRequests != null && reloadRequests.size() > 0)) {
                        insertSqlEventsPriorToReload(targetNode, nodeIdRecord, loadId, createBy, transactional, transaction, reverse, triggerHistories, triggerRoutersByHistoryId, mapReloadRequests, isFullLoad, symNodeSecurityReloadChannel);
                    }
                    insertCreateBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests);
                    insertDeleteBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests);
                    insertSQLBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests);
                    insertLoadBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests, processInfo);
                    if (isFullLoad) {
                        String afterSql = parameterService.getString(reverse ? ParameterConstants.INITIAL_LOAD_REVERSE_AFTER_SQL : ParameterConstants.INITIAL_LOAD_AFTER_SQL);
                        if (isNotBlank(afterSql)) {
                            insertSqlEvent(transaction, targetNode, afterSql, true, loadId, createBy);
                        }
                    }
                    insertFileSyncBatchForReload(targetNode, loadId, createBy, transactional, transaction, processInfo);
                    if (isFullLoad) {
                        callReloadListeners(false, targetNode, transactional, transaction, loadId);
                        if (!reverse) {
                            nodeService.setInitialLoadEnabled(transaction, nodeIdRecord, false, false, loadId, createBy);
                        } else {
                            nodeService.setReverseInitialLoadEnabled(transaction, nodeIdRecord, false, false, loadId, createBy);
                        }
                    }
                    if (!Constants.DEPLOYMENT_TYPE_REST.equals(targetNode.getDeploymentType())) {
                        insertNodeSecurityUpdate(transaction, nodeIdRecord, targetNode.getNodeId(), true, loadId, createBy, symNodeSecurityReloadChannel);
                    }
                    engine.getStatisticManager().incrementNodesLoaded(1);
                    if (reloadRequests != null && reloadRequests.size() > 0) {
                        for (TableReloadRequest request : reloadRequests) {
                            transaction.prepareAndExecute(getSql("updateProcessedTableReloadRequest"), loadId, new Date(), request.getTargetNodeId(), request.getSourceNodeId(), request.getTriggerId(), request.getRouterId(), request.getCreateTime());
                        }
                        log.info("Table reload request(s) for load id " + loadId + " have been processed.");
                    }
                    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 (!reverse) {
                    /*
                         * Remove all incoming events for the node that we are
                         * starting a reload for
                         */
                    engine.getPurgeService().purgeAllIncomingEventsForNode(targetNode.getNodeId());
                }
            }
        } finally {
            engine.getClusterService().unlock(ClusterConstants.SYNCTRIGGERS);
        }
    } else {
        log.info("Not attempting to insert reload events because sync trigger is currently running");
    }
}
Also used : NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) ArrayList(java.util.ArrayList) UniqueKeyException(org.jumpmind.db.sql.UniqueKeyException) NotImplementedException(org.apache.commons.lang.NotImplementedException) SymmetricException(org.jumpmind.symmetric.SymmetricException) IoException(org.jumpmind.exception.IoException) Date(java.util.Date) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) INodeService(org.jumpmind.symmetric.service.INodeService) TableReloadRequest(org.jumpmind.symmetric.model.TableReloadRequest) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with ISqlTransaction

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

the class SQLDatabaseWriterFilter method executeScripts.

@Override
protected void executeScripts(DataContext context, String key, Set<String> scripts, boolean isFailOnError) {
    if (scripts != null) {
        try {
            ISqlTransaction transaction = context.findTransaction();
            for (String script : scripts) {
                String sql = doTokenReplacementOnSql(context, script);
                transaction.query(sql, lookupColumnRowMapper, null);
            }
        } catch (Exception e) {
            if (isFailOnError) {
                throw (RuntimeException) e;
            } else {
                log.error("Failed while executing sql script", e);
            }
        }
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 3 with ISqlTransaction

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

the class DataGapFastDetector method saveDataGaps.

protected long saveDataGaps(long ts, long printStats) {
    ISqlTemplate sqlTemplate = symmetricDialect.getPlatform().getSqlTemplate();
    int totalGapChanges = gapsDeleted.size() + gapsAdded.size();
    if (totalGapChanges > 0) {
        ISqlTransaction transaction = null;
        gaps = new ArrayList<DataGap>(gapsAll);
        Collections.sort(gaps);
        try {
            transaction = sqlTemplate.startSqlTransaction();
            int maxGapChanges = parameterService.getInt(ParameterConstants.ROUTING_MAX_GAP_CHANGES);
            if (!parameterService.is(ParameterConstants.CLUSTER_LOCKING_ENABLED) && (totalGapChanges > maxGapChanges || useInMemoryGaps)) {
                dataService.deleteAllDataGaps(transaction);
                if (useInMemoryGaps && totalGapChanges <= maxGapChanges) {
                    log.info("There are {} data gap changes, which is within the max of {}, so switching to database", totalGapChanges, maxGapChanges);
                    useInMemoryGaps = false;
                    printStats = insertDataGaps(transaction, ts, printStats);
                } else {
                    if (!useInMemoryGaps) {
                        log.info("There are {} data gap changes, which exceeds the max of {}, so switching to in-memory", totalGapChanges, maxGapChanges);
                        useInMemoryGaps = true;
                    }
                    DataGap newGap = new DataGap(gaps.get(0).getStartId(), gaps.get(gaps.size() - 1).getEndId());
                    dataService.insertDataGap(transaction, newGap);
                }
            } else {
                printStats = deleteDataGaps(transaction, ts, printStats);
                printStats = insertDataGaps(transaction, ts, printStats);
            }
            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();
            }
        }
    }
    return printStats;
}
Also used : DataGap(org.jumpmind.symmetric.model.DataGap) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction)

Example 4 with ISqlTransaction

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

the class AbstractSymmetricDialect method createDdlTrigger.

public void createDdlTrigger(final String tablePrefix, StringBuilder sqlBuffer, String triggerName) {
    if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
        String triggerSql = triggerTemplate.createDdlTrigger(tablePrefix, platform.getDefaultCatalog(), platform.getDefaultSchema(), triggerName);
        log.info("Creating DDL trigger " + triggerName);
        if (triggerSql != null) {
            ISqlTransaction transaction = null;
            try {
                transaction = this.platform.getSqlTemplate().startSqlTransaction(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
                try {
                    log.debug("Running: {}", triggerSql);
                    logSql(triggerSql, sqlBuffer);
                    transaction.execute(triggerSql);
                } catch (SqlException ex) {
                    log.info("Failed to create DDL trigger: {}", triggerSql);
                    throw ex;
                }
                transaction.commit();
            } catch (SqlException ex) {
                transaction.rollback();
                throw ex;
            } finally {
                transaction.close();
            }
        }
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) SqlException(org.jumpmind.db.sql.SqlException)

Example 5 with ISqlTransaction

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

the class AdditiveColumnTransform method transform.

public String transform(IDatabasePlatform platform, DataContext context, TransformColumn column, TransformedData data, Map<String, String> sourceValues, String newValue, String oldValue) throws IgnoreColumnException, IgnoreRowException {
    BigDecimal multiplier = new BigDecimal(1.00);
    if (StringUtils.isNotBlank(column.getTransformExpression())) {
        multiplier = new BigDecimal(column.getTransformExpression());
    }
    Table table = platform.getTableFromCache(data.getCatalogName(), data.getSchemaName(), data.getTableName(), false);
    if (table == null) {
        if (log.isDebugEnabled()) {
            log.debug("Could not find the target table {}", data.getFullyQualifiedTableName());
        }
        throw new IgnoreColumnException();
    } else if (table.getColumnWithName(column.getTargetColumnName()) == null) {
        if (log.isDebugEnabled()) {
            log.debug("Could not find the target column {}", column.getTargetColumnName());
        }
        throw new IgnoreColumnException();
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Old, new, transform expression as received: " + oldValue + ", " + newValue + ", " + column.getTransformExpression());
        }
        if (!StringUtils.isNotBlank(newValue) || data.getSourceDmlType() == DataEventType.DELETE) {
            newValue = "0";
        }
        if (!StringUtils.isNotBlank(oldValue)) {
            oldValue = "0";
        }
        BigDecimal delta = new BigDecimal(newValue);
        delta = delta.subtract(new BigDecimal(oldValue));
        delta = delta.multiply(multiplier);
        newValue = delta.toString();
        String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn() ? platform.getDatabaseInfo().getDelimiterToken() : "";
        StringBuilder sql = new StringBuilder(String.format("update %s set %s=%s+(%s) where ", getFullyQualifiedTableName(platform, data.getSchemaName(), data.getCatalogName(), data.getTableName()), quote + column.getTargetColumnName() + quote, quote + column.getTargetColumnName() + quote, newValue));
        String[] keyNames = data.getKeyNames();
        List<Column> columns = new ArrayList<Column>();
        List<String> keyValuesList = new ArrayList<String>();
        boolean addedFirstKey = false;
        for (int i = 0; i < keyNames.length; i++) {
            Column targetCol = table.getColumnWithName(keyNames[i]);
            if (targetCol != null) {
                columns.add(targetCol);
                keyValuesList.add(sourceValues.get(keyNames[i]));
                if (addedFirstKey) {
                    sql.append("and ");
                } else {
                    addedFirstKey = true;
                }
                sql.append(quote);
                sql.append(keyNames[i]);
                sql.append(quote);
                sql.append("=? ");
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("SQL: " + sql);
        }
        ISqlTransaction transaction = context.findTransaction();
        if (0 < transaction.prepareAndExecute(sql.toString(), platform.getObjectValues(context.getBatch().getBinaryEncoding(), keyValuesList.toArray(new String[keyValuesList.size()]), columns.toArray(new Column[columns.size()])))) {
            throw new IgnoreColumnException();
        }
    }
    return newValue;
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) BigDecimal(java.math.BigDecimal)

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