]> source.dussan.org Git - sonarqube.git/commitdiff
Revert "Improve db migration so that multiple SELECT can be concurrently executed"
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 31 Oct 2014 10:59:31 +0000 (11:59 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 31 Oct 2014 10:59:31 +0000 (11:59 +0100)
This reverts commit 1cb697d9bf178093e6ceaaaf33bfa989f79f6469.

server/sonar-server/src/main/java/org/sonar/server/db/migrations/BaseDataChange.java
server/sonar-server/src/main/java/org/sonar/server/db/migrations/DataChange.java

index 58304aa3d1996d680a4f75251b0122b5dbca580a..32ebbaf443e19e067b7a2131850bb47d13ea988c 100644 (file)
  */
 package org.sonar.server.db.migrations;
 
+import org.apache.commons.dbutils.DbUtils;
 import org.sonar.core.persistence.Database;
 
+import java.sql.Connection;
 import java.sql.SQLException;
 
 public abstract class BaseDataChange implements DataChange, DatabaseMigration {
@@ -33,11 +35,22 @@ public abstract class BaseDataChange implements DataChange, DatabaseMigration {
 
   @Override
   public final void execute() throws SQLException {
-    Context context = new Context(db);
+    Connection readConnection = null, writeConnection = null;
     try {
+      readConnection = db.getDataSource().getConnection();
+      readConnection.setAutoCommit(false);
+      if (readConnection.getMetaData().supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED)) {
+        readConnection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+      }
+
+      writeConnection = db.getDataSource().getConnection();
+      writeConnection.setAutoCommit(false);
+      Context context = new Context(db, readConnection, writeConnection);
       execute(context);
+
     } finally {
-      context.close();
+      DbUtils.closeQuietly(readConnection);
+      DbUtils.closeQuietly(writeConnection);
     }
   }
 }
index dff05618251f3e0a87d499b06b5327cd7e7c57f4..c901a7825948a05494c75739aa17c23959259ea5 100644 (file)
  */
 package org.sonar.server.db.migrations;
 
-import com.google.common.collect.Lists;
-import org.apache.commons.dbutils.DbUtils;
 import org.sonar.core.persistence.Database;
 
 import java.sql.Connection;
 import java.sql.SQLException;
-import java.util.List;
 
 public interface DataChange {
 
   class Context {
     private final Database db;
-    private final List<Connection> connections = Lists.newArrayList();
+    private final Connection readConnection, writeConnection;
 
-    Context(Database db) {
+    public Context(Database db, Connection readConnection, Connection writeConnection) {
       this.db = db;
+      this.readConnection = readConnection;
+      this.writeConnection = writeConnection;
     }
 
     public Select prepareSelect(String sql) throws SQLException {
-      return SelectImpl.create(db, openReadConnection(), sql);
+      return SelectImpl.create(db, readConnection, sql);
     }
 
     public Upsert prepareUpsert(String sql) throws SQLException {
-      return UpsertImpl.create(openWriteConnection(), sql);
+      return UpsertImpl.create(writeConnection, sql);
     }
 
     public MassUpdate prepareMassUpdate() throws SQLException {
-      return new MassUpdate(db, openReadConnection(), openWriteConnection());
-    }
-
-    private Connection openReadConnection() throws SQLException {
-      Connection readConnection = db.getDataSource().getConnection();
-      readConnection.setAutoCommit(false);
-      if (readConnection.getMetaData().supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED)) {
-        readConnection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
-      }
-      connections.add(readConnection);
-      return readConnection;
-    }
-
-    private Connection openWriteConnection() throws SQLException {
-      Connection writeConnection = db.getDataSource().getConnection();
-      writeConnection.setAutoCommit(false);
-      connections.add(writeConnection);
-      return writeConnection;
-    }
-
-    void close() {
-      for (Connection connection : connections) {
-        DbUtils.closeQuietly(connection);
-      }
-      connections.clear();
+      return new MassUpdate(db, readConnection, writeConnection);
     }
   }