Skip to content

Commit

Permalink
0003574: Improved logging for data truncation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpmind-josh committed May 23, 2018
1 parent 6d158a8 commit 5ffcafb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
Expand Up @@ -362,4 +362,8 @@ protected <T> T get(Cursor cursor, Class<T> clazz, int columnIndex) {
return (T) result;
}

@Override
public boolean isDataTruncationViolation(Throwable ex) {
return false;
}
}
Expand Up @@ -294,6 +294,8 @@ public SqlException translate(Throwable ex) {
public SqlException translate(String message, Throwable ex) {
if (isUniqueKeyViolation(ex) && !(ex instanceof UniqueKeyException)) {
return new UniqueKeyException(ex);
} else if (isDataTruncationViolation(ex)) {
return new DataTruncationException(ex);
} else if (ex instanceof SqlException) {
return (SqlException) ex;
} else {
Expand Down
@@ -0,0 +1,23 @@
package org.jumpmind.db.sql;

public class DataTruncationException extends SqlException {

private static final long serialVersionUID = 1L;

public DataTruncationException() {
super();
}

public DataTruncationException(String message, Throwable cause) {
super(message, cause);
}

public DataTruncationException(String message) {
super(message);
}

public DataTruncationException(Throwable cause) {
super(cause);
}

}
Expand Up @@ -108,6 +108,8 @@ public <T, W> Map<T, W> query(String sql, String keyCol, String valueCol, Object

public boolean isUniqueKeyViolation(Throwable ex);

public boolean isDataTruncationViolation(Throwable ex);

public boolean isForeignKeyViolation(Throwable ex);

public ISqlTransaction startSqlTransaction();
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.DatabaseInfo;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.DataTruncationException;
import org.jumpmind.db.sql.DmlStatement;
import org.jumpmind.db.sql.DmlStatement.DmlType;
import org.jumpmind.db.sql.ISqlRowMapper;
Expand Down Expand Up @@ -709,11 +710,34 @@ protected void logFailureDetails(Throwable e, CsvData data, boolean logLastDmlDe
failureMessage.append("\n");
}

if (e instanceof DataTruncationException) {
logDataTruncation(data, failureMessage);
}
data.writeCsvDataDetails(failureMessage);

log.info(failureMessage.toString(), e);
}

protected void logDataTruncation(CsvData data, StringBuilder failureMessage) {
String[] rowData = data.getParsedData(CsvData.ROW_DATA);
int rowIndex = 0;
for (Column col : targetTable.getColumns()) {
if (col.getJdbcTypeCode() == Types.VARCHAR) { // CHAR
if (rowData[rowIndex].length() > Integer.parseInt(col.getSize())) {
failureMessage.append("Failed truncation column: ");
failureMessage.append(col.getName());
failureMessage.append(" with size of: ");
failureMessage.append(Integer.parseInt(col.getSize()));
failureMessage.append(" failed to load data: ");
failureMessage.append(rowData[rowIndex]);
failureMessage.append("\n");
}
}

rowIndex++;
}
}

protected String dmlValuesToString(Object[] dmlValues, int[] types) {
StringBuilder buff = new StringBuilder();
if (dmlValues == null || dmlValues.length == 0) {
Expand Down
Expand Up @@ -64,4 +64,17 @@ protected boolean allowsNullForIdentityColumn() {
protected void setNanOrNull(PreparedStatement ps, int i, Object arg, int argType) throws SQLException {
StatementCreatorUtils.setParameterValue(ps, i, Types.FLOAT, Float.NaN);
}

@Override
public boolean isDataTruncationViolation(Throwable ex) {
boolean dataTruncationViolation = false;
SQLException sqlEx = findSQLException(ex);
if (sqlEx != null) {
String sqlState = sqlEx.getSQLState();
if (sqlState != null && sqlState.equals("22001")) {
dataTruncationViolation = true;
}
}
return dataTruncationViolation;
}
}
Expand Up @@ -1087,5 +1087,9 @@ public void doSetValue(PreparedStatement ps, int parameterPosition, Object argVa
StatementCreatorUtils.setParameterValue(ps, parameterPosition, SqlTypeValue.TYPE_UNKNOWN, argValue);
}

@Override
public boolean isDataTruncationViolation(Throwable ex) {
return false;
}

}

0 comments on commit 5ffcafb

Please sign in to comment.