Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3ebb6f630434b2dbf9e0838d5462e09a92c7be2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.database.core;

import java.util.logging.Level;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;

public final class DatabaseTransactions {

   private DatabaseTransactions() {
      // Utility class
   }

   public static void execute(IOseeDatabaseService dbService, OseeConnection connection, IDbTransactionWork dbWork) throws OseeCoreException {
      boolean initialAutoCommit = true;
      Exception saveException = null;
      try {
         OseeLog.logf(DatabaseTransactions.class, Level.FINEST, "Start Transaction: [%s]", dbWork.getName());

         initialAutoCommit = connection.getAutoCommit();
         connection.setAutoCommit(false);
         setConstraintChecking(dbService, connection, false);
         dbWork.handleTxWork(connection);

         connection.commit();
         OseeLog.logf(DatabaseTransactions.class, Level.FINEST, "End Transaction: [%s]", dbWork.getName());
      } catch (Exception ex) {
         saveException = ex;
         try {
            connection.rollback();
         } finally {
            try {
               connection.destroy();
            } finally {
               dbWork.handleTxException(ex);
            }
         }
      } finally {
         try {
            try {
               if (!connection.isClosed()) {
                  try {
                     setConstraintChecking(dbService, connection, true);
                  } catch (OseeCoreException ex) {
                     OseeLog.log(DatabaseTransactions.class, Level.WARNING, "Error while enabling constraint checking",
                        ex);
                  } finally {
                     connection.setAutoCommit(initialAutoCommit);
                     connection.close();
                  }
               }
            } finally {
               dbWork.handleTxFinally();
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(DatabaseTransactions.class, Level.SEVERE, ex);
            if (saveException == null) {
               saveException = ex;
            }
         }

         if (saveException != null) {
            OseeExceptions.wrapAndThrow(saveException);
         }
      }
   }

   private static void setConstraintChecking(IOseeDatabaseService dbService, OseeConnection connection, boolean enable) throws OseeCoreException {
      String cmd = null;
      SupportedDatabase dbType = SupportedDatabase.getDatabaseType(connection.getMetaData());
      switch (dbType) {
         case h2:
            cmd = String.format("SET REFERENTIAL_INTEGRITY = %s", Boolean.toString(enable).toUpperCase());
            break;
         case hsql:
            cmd = String.format("SET DATABASE REFERENTIAL INTEGRITY %s", Boolean.toString(enable).toUpperCase());
            break;
         default:
            // NOTE: this must be a PreparedStatement to play correctly with DB Transactions.
            cmd = String.format("SET CONSTRAINTS ALL %s", enable ? "IMMEDIATE" : "DEFERRED");
            break;
      }
      dbService.runPreparedUpdate(connection, cmd);
   }
}

Back to the top