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