cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r405244 - in /incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access: DataContext.java jdbc/JDBCResultIterator.java
Date Tue, 09 May 2006 00:33:55 GMT
Author: aadamchik
Date: Mon May  8 17:33:53 2006
New Revision: 405244

URL: http://svn.apache.org/viewcvs?rev=405244&view=rev
Log:
CAY-545

Modified:
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/DataContext.java
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/jdbc/JDBCResultIterator.java

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/DataContext.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/DataContext.java?rev=405244&r1=405243&r2=405244&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/DataContext.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/DataContext.java
Mon May  8 17:33:53 2006
@@ -934,8 +934,8 @@
 
             public boolean visitCollectionArc(CollectionProperty property) {
                 property.injectValueHolder(object);
-                
-                if(!property.isFault(object)) {
+
+                if (!property.isFault(object)) {
                     Iterator it = ((Collection) property.readProperty(object)).iterator();
                     while (it.hasNext()) {
                         Object target = it.next();
@@ -961,7 +961,7 @@
                 if (target instanceof DataObject) {
 
                     DataObject targetDO = (DataObject) target;
-                    
+
                     // make sure it is registered
                     registerNewObject(targetDO);
                     getObjectStore().recordArcCreated(
@@ -977,6 +977,7 @@
             }
         });
     }
+
     /**
      * Unregisters a Collection of DataObjects from the DataContext and the underlying
      * ObjectStore. This operation also unsets DataContext and ObjectId for each object
@@ -1298,10 +1299,57 @@
     }
 
     /**
-     * Performs a single database select query returning result as a ResultIterator.
-     * Returned ResultIterator will provide access to DataRows.
+     * Performs a single database select query returning result as a ResultIterator. It is
+     * caller's responsibility to explicitly close the ResultIterator. A failure to do so
+     * will result in a database connection not being released. Another side effect of an
+     * open ResultIterator is that an internal Cayenne transaction that originated in this
+     * method stays open until the iterator is closed. So users should normally close the
+     * iterator within the same thread that opened it.
      */
     public ResultIterator performIteratedQuery(Query query) throws CayenneException {
+        if (Transaction.getThreadTransaction() != null) {
+            return internalPerformIteratedQuery(query);
+        }
+        else {
+
+            // manually manage a transaction, so that a ResultIterator wrapper could close
+            // it when it is done.
+            Transaction tx = getParentDataDomain().createTransaction();
+            Transaction.bindThreadTransaction(tx);
+
+            ResultIterator result;
+            try {
+                result = internalPerformIteratedQuery(query);
+            }
+            catch (Exception e) {
+                Transaction.bindThreadTransaction(null);
+                tx.setRollbackOnly();
+                throw new CayenneException(e);
+            }
+            finally {
+                // note: we are keeping the transaction bound to the current thread on
+                // success - iterator will unbind it. Unsetting a transaction here would
+                // result in some strangeness, at least on Ingres
+
+                if (tx.getStatus() == Transaction.STATUS_MARKED_ROLLEDBACK) {
+                    try {
+                        tx.rollback();
+                    }
+                    catch (Exception rollbackEx) {
+                    }
+                }
+            }
+
+            return new TransactionResultIteratorDecorator(result, tx);
+        }
+    }
+
+    /**
+     * Runs an iterated query in transactional context provided by the caller.
+     * 
+     * @since 1.2
+     */
+    ResultIterator internalPerformIteratedQuery(Query query) throws CayenneException {
         // note that for now DataChannel API does not support cursors (aka
         // ResultIterator), so we have to go directly to the DataDomain.
         IteratedSelectObserver observer = new IteratedSelectObserver();

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/jdbc/JDBCResultIterator.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/jdbc/JDBCResultIterator.java?rev=405244&r1=405243&r2=405244&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/jdbc/JDBCResultIterator.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/jdbc/JDBCResultIterator.java
Mon May  8 17:33:53 2006
@@ -237,6 +237,10 @@
                 }
             }
 
+            // TODO: andrus, 5/8/2006 - closing connection within JDBCResultIterator is
+            // obsolete as this is bound to transaction closing in DataContext. Deprecate
+            // this after 1.2
+            
             // close connection, if this object was explicitly configured to be
             // responsible for doing it
             if (connection != null && isClosingConnection()) {



Mime
View raw message