KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > TransactionResultIteratorDecorator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.cayenne.CayenneException;
27 import org.apache.cayenne.map.DbEntity;
28
29 /**
30  * Decorates ResultIterator to close active transaction when the iterator is closed.
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 final class TransactionResultIteratorDecorator implements ResultIterator {
36
37     private ResultIterator result;
38     private Transaction tx;
39
40     public TransactionResultIteratorDecorator(ResultIterator result, Transaction tx) {
41         this.result = result;
42         this.tx = tx;
43     }
44
45     /**
46      * Closes the result and commits the transaction.
47      */

48     public void close() throws CayenneException {
49
50         try {
51             result.close();
52             tx.commit();
53         }
54         catch (Exception JavaDoc e) {
55             try {
56                 tx.rollback();
57             }
58             catch (Exception JavaDoc rollbackEx) {
59             }
60
61             throw new CayenneException(e);
62         }
63         finally {
64             if(Transaction.getThreadTransaction() == tx) {
65                 Transaction.bindThreadTransaction(null);
66             }
67         }
68     }
69
70     public List JavaDoc dataRows(boolean close) throws CayenneException {
71         List JavaDoc list = new ArrayList JavaDoc();
72
73         try {
74             while (hasNextRow()) {
75                 list.add(nextDataRow());
76             }
77         }
78         finally {
79             if (close) {
80                 close();
81             }
82         }
83
84         return list;
85     }
86
87     public int getDataRowWidth() {
88         return result.getDataRowWidth();
89     }
90
91     public boolean hasNextRow() throws CayenneException {
92         return result.hasNextRow();
93     }
94
95     public Map JavaDoc nextDataRow() throws CayenneException {
96         return result.nextDataRow();
97     }
98
99     public Map JavaDoc nextObjectId(DbEntity entity) throws CayenneException {
100         return result.nextObjectId(entity);
101     }
102
103     public void skipDataRow() throws CayenneException {
104         result.skipDataRow();
105     }
106 }
107
Popular Tags