--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.transaction.TransactionControl\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+package org.apache.derby.iapi.transaction;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Iterator;\r
+\r
+import org.apache.derby.iapi.error.ExceptionSeverity;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.reference.SQLState;\r
+\r
+/**\r
+ * Provide support to transactions to manage sets of\r
+ * actions to perform at transaction boundaries.\r
+ *\r
+ * <P> Add rollback of savepoints?\r
+ * TODO: A\r
+ */\r
+public final class TransactionControl {\r
+ \r
+ private final ArrayList listeners;\r
+ \r
+ public TransactionControl()\r
+ {\r
+ listeners = new ArrayList();\r
+ }\r
+ \r
+ /**\r
+ * Add a listener to the curent transaction.\r
+ * \r
+ * A listener may be added multiple times and it will\r
+ * receive multiple callbacks.\r
+ * \r
+ */\r
+ public void addListener(TransactionListener listener)\r
+ {\r
+ listeners.add(listener);\r
+ }\r
+ \r
+ /**\r
+ * Remove a listener from the current transaction.\r
+ * \r
+ */\r
+ public void removeListener(TransactionListener listener)\r
+ {\r
+ listeners.remove(listener);\r
+ }\r
+ \r
+ /**\r
+ * Notify all listeners that a commit is about to occur.\r
+ * If a listener throws an exception then no\r
+ * further listeners will be notified and a\r
+ * StandardException with rollback severity will be thrown.\r
+ * @throws StandardException\r
+ */\r
+ public void preCommitNotify() throws StandardException\r
+ {\r
+ if (listeners.isEmpty())\r
+ return;\r
+ \r
+ for (Iterator i = listeners.iterator(); i.hasNext(); )\r
+ {\r
+ TransactionListener listener =\r
+ (TransactionListener) i.next();\r
+ \r
+ try {\r
+ if(listener.preCommit())\r
+ i.remove();\r
+ }\r
+ catch (StandardException se) \r
+ { \r
+ // This catches any exceptions that have Transaction severity\r
+ // or less (e.g. Statement exception).\r
+ // If we received any lesser\r
+ // error then we abort the transaction anyway.\r
+ \r
+ if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY)\r
+ {\r
+ throw StandardException.newException(\r
+ SQLState.XACT_COMMIT_EXCEPTION, se);\r
+ }\r
+ \r
+ throw se;\r
+ \r
+ }\r
+ \r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Notify all listeners that a rollback is about to occur.\r
+ * If a listener throws an exception then no\r
+ * further listeners will be notified and a\r
+ * StandardException with shutdown database(?) severity will be thrown.\r
+ * @throws StandardException\r
+ */\r
+ public void preRollbackNotify() throws StandardException\r
+ { \r
+ if (listeners.isEmpty())\r
+ return;\r
+ \r
+ for (Iterator i = listeners.iterator(); i.hasNext(); )\r
+ {\r
+ TransactionListener listener =\r
+ (TransactionListener) i.next();\r
+ \r
+ try {\r
+ listener.preRollback();\r
+ i.remove();\r
+ } catch (StandardException se) {\r
+ // TODO: Define behaviour on exception during rollback.\r
+\r
+ if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY)\r
+ {\r
+ \r
+ }\r
+ throw se;\r
+ }\r
+ }\r
+ }\r
+}\r