You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2007/03/27 01:06:22 UTC

svn commit: r522656 - in /db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction: TransactionControl.java TransactionListener.java

Author: djd
Date: Mon Mar 26 16:06:21 2007
New Revision: 522656

URL: http://svn.apache.org/viewvc?view=rev&rev=522656
Log:
DERBY-2485 Example code for a simple transaction listener scheme. Not yet used or
compiled by the build.xml files.

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionControl.java   (with props)
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionListener.java   (with props)

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

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionControl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionListener.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionListener.java?view=auto&rev=522656
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionListener.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionListener.java Mon Mar 26 16:06:21 2007
@@ -0,0 +1,60 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.transaction.TransactionListener
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+package org.apache.derby.iapi.transaction;
+
+import org.apache.derby.iapi.error.StandardException;
+
+/**
+ * An interface that must be implemented by a object that
+ * wants to be notified when a significant transaction event occurs.
+ */
+public interface TransactionListener {
+    
+    /**
+     * Notifies registered listener that the transaction
+     * is about to commit. Called before the commit is
+     * recorded and flushed to the transaction log device.
+     * 
+     * @return true to remove this listener once this
+     * method returns.
+     * 
+     * @throws StandardException If thrown the commit attempt
+     * will be stopped and instead the transaction will be rolled back.
+     */
+    boolean preCommit() throws StandardException;
+    
+    /**
+     * Notifies registered listener that the transaction
+     * is about to rollback. Called before any physical rollback.
+     * The listener will be removed from the current transaction
+     * once the method returns.
+     * 
+     * @throws StandardException If thrown the rollback attempt
+     * will be stopped and instead the database will be shut down.
+     * 
+     * TODO: Define behaviour on exception during rollback.
+     */
+    void preRollback() throws StandardException;
+
+    // to support statement/savepoint rollback.
+    // void preSavepointRollback() throws StandardException;
+
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/transaction/TransactionListener.java
------------------------------------------------------------------------------
    svn:eol-style = native