Skip to content

Commit

Permalink
0002152: DDL errors on DB2 i5 AS/400
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Jan 29, 2015
1 parent 0e8a22c commit 9837fe6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 14 deletions.
Expand Up @@ -28,6 +28,7 @@ public class Db2As400SymmetricDialect extends Db2SymmetricDialect implements ISy

public Db2As400SymmetricDialect(IParameterService parameterService, IDatabasePlatform platform) {
super(parameterService, platform);
this.triggerTemplate = new Db2As400TriggerTemplate(this);
}

@Override
Expand Down
@@ -0,0 +1,39 @@
package org.jumpmind.symmetric.db.db2;

import org.jumpmind.symmetric.db.ISymmetricDialect;

public class Db2As400TriggerTemplate extends Db2TriggerTemplate {

public Db2As400TriggerTemplate(ISymmetricDialect symmetricDialect) {
super(symmetricDialect);
sqlTemplates.put("updateTriggerTemplate" ,
"CREATE TRIGGER $(schemaName)$(triggerName) " +
" AFTER UPDATE ON $(schemaName)$(tableName) " +
" REFERENCING OLD AS OLD NEW AS NEW " +
" FOR EACH ROW MODE DB2SQL " +
" BEGIN ATOMIC " +
" IF $(syncOnIncomingBatchCondition) then " +
" IF $(dataHasChangedCondition) THEN " +
" INSERT into $(defaultSchema)$(prefixName)_data " +
" (table_name, event_type, trigger_hist_id, pk_data, row_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) " +
" VALUES('$(targetTableName)', 'U', $(triggerHistoryId), " +
" $(oldKeys), " +
" $(columns), " +
" $(oldColumns), " +
" $(channelExpression), " +
" $(txIdExpression), " +
" $(sourceNodeExpression), " +
" $(externalSelect), " +
" CURRENT_TIMESTAMP); " +
" END IF; " +
" END IF; " +
" $(custom_on_update_text) " +
" END " );

}

public boolean useTriggerTemplateForColumnTemplatesDuringInitialLoad() {
return false;
}

}
Expand Up @@ -38,7 +38,7 @@ public static enum RegistrationStatus {
private RegistrationStatus status;
private String hostName;
private String ipAddress;
private long attemptCount;
private long attemptCount = 1;
private String registeredNodeId;
private String errorMessage;
private Date createTime = new Date();
Expand Down
Expand Up @@ -313,23 +313,43 @@ public boolean deleteRegistrationRequest(RegistrationRequest request) {
}

public void saveRegistrationRequest(RegistrationRequest request) {
/**
* Lookup existing registration requests to update the attempt count. We previously
* did this in SQL on the update, but as400 v5 didn't like that
*/
boolean foundOne = false;
List<RegistrationRequest> requests = getRegistrationRequests(true);
for (RegistrationRequest registrationRequest : requests) {
if (registrationRequest.getNodeGroupId().equals(request.getNodeGroupId()) && registrationRequest.getExternalId().equals(request.getExternalId())) {
request.setAttemptCount(registrationRequest.getAttemptCount()+1);
foundOne = true;
break;
}
}
String externalId = request.getExternalId() == null ? "" : request.getExternalId();
String nodeGroupId = request.getNodeGroupId() == null ? "" : request.getNodeGroupId();
int count = sqlTemplate.update(
getSql("updateRegistrationRequestSql"),
new Object[] { request.getLastUpdateBy(), request.getLastUpdateTime(),
request.getRegisteredNodeId(), request.getStatus().name(), request.getErrorMessage(),
nodeGroupId, externalId, request.getIpAddress(), request.getHostName() },
new int[] { Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
int count = 0;
if (foundOne) {
count = sqlTemplate.update(
getSql("updateRegistrationRequestSql"),
new Object[] { request.getAttemptCount(), request.getLastUpdateBy(),
request.getLastUpdateTime(), request.getRegisteredNodeId(),
request.getStatus().name(), request.getErrorMessage(), nodeGroupId,
externalId, request.getIpAddress(), request.getHostName() }, new int[] {
Types.NUMERIC, Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR });
}

if (count == 0) {
sqlTemplate.update(
getSql("insertRegistrationRequestSql"),
new Object[] { request.getLastUpdateBy(), request.getLastUpdateTime(),
request.getRegisteredNodeId(), request.getStatus().name(), nodeGroupId,
externalId, request.getIpAddress(), request.getHostName(), request.getErrorMessage() },
new int[] { Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
externalId, request.getIpAddress(), request.getHostName(),
request.getErrorMessage() }, new int[] { Types.VARCHAR, Types.DATE,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
}

}
Expand Down
Expand Up @@ -20,9 +20,17 @@
*/
package org.jumpmind.symmetric.service.impl;

import java.io.File;
import java.sql.Types;
import java.util.Date;
import java.util.Map;

import org.apache.commons.dbcp.BasicDataSource;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.sql.SqlTemplateSettings;
import org.jumpmind.db.util.BasicDataSourceFactory;
import org.jumpmind.properties.TypedProperties;

public class RegistrationServiceSqlMap extends AbstractSqlMap {

Expand Down Expand Up @@ -69,10 +77,10 @@ public RegistrationServiceSqlMap(IDatabasePlatform platform,
putSql("updateRegistrationRequestSql",
""
+ "update $(registration_request) "
+ " set "
+ " last_update_by=?, last_update_time=?, attempt_count=attempt_count+1, registered_node_id=?, status=?, error_message=? "
+ " set attempt_count=?, "
+ " last_update_by=?, last_update_time=?, registered_node_id=?, status=?, error_message=? "
+ " where "
+ " node_group_id=? and external_id=? and ip_address=? and host_name=? and status in ('RQ','ER') ");
+ " node_group_id=? and external_id=? and ip_address=? and host_name=? and (status='RQ' or status='ER') ");

putSql("selectRegistrationRequestSql",
""
Expand All @@ -85,5 +93,23 @@ public RegistrationServiceSqlMap(IDatabasePlatform platform,
"delete from $(registration_request) where node_group_id=? and external_id=? and ip_address=? and host_name=? and status=?");

}

public static void main(String[] args) throws Exception {
String sql = "update sym_registration_request "
+ " set "
+ " last_update_by=?, last_update_time=?, attempt_count=1, registered_node_id=?, status=?, error_message=? "
+ " where "
+ " node_group_id=? and external_id=? and ip_address=? and host_name=? and status in ('RQ', 'ER') ";
BasicDataSource ds = BasicDataSourceFactory.create(new TypedProperties(new File("../symmetric-pro/target/engines/server.properties")));
IDatabasePlatform platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(ds, new SqlTemplateSettings(), false);
int count = platform.getSqlTemplate().update(
sql,
new Object[] { "me", new Date(),
"1", "ER", "",
"client", "1", "127.0.0.1", "localhost" },
new int[] { Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
System.out.println("updated " + count);
}

}
Expand Up @@ -33,6 +33,7 @@ public class Db2As400DdlBuilder extends Db2DdlBuilder {

public Db2As400DdlBuilder() {
this.databaseName = DatabaseNamesConstants.DB2AS400;
databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "LONG VARCHAR", Types.VARCHAR);
databaseInfo.setRequiresAutoCommitForDdl(true);
}

Expand Down

0 comments on commit 9837fe6

Please sign in to comment.