Search in sources :

Example 6 with SymmetricException

use of org.jumpmind.symmetric.SymmetricException in project symmetric-ds by JumpMind.

the class SimpleMongoClientManager method get.

@Override
public MongoClient get() {
    MongoClient client = clients.get(name);
    if (client == null) {
        synchronized (clients) {
            if (client == null) {
                int port = 27017;
                String host = "localhost";
                if (parameterService != null) {
                    port = parameterService.getInt(name + MongoConstants.PORT, port);
                    host = parameterService.getString(name + MongoConstants.HOST, host);
                }
                try {
                    client = new MongoClient(host, port);
                    clients.put(name, client);
                } catch (UnknownHostException e) {
                    throw new SymmetricException(e);
                }
            }
        }
    }
    return client;
}
Also used : MongoClient(com.mongodb.MongoClient) UnknownHostException(java.net.UnknownHostException) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 7 with SymmetricException

use of org.jumpmind.symmetric.SymmetricException in project symmetric-ds by JumpMind.

the class PostgreSqlSymmetricDialect method createRequiredDatabaseObjects.

@Override
public void createRequiredDatabaseObjects() {
    ISqlTransaction transaction = null;
    try {
        transaction = platform.getSqlTemplate().startSqlTransaction();
        enableSyncTriggers(transaction);
    } catch (Exception e) {
        String message = "Please add \"custom_variable_classes = 'symmetric'\" to your postgresql.conf file";
        log.error(message);
        throw new SymmetricException(message, e);
    } finally {
        if (transaction != null) {
            transaction.close();
        }
    }
    String triggersDisabled = this.parameterService.getTablePrefix() + "_" + "triggers_disabled";
    if (!installed(SQL_FUNCTION_INSTALLED, triggersDisabled)) {
        String sql = "CREATE or REPLACE FUNCTION $(functionName)() RETURNS INTEGER AS $$                                                                                                                     " + "                                DECLARE                                                                                                                                                                " + "                                  triggerDisabled INTEGER;                                                                                                                                             " + "                                BEGIN                                                                                                                                                                  " + "                                  select current_setting('symmetric.triggers_disabled') into triggerDisabled;                                                                                          " + "                                  return triggerDisabled;                                                                                                                                              " + "                                EXCEPTION WHEN OTHERS THEN                                                                                                                                             " + "                                  return 0;                                                                                                                                                            " + "                                END;                                                                                                                                                                   " + "                                $$ LANGUAGE plpgsql;                                                                                                                                                   ";
        install(sql, triggersDisabled);
    }
    String nodeDisabled = this.parameterService.getTablePrefix() + "_" + "node_disabled";
    if (!installed(SQL_FUNCTION_INSTALLED, nodeDisabled)) {
        String sql = "CREATE or REPLACE FUNCTION $(functionName)() RETURNS VARCHAR AS $$                                                                                                                     " + "                                DECLARE                                                                                                                                                                " + "                                  nodeId VARCHAR(50);                                                                                                                                                  " + "                                BEGIN                                                                                                                                                                  " + "                                  select current_setting('symmetric.node_disabled') into nodeId;                                                                                                       " + "                                  return nodeId;                                                                                                                                                       " + "                                EXCEPTION WHEN OTHERS THEN                                                                                                                                             " + "                                  return '';                                                                                                                                                           " + "                                END;                                                                                                                                                                   " + "                                $$ LANGUAGE plpgsql;                                                                                                                                                   ";
        install(sql, nodeDisabled);
    }
    String largeObjects = this.parameterService.getTablePrefix() + "_" + "largeobject";
    if (!installed(SQL_FUNCTION_INSTALLED, largeObjects)) {
        String sql = "CREATE OR REPLACE FUNCTION $(functionName)(objectId oid) RETURNS text AS $$                                                                                                            " + "                                DECLARE                                                                                                                                                                " + "                                  encodedBlob text;                                                                                                                                                    " + "                                  encodedBlobPage text;                                                                                                                                                " + "                                BEGIN                                                                                                                                                                  " + "                                  encodedBlob := '';                                                                                                                                                   " + "                                  FOR encodedBlobPage IN SELECT pg_catalog.encode(data, 'escape')                                                                                                                 " + "                                  FROM pg_largeobject WHERE loid = objectId ORDER BY pageno LOOP                                                                                                       " + "                                    encodedBlob := encodedBlob || encodedBlobPage;                                                                                                                     " + "                                  END LOOP;                                                                                                                                                            " + "                                  RETURN pg_catalog.encode(pg_catalog.decode(encodedBlob, 'escape'), 'base64');                                                                                                              " + "                                EXCEPTION WHEN OTHERS THEN                                                                                                                                             " + "                                  RETURN '';                                                                                                                                                           " + "                                END                                                                                                                                                                    " + "                                $$ LANGUAGE plpgsql;                                                                                                                                                   ";
        install(sql, largeObjects);
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) SymmetricException(org.jumpmind.symmetric.SymmetricException) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 8 with SymmetricException

use of org.jumpmind.symmetric.SymmetricException in project symmetric-ds by JumpMind.

the class JavaDatabaseWriterFilter 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;
    try {
        for (LoadFilter filter : loadFiltersForTable) {
            currentFilter = filter;
            if (filter.isFilterOnDelete() && data.getDataEventType().equals(DataEventType.DELETE) || filter.isFilterOnInsert() && data.getDataEventType().equals(DataEventType.INSERT) || filter.isFilterOnUpdate() && data.getDataEventType().equals(DataEventType.UPDATE)) {
                if (writeMethod.equals(WriteMethod.BEFORE_WRITE) && filter.getBeforeWriteScript() != null) {
                    writeRow = getCompiledClass(filter.getBeforeWriteScript()).execute(context, table, data, error);
                } else if (writeMethod.equals(WriteMethod.AFTER_WRITE) && filter.getAfterWriteScript() != null) {
                    writeRow = getCompiledClass(filter.getAfterWriteScript()).execute(context, table, data, error);
                } else if (writeMethod.equals(WriteMethod.HANDLE_ERROR) && filter.getHandleErrorScript() != null) {
                    writeRow = getCompiledClass(filter.getHandleErrorScript()).execute(context, table, data, error);
                }
            }
        }
    } catch (Exception ex) {
        String formattedMessage = String.format("Error executing Java load filter %s on table %s. The error was: %s", new Object[] { currentFilter != null ? currentFilter.getLoadFilterId() : "N/A", table.getName(), ex.getMessage() });
        log.error(formattedMessage);
        if (currentFilter.isFailOnError()) {
            throw new SymmetricException(formattedMessage, ex);
        }
    }
    return writeRow;
}
Also used : LoadFilter(org.jumpmind.symmetric.model.LoadFilter) SymmetricException(org.jumpmind.symmetric.SymmetricException) SymmetricException(org.jumpmind.symmetric.SymmetricException)

Example 9 with SymmetricException

use of org.jumpmind.symmetric.SymmetricException in project symmetric-ds by JumpMind.

the class DataService method insertSqlEventsPriorToReload.

private void insertSqlEventsPriorToReload(Node targetNode, String nodeIdRecord, long loadId, String createBy, boolean transactional, ISqlTransaction transaction, boolean reverse, List<TriggerHistory> triggerHistories, Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId, Map<String, TableReloadRequest> reloadRequests, boolean isFullLoad, String channelId) {
    if (!Constants.DEPLOYMENT_TYPE_REST.equals(targetNode.getDeploymentType())) {
        /*
                 * Insert node security so the client doing the initial load knows
                 * that an initial load is currently happening
                 */
        insertNodeSecurityUpdate(transaction, nodeIdRecord, targetNode.getNodeId(), true, loadId, createBy, channelId);
        if (isFullLoad) {
            /*
                     * Mark incoming batches as OK at the target node because we marked
                     * outgoing batches as OK at the source
                     */
            insertSqlEvent(transaction, targetNode, String.format("update %s_incoming_batch set status='OK', error_flag=0 where node_id='%s' and status != 'OK'", tablePrefix, engine.getNodeService().findIdentityNodeId()), true, loadId, createBy);
        }
    }
    if (!isFullLoad && reloadRequests != null && reloadRequests.size() > 0) {
        String beforeSql = "";
        int beforeSqlSent = 0;
        for (TriggerHistory triggerHistory : triggerHistories) {
            List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
            for (TriggerRouter triggerRouter : triggerRouters) {
                String key = triggerRouter.getTriggerId() + triggerRouter.getRouterId();
                TableReloadRequest currentRequest = reloadRequests.get(key);
                if (currentRequest == null) {
                    throw new SymmetricException("Could not locate table reload request for key '" + key + "'. Available requests are: " + reloadRequests.keySet());
                }
                beforeSql = currentRequest.getBeforeCustomSql();
                if (isNotBlank(beforeSql)) {
                    String tableName = triggerRouter.qualifiedTargetTableName(triggerHistory);
                    String formattedBeforeSql = String.format(beforeSql, tableName) + ";";
                    insertSqlEvent(transaction, targetNode, formattedBeforeSql, true, loadId, createBy);
                    beforeSqlSent++;
                }
            }
        }
        if (beforeSqlSent > 0) {
            log.info("Before sending load {} to target node {} the before sql [{}] was sent for {} tables", new Object[] { loadId, targetNode, beforeSql, beforeSqlSent });
        }
    } else {
        String beforeSql = parameterService.getString(reverse ? ParameterConstants.INITIAL_LOAD_REVERSE_BEFORE_SQL : ParameterConstants.INITIAL_LOAD_BEFORE_SQL);
        if (isNotBlank(beforeSql)) {
            insertSqlEvent(transaction, targetNode, beforeSql, true, loadId, createBy);
        }
    }
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) SymmetricException(org.jumpmind.symmetric.SymmetricException) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) TableReloadRequest(org.jumpmind.symmetric.model.TableReloadRequest)

Example 10 with SymmetricException

use of org.jumpmind.symmetric.SymmetricException in project symmetric-ds by JumpMind.

the class MongoDatabaseWriter method upsert.

protected LoadStatus upsert(CsvData data) {
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    try {
        DB db = clientManager.getDB(objectMapper.mapToDatabase(this.targetTable));
        DBCollection collection = db.getCollection(objectMapper.mapToCollection(this.targetTable));
        String[] columnNames = sourceTable.getColumnNames();
        Map<String, String> newData = data.toColumnNameValuePairs(columnNames, CsvData.ROW_DATA);
        Map<String, String> oldData = data.toColumnNameValuePairs(columnNames, CsvData.OLD_DATA);
        Map<String, String> pkData = data.toKeyColumnValuePairs(this.sourceTable);
        DBObject query = objectMapper.mapToDBObject(sourceTable, newData, oldData, pkData, true);
        DBObject object = objectMapper.mapToDBObject(sourceTable, newData, oldData, pkData, false);
        WriteResult results = collection.update(query, object, true, false, WriteConcern.ACKNOWLEDGED);
        if (results.getN() == 1) {
            return LoadStatus.SUCCESS;
        } else {
            throw new SymmetricException("Failed to write data: " + object.toString());
        }
    } finally {
        statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) SymmetricException(org.jumpmind.symmetric.SymmetricException) DBObject(com.mongodb.DBObject) DB(com.mongodb.DB)

Aggregations

SymmetricException (org.jumpmind.symmetric.SymmetricException)14 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)3 Node (org.jumpmind.symmetric.model.Node)3 NodeSecurity (org.jumpmind.symmetric.model.NodeSecurity)3 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)3 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)3 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)3 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 Table (org.jumpmind.db.model.Table)2 IoException (org.jumpmind.exception.IoException)2 TransformTable (org.jumpmind.symmetric.io.data.transform.TransformTable)2 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)2 Router (org.jumpmind.symmetric.model.Router)2 InternalIncomingTransport (org.jumpmind.symmetric.transport.internal.InternalIncomingTransport)2 EvalError (bsh.EvalError)1 Interpreter (bsh.Interpreter)1 ParseException (bsh.ParseException)1 TargetError (bsh.TargetError)1 DB (com.mongodb.DB)1