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;
}
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);
}
}
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;
}
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);
}
}
}
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);
}
}
Aggregations