X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FTransaction.java;h=0a8dcea413295cdf1cd436d6a2b04995f4642b39;hb=3e6b970912a5b3a61fc594c5464f1e2582ed0b3c;hp=f3d38c5eb5256262a7151e50cddef35c3efdd26a;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Transaction.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Transaction.java
index f3d38c5eb..0a8dcea41 100644
--- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Transaction.java
+++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Transaction.java
@@ -1,702 +1,692 @@
-/*******************************************************************************
- * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
- * 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:
- * VTT Technical Research Centre of Finland - initial API and implementation
- *******************************************************************************/
-package org.simantics.db.common.utils;
-
-import java.util.Collection;
-import java.util.Set;
-import java.util.concurrent.Semaphore;
-
-import org.simantics.databoard.accessor.Accessor;
-import org.simantics.databoard.binding.Binding;
-import org.simantics.databoard.type.Datatype;
-import org.simantics.db.ReadGraph;
-import org.simantics.db.RequestProcessor;
-import org.simantics.db.Resource;
-import org.simantics.db.Statement;
-import org.simantics.db.VirtualGraph;
-import org.simantics.db.WriteGraph;
-import org.simantics.db.WriteOnlyGraph;
-import org.simantics.db.common.request.DelayedWriteRequest;
-import org.simantics.db.exception.AdaptionException;
-import org.simantics.db.exception.BindingException;
-import org.simantics.db.exception.CancelTransactionException;
-import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.exception.DoesNotContainValueException;
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
-import org.simantics.db.exception.NoInverseException;
-import org.simantics.db.exception.NoSingleResultException;
-import org.simantics.db.exception.ResourceNotFoundException;
-import org.simantics.db.exception.RuntimeDatabaseException;
-import org.simantics.db.exception.ServiceException;
-import org.simantics.db.exception.ValidationException;
-import org.simantics.db.procedure.Procedure;
-import org.simantics.db.request.DelayedWrite;
-import org.simantics.db.request.Read;
-import org.simantics.db.request.UndoTraits;
-import org.simantics.db.request.Write;
-import org.simantics.utils.datastructures.Callback;
-
-/**
- * Synchronous Transaction.
- *
- * Hint: Import all methods as static.
- * import static org.simantics.db.Transaction.*;
- *
- * Remember also to change Eclipse Preferences:
- * Organize Imports: number of static imports needed for .* on vakiona 99, siihen vaikka 3
- *
- *
- * Usage A:
- *
- * startTransaction(session, true);
- * try {
- * ...
- * commit();
- * } finally {
- * endTransaction();
- * }
- *
- * Usage B:
- * setGraph(g);
- * ...
- * setGraph(null);
- *
- * @author Toni Kalajainen
- */
-public class Transaction {
-
- public static enum Type {
- READ,
- WRITE,
- DELAYED_WRITE
- }
-
- /** Thread local transactions */
- private static ThreadLocal transactions = new ThreadLocal();
-
- static private class TransactionInfo {
- WriteGraph wg;
- ReadGraph rg;
-
- /** Semaphore that is released when the transaction is complete */
- Semaphore ts;
-
- /** Semaphore that is release after transaction ended */
- Semaphore es;
-
- /** Error */
- DatabaseException error;
-
- /** */
- boolean commit = false;
-
- TransactionInfo() {
- }
-
- TransactionInfo(Object graph) {
- if (graph instanceof WriteGraph) {
- wg = (WriteGraph) graph;
- rg = (ReadGraph) graph;
- } else if (graph instanceof ReadGraph) {
- wg = null;
- rg = (ReadGraph) graph;
- } else {
- throw new RuntimeDatabaseException("Not a sync graph");
- }
- }
- }
-
- public static ReadGraph readGraph() {
- TransactionInfo t = transactions.get();
- return t == null ? null : t.rg;
- }
-
- public static WriteGraph writeGraph() {
- TransactionInfo t = transactions.get();
- return t == null ? null : t.wg;
- }
-
- public static Object setGraph(Object graph) {
- if (graph==null) {
- transactions.set(null);
- return null;
- }
-
- {
- Object oldGraph = null;
- TransactionInfo t = transactions.get();
- if (t!=null) {
- oldGraph = t.rg;
- if (graph instanceof WriteGraph) {
- t.wg = (WriteGraph) graph;
- t.rg = (ReadGraph) graph;
- } else if (graph instanceof ReadGraph) {
- t.wg = null;
- t.rg = (ReadGraph) graph;
- } else {
- throw new RuntimeDatabaseException("Not a sync graph");
- }
- } else {
- t = new TransactionInfo(graph);
- }
- transactions.set(t);
- return oldGraph;
- }
- }
-
- public static void startTransaction(RequestProcessor processor, boolean write) throws DatabaseException {
- startTransaction(processor, write ? Type.WRITE : Type.READ);
- }
-
- public static void startTransaction(RequestProcessor processor, Type type) throws DatabaseException {
- switch (type) {
- case READ:
- {
- if (transactions.get()!=null) throw new RuntimeDatabaseException("There is already a transaction.");
- final Semaphore started = new Semaphore(0);
- final TransactionInfo t = new TransactionInfo();
- t.es = new Semaphore(0);
- t.ts = new Semaphore(0);
- transactions.set(t);
-
- Read