Advertisement
broken-arrow

Untitled

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