improve log importer

- try importing line by line if the batch import did not work
- print more informative error messages
- fix writing failed imports to failed.txt
This commit is contained in:
Brokkonaut
2018-09-05 12:59:06 +02:00
parent c390504b70
commit 3c64376dd1

View File

@ -41,27 +41,51 @@ public class DumpedLogImporter implements Runnable {
String line = null; String line = null;
try { try {
logblock.getLogger().info("Trying to import " + sqlFile.getName() + " ..."); logblock.getLogger().info("Trying to import " + sqlFile.getName() + " ...");
// first try batch import the whole file
final BufferedReader reader = new BufferedReader(new FileReader(sqlFile)); final BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
int statements = 0;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.endsWith(";")) { if (line.endsWith(";")) {
line = line.substring(0, line.length() - 1); line = line.substring(0, line.length() - 1);
} }
if(!line.isEmpty()) { if (!line.isEmpty()) {
statements++;
st.addBatch(line); st.addBatch(line);
successes++;
} }
} }
st.executeBatch(); st.executeBatch();
conn.commit(); conn.commit();
reader.close(); reader.close();
sqlFile.delete(); sqlFile.delete();
successes += statements;
logblock.getLogger().info("Successfully imported " + sqlFile.getName() + "."); logblock.getLogger().info("Successfully imported " + sqlFile.getName() + ".");
} catch (final Exception ex) { } catch (final Exception ignored) {
logblock.getLogger().warning("Error while importing: '" + line + "': " + ex.getMessage()); // if the batch import did not work, retry line by line
writer.write(line + newline); try {
errors++; final BufferedReader reader = new BufferedReader(new FileReader(sqlFile));
ex.printStackTrace(); while ((line = reader.readLine()) != null) {
return; if (line.endsWith(";")) {
line = line.substring(0, line.length() - 1);
}
if (!line.isEmpty()) {
try {
st.execute(line);
successes++;
} catch (final SQLException ex) {
logblock.getLogger().severe("Error while importing: '" + line + "': " + ex.getMessage());
writer.write(line + newline);
errors++;
}
}
}
conn.commit();
reader.close();
sqlFile.delete();
logblock.getLogger().info("Successfully imported " + sqlFile.getName() + ".");
} catch (final Exception ex) {
logblock.getLogger().severe("Error while importing " + sqlFile.getName() + ": " + ex.getMessage());
errors++;
}
} }
} }
} finally { } finally {