use of org.dbflute.exception.DfJDBCException in project dbflute-core by dbflute.
the class DfDBFluteTaskUtil method shutdownIfDerbyEmbedded.
// ===================================================================================
// Connection
// ==========
public static void shutdownIfDerbyEmbedded(String driver) throws SQLException {
if (!driver.startsWith("org.apache.derby.") || !driver.endsWith(".EmbeddedDriver")) {
return;
}
final String shutdownUrl = "jdbc:derby:;shutdown=true";
Connection conn = null;
try {
_log.info("...Shutting down the connection to Derby");
conn = DriverManager.getConnection(shutdownUrl);
} catch (SQLException e) {
if ("XJ015".equals(e.getSQLState())) {
_log.info(" -> success: " + e.getMessage());
} else {
String msg = "Failed to shut down the connection to Derby:";
msg = msg + " shutdownUrl=" + shutdownUrl;
throw new DfJDBCException(msg, e);
}
} finally {
if (conn != null) {
conn.close();
}
}
}
use of org.dbflute.exception.DfJDBCException in project dbflute-core by dbflute.
the class DfAbsractDataWriter method throwColumnValueProcessingFailureException.
protected void throwColumnValueProcessingFailureException(StringProcessor processor, String tableName, String columnName, String value) throws SQLException {
final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
br.addNotice("The column value could not be treated by the processor.");
br.addItem("Advice");
br.addElement("The column has string expressions judging the type of the column");
br.addElement("by analyzing the value of first record.");
br.addElement("But the value of second or more record did not match the type.");
br.addElement("So confirm your expressions.");
br.addItem("Table Name");
br.addElement(tableName);
br.addItem("Column Name");
br.addElement(columnName);
br.addItem("String Expression");
br.addElement(value);
br.addItem("Processor");
br.addElement(processor);
final String msg = br.buildExceptionMessage();
throw new DfJDBCException(msg);
}
use of org.dbflute.exception.DfJDBCException in project dbflute-core by dbflute.
the class DfXlsDataHandlerImpl method handleWriteTableException.
protected // basic
void handleWriteTableException(// basic
String dataDirectory, // basic
File file, // basic
DfDataTable dataTable, // an exception of main process
SQLException mainEx, // retry
SQLException retryEx, // retry
DfDataRow retryDataRow, List<String> columnNameList) {
// supplement
final DfJDBCException wrappedEx = DfJDBCException.voice(mainEx);
final String tableDbName = dataTable.getTableDbName();
final String msg = buildWriteFailureMessage(dataDirectory, file, tableDbName, wrappedEx, retryEx, retryDataRow, columnNameList);
throw new DfXlsDataRegistrationFailureException(msg, wrappedEx);
}
use of org.dbflute.exception.DfJDBCException in project dbflute-core by dbflute.
the class DfXlsDataWritingExceptionThrower method handleWriteTableSQLException.
// -----------------------------------------------------
// Write SQLException
// ------------------
public // basic
void handleWriteTableSQLException(// basic
String dataDirectory, // basic
File file, // basic
DfDataTable dataTable, // an exception of main process
SQLException mainEx, // retry
SQLException retryEx, // retry
DfDataRow retryDataRow, // supplement
List<String> columnNameList, // cache map of bind type
Map<String, Map<String, Class<?>>> bindTypeCacheMap, // cache map of string processor
Map<String, Map<String, StringProcessor>> stringProcessorCacheMap) {
final DfJDBCException wrappedEx = DfJDBCException.voice(mainEx);
final String tableDbName = dataTable.getTableDbName();
final String msg = buildWriteFailureMessage(dataDirectory, file, tableDbName, wrappedEx, retryEx, retryDataRow, columnNameList, bindTypeCacheMap, stringProcessorCacheMap);
throw new DfXlsDataRegistrationFailureException(msg, wrappedEx);
}
use of org.dbflute.exception.DfJDBCException in project dbflute-core by dbflute.
the class DfDelimiterDataWriterImpl method doWriteData.
// -----------------------------------------------------
// Write Data
// ----------
protected void doWriteData(DfDelimiterDataResultInfo resultInfo, boolean forcedlySuppressBatch, int offsetRowCount) throws IOException {
final String dataDirectory = Srl.substringLastFront(_filePath, "/");
final LoggingInsertType loggingInsertType = getLoggingInsertType(dataDirectory);
final String tableDbName = extractTableDbName();
final Map<String, DfColumnMeta> columnMetaMap = getColumnMetaMap(tableDbName);
if (columnMetaMap.isEmpty()) {
throwTableNotFoundException(_filePath, tableDbName);
}
// process before handling table
beforeHandlingTable(tableDbName, columnMetaMap);
// fixedly
final String lineSeparatorInValue = "\n";
final File dataFile = new File(_filePath);
final boolean canBatchUpdate = canBatchUpdate(forcedlySuppressBatch, dataDirectory);
final StringBuilder lineStringSb = new StringBuilder();
final StringBuilder preContinuedSb = new StringBuilder();
final List<String> columnNameList = new ArrayList<String>();
final List<String> columnValueList = new ArrayList<String>();
List<String> valueListSnapshot = null;
// not line on file, as registered record
int rowNumber = 0;
String executedSql = null;
// may committed per limit size, for skip in retry
int committedRowCount = 0;
FileInputStream fis = null;
InputStreamReader ir = null;
BufferedReader br = null;
Connection conn = null;
PreparedStatement ps = null;
try {
fis = new FileInputStream(dataFile);
ir = new InputStreamReader(fis, _encoding);
br = new BufferedReader(ir);
DfDelimiterDataFirstLineInfo firstLineInfo = null;
int loopIndex = -1;
// current registered size to prepared statement
int addedBatchSize = 0;
while (true) {
++loopIndex;
{
final String readLine = br.readLine();
if (readLine == null) {
break;
}
clearAppend(lineStringSb, readLine);
}
// - - - - - - - - - -/
if (loopIndex == 0) {
firstLineInfo = analyzeFirstLine(lineStringSb.toString(), _delimiter);
setupColumnNameList(columnNameList, dataDirectory, dataFile, tableDbName, firstLineInfo, columnMetaMap);
continue;
}
// /- - - - - - - - - - - - - - -
// analyze values in line strings
// - - - - - - - - - -/
// might be clear-appended
filterLineStringIfNeeds(lineStringSb);
{
if (preContinuedSb.length() > 0) {
// done performance tuning, suppress incremental strings from many line separators by jflute (2018/03/02)
// it needs to change lineString, preContinueString to StringBuilder type...
// lineString = preContinueString + "\n" + lineString; (2021/01/21)
// and insert has array-copy so may not be fast
// lineStringSb.insert(0, "\n").insert(0, preContinuedSb); (2021/01/21)
// used only here so changing is no problem
preContinuedSb.append(lineSeparatorInValue).append(lineStringSb);
clearAppend(lineStringSb, preContinuedSb);
}
final DfDelimiterDataValueLineInfo valueLineInfo = analyzeValueLine(lineStringSb.toString(), _delimiter);
// empty string resolved later
final List<String> extractedList = valueLineInfo.getValueList();
if (valueLineInfo.isContinueNextLine()) {
clearAppend(preContinuedSb, extractedList.remove(extractedList.size() - 1));
columnValueList.addAll(extractedList);
// keeping valueList that has previous values
continue;
}
columnValueList.addAll(extractedList);
}
// - - - - - - - - - -/
if (isDifferentColumnValueCount(firstLineInfo, columnValueList)) {
handleDifferentColumnValueCount(resultInfo, dataDirectory, tableDbName, firstLineInfo, columnValueList);
// clear temporary variables
clear(preContinuedSb);
columnValueList.clear();
valueListSnapshot = null;
continue;
}
// *valid record is prepared here
++rowNumber;
valueListSnapshot = columnValueList;
if (rowNumber <= offsetRowCount) {
// basically only when retry
// clear temporary variables
clear(preContinuedSb);
columnValueList.clear();
valueListSnapshot = null;
// e.g. 1 ~ 100000 rows if 100000 already committed
continue;
}
// /- - - - - - - - - - - - - - - -
// process registration to database
// - - - - - - - - - -/
final DfDelimiterDataWriteSqlBuilder sqlBuilder = createSqlBuilder(resultInfo, tableDbName, columnMetaMap, columnNameList, columnValueList);
if (conn == null) {
conn = _dataSource.getConnection();
}
if (ps == null) {
// for performance (suppress implicit transaction per SQL)
beginTransaction(conn);
executedSql = sqlBuilder.buildSql();
ps = prepareStatement(conn, executedSql);
}
final Map<String, Object> columnValueMap = sqlBuilder.setupParameter();
final Set<String> sysdateColumnSet = sqlBuilder.getSysdateColumnSet();
resolveRelativeDate(dataDirectory, tableDbName, columnValueMap, columnMetaMap, sysdateColumnSet, rowNumber);
handleLoggingInsert(tableDbName, columnValueMap, loggingInsertType, rowNumber);
int bindCount = 1;
for (Entry<String, Object> entry : columnValueMap.entrySet()) {
final String columnName = entry.getKey();
final Object obj = entry.getValue();
// - - - - - - - - - -/
if (processNull(dataDirectory, tableDbName, columnName, obj, ps, bindCount, columnMetaMap, rowNumber)) {
bindCount++;
continue;
}
// It registers the value to statement by the type.
if (processNotNullNotString(dataDirectory, tableDbName, columnName, obj, conn, ps, bindCount, columnMetaMap, rowNumber)) {
bindCount++;
continue;
}
// /- - - - - - - - - - - - - - - - - -
// process NotNull and StringExpression
// - - - - - - - - - -/
final String value = (String) obj;
processNotNullString(dataDirectory, dataFile, tableDbName, columnName, value, conn, ps, bindCount, columnMetaMap, rowNumber);
bindCount++;
}
if (canBatchUpdate) {
// mainly here
ps.addBatch();
} else {
ps.execute();
}
++addedBatchSize;
if (isBatchLimit(dataDirectory, addedBatchSize)) {
// transaction scope
if (canBatchUpdate) {
// mainly here
// this is supported in only delimiter data writer because delimiter data can treat large data
// (actually needed, GC overhead limit exceeded when 1000000 records to MySQL, 2021/01/20)
// to avoid OutOfMemory
ps.executeBatch();
}
commitTransaction(conn);
committedRowCount = committedRowCount + addedBatchSize;
addedBatchSize = 0;
close(ps);
ps = null;
}
// *one record is finished here
// clear temporary variables
// if an exception occurs from execute() or addBatch(),
// this valueList is to be information for debug
clear(preContinuedSb);
columnValueList.clear();
// keep here for retry
// valueListSnapshot = null;
}
if (ps != null && addedBatchSize > 0) {
if (canBatchUpdate) {
// mainly here
ps.executeBatch();
}
commitTransaction(conn);
committedRowCount = committedRowCount + addedBatchSize;
}
noticeLoadedRowSize(tableDbName, rowNumber);
resultInfo.registerLoadedMeta(dataDirectory, _filePath, rowNumber);
checkImplicitClassification(dataFile, tableDbName, columnNameList);
} catch (SQLException e) {
// request retry if it needs (e.g. execution exception of batch insert)
// the snapshot is used only when retry failure basically
final DfJDBCException wrapped = DfJDBCException.voice(e);
final String msg = buildFailureMessage(_filePath, tableDbName, executedSql, columnValueList, wrapped);
throw new DfDelimiterDataRegistrationFailureException(msg, wrapped.getNextException()).retryIfNeeds(createRetryResource(canBatchUpdate, committedRowCount)).snapshotRow(createRowSnapshot(columnNameList, valueListSnapshot, rowNumber));
} catch (RuntimeException e) {
// unneeded snapshot at this side but just in case (or changing determination future)
final String msg = buildFailureMessage(_filePath, tableDbName, executedSql, columnValueList, null);
throw new DfDelimiterDataRegistrationFailureException(msg, e).snapshotRow(createRowSnapshot(columnNameList, valueListSnapshot, rowNumber));
} finally {
closeStream(fis, ir, br);
try {
rollbackTransaction(conn);
} catch (SQLException continued) {
_log.info("Failed to rollback the delimiter data transaction.", continued);
}
close(ps);
close(conn);
// process after (finally) handling table
finallyHandlingTable(tableDbName, columnMetaMap);
}
}
Aggregations