Skip to content

Commit

Permalink
0001723: Unique constraint violation is not logged for tables declare…
Browse files Browse the repository at this point in the history
…d for conflict detection
  • Loading branch information
chenson42 committed May 22, 2014
1 parent e3ae299 commit ba33fa2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Expand Up @@ -45,6 +45,8 @@ abstract public class AbstractDatabaseWriter implements IDataWriter {

protected final static Logger log = LoggerFactory.getLogger(AbstractDatabaseWriter.class);

public static final String CONFLICT_ERROR = "DatabaseWriter.ConflictError";

public static enum LoadStatus {
SUCCESS, CONFLICT
};
Expand Down Expand Up @@ -73,7 +75,7 @@ public static enum LoadStatus {

protected IDatabaseWriterConflictResolver conflictResolver;

protected Set<String> missingTables = new HashSet<String>();
protected Set<String> missingTables = new HashSet<String>();

public AbstractDatabaseWriter() {
this(null, null);
Expand Down Expand Up @@ -130,6 +132,7 @@ public void write(CsvData data) {
}

protected void write(CsvData data, boolean fallback) {
context.put(CONFLICT_ERROR, null);
if (data.requiresTable() &&
(targetTable == null && data.getDataEventType() != DataEventType.SQL)) {
// if we cross batches and the table isn't specified, then
Expand Down
Expand Up @@ -220,7 +220,8 @@ protected void attemptToResolve(ResolvedData resolvedData, CsvData data, Abstrac
}
}
} else {
throw new ConflictException(data, writer.getTargetTable(), false, conflict);
throw new ConflictException(data, writer.getTargetTable(), false, conflict,
(Exception) writer.getContext().get(AbstractDatabaseWriter.CONFLICT_ERROR));
}
}

Expand Down
Expand Up @@ -37,13 +37,17 @@ public class ConflictException extends RuntimeException {

protected Conflict conflict;

public ConflictException(CsvData data, Table table, boolean fallbackOperationFailed, Conflict conflict) {
super(message(data, table, fallbackOperationFailed));
public ConflictException(CsvData data, Table table, boolean fallbackOperationFailed, Conflict conflict, Exception cause) {
super(message(data, table, fallbackOperationFailed), cause);
this.data = data;
this.table = table;
this.fallbackOperationFailed = fallbackOperationFailed;
this.conflict = conflict;
}

public ConflictException(CsvData data, Table table, boolean fallbackOperationFailed, Conflict conflict) {
this(data, table, fallbackOperationFailed, conflict, null);
}

protected static String message(CsvData data, Table table, boolean fallbackOperationFailed) {
Map<String, String> pks = data.toColumnNameValuePairs(table.getPrimaryKeyColumnNames(),
Expand Down
Expand Up @@ -60,7 +60,7 @@ public class DefaultDatabaseWriter extends AbstractDatabaseWriter {

protected final static Logger log = LoggerFactory.getLogger(DefaultDatabaseWriter.class);

public static final String CUR_DATA="DatabaseWriter.CurData";
public static final String CUR_DATA = "DatabaseWriter.CurData";

protected IDatabasePlatform platform;

Expand Down Expand Up @@ -178,10 +178,11 @@ protected LoadStatus insert(CsvData data) {
} catch (SqlException ex) {
if (platform.getSqlTemplate().isUniqueKeyViolation(ex)) {
if (!platform.getDatabaseInfo().isRequiresSavePointsInTransaction()) {
context.put(CONFLICT_ERROR, ex);
context.put(CUR_DATA,getCurData(transaction));
return LoadStatus.CONFLICT;
} else {
log.warn("Detected a conflict via an exception, but cannot perform conflict resolution because the database in use requires savepoints");
log.info("Detected a conflict via an exception, but cannot perform conflict resolution because the database in use requires savepoints");
throw ex;
}
} else {
Expand Down

0 comments on commit ba33fa2

Please sign in to comment.