Advertisement
broken-arrow

Untitled

Dec 27th, 2021
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1.     protected final void batchUpdate(@NonNull List<String> batchupdate) {
  2.         ArrayList<String> sqls = new ArrayList<>(batchupdate);
  3.         this.connection = connect();
  4.  
  5.         if (sqls.size() == 0)
  6.             return;
  7.  
  8.         if (!hasStartWriteToDb)
  9.             try {
  10.                 hasStartWriteToDb = true;
  11.                 final Statement batchStatement = this.connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  12.                 final int processedCount = sqls.size();
  13.  
  14.                 // Prevent automatically sending db instructions
  15.                 this.connection.setAutoCommit(false);
  16.  
  17.  
  18.                 for (final String sql : sqls)
  19.                     batchStatement.addBatch(sql);
  20.                 if (processedCount > 10_000)
  21.                     CustomContainersMainClass.getInstance().sendLogMsg(Level.WARNING, "Updating your database (" + processedCount + " entries)... PLEASE BE PATIENT THIS WILL TAKE "
  22.                             + (processedCount > 50_000 ? "10-20 MINUTES" : "5-10 MINUTES") + " - If server will print a crash report, ignore it, update will proceed.");
  23.  
  24.                 // Set the flag to start time notifications timer
  25.                 batchUpdateGoingOn = true;
  26.  
  27.                 // Notify console that progress still is being made
  28.                 new Timer().scheduleAtFixedRate(new TimerTask() {
  29.  
  30.                     @Override
  31.                     public void run() {
  32.                         if (batchUpdateGoingOn)
  33.                             CustomContainersMainClass.getInstance().sendLogMsg(Level.WARNING, "Still executing, DO NOT SHUTDOWN YOUR SERVER.");
  34.                         else
  35.                             cancel();
  36.                     }
  37.                 }, 1000 * 30, 1000 * 30);
  38.  
  39.                 // Execute
  40.                 batchStatement.executeBatch();
  41.  
  42.                 // This will block the thread
  43.                 this.connection.commit();
  44.  
  45.                 CustomContainersMainClass.getInstance().sendLogMsg(Level.INFO, "Updated " + processedCount + " database entries.");
  46.  
  47.             } catch (final Throwable t) {
  48.                 t.printStackTrace();
  49.  
  50.             } finally {
  51.                 try {
  52.                     this.connection.setAutoCommit(true);
  53.                     this.connection.setAutoCommit(true);
  54.  
  55.                 } catch (final SQLException ex) {
  56.                     ex.printStackTrace();
  57.                 }
  58.                 hasStartWriteToDb = false;
  59.                 // Even in case of failure, cancel
  60.                 batchUpdateGoingOn = false;
  61.             }
  62.     }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement