| 1 | |
package org.apache.ojb.otm.core; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.Iterator; |
| 20 | |
import java.util.List; |
| 21 | |
|
| 22 | |
import org.apache.ojb.otm.OTMKit; |
| 23 | |
import org.apache.ojb.otm.OTMConnection; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class Transaction |
| 32 | |
{ |
| 33 | |
private boolean _isInProgress; |
| 34 | |
private List _listeners; |
| 35 | |
private List _connections; |
| 36 | |
private OTMKit _kit; |
| 37 | |
|
| 38 | |
public Transaction() |
| 39 | |
{ |
| 40 | |
_listeners = new ArrayList(); |
| 41 | |
_connections = new ArrayList(); |
| 42 | |
} |
| 43 | |
|
| 44 | |
public OTMKit getKit() |
| 45 | |
{ |
| 46 | |
return _kit; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public void setKit(OTMKit kit) |
| 50 | |
{ |
| 51 | |
_kit = kit; |
| 52 | |
} |
| 53 | |
|
| 54 | |
public void begin() |
| 55 | |
throws TransactionException |
| 56 | |
{ |
| 57 | |
if (_isInProgress) |
| 58 | |
{ |
| 59 | |
throw new TransactionInProgressException( |
| 60 | |
"Transaction already in progress, cannot restart"); |
| 61 | |
} |
| 62 | |
|
| 63 | |
_isInProgress = true; |
| 64 | |
|
| 65 | |
for (Iterator iterator = _connections.iterator(); iterator.hasNext();) |
| 66 | |
{ |
| 67 | |
BaseConnection connection = (BaseConnection) iterator.next(); |
| 68 | |
connection.transactionBegin(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
for (Iterator iterator = _listeners.iterator(); iterator.hasNext();) |
| 72 | |
{ |
| 73 | |
TransactionListener listener = (TransactionListener) iterator.next(); |
| 74 | |
listener.transactionBegan(this); |
| 75 | |
} |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public void commit() |
| 87 | |
throws TransactionException |
| 88 | |
{ |
| 89 | |
if (!_isInProgress) |
| 90 | |
{ |
| 91 | |
throw new TransactionNotInProgressException( |
| 92 | |
"Transaction not in progress, nothing to commit"); |
| 93 | |
} |
| 94 | |
|
| 95 | |
for (Iterator iterator = _listeners.iterator(); iterator.hasNext();) |
| 96 | |
{ |
| 97 | |
TransactionListener listener = (TransactionListener) iterator.next(); |
| 98 | |
|
| 99 | |
listener.transactionCommitting(this); |
| 100 | |
} |
| 101 | |
|
| 102 | |
for (Iterator iterator = _connections.iterator(); iterator.hasNext();) |
| 103 | |
{ |
| 104 | |
BaseConnection connection = (BaseConnection) iterator.next(); |
| 105 | |
ConcreteEditingContext context = |
| 106 | |
(ConcreteEditingContext) connection.getEditingContext(); |
| 107 | |
|
| 108 | |
context.commit(); |
| 109 | |
connection.transactionPrepare(); |
| 110 | |
} |
| 111 | |
|
| 112 | |
for (Iterator iterator = _connections.iterator(); iterator.hasNext();) |
| 113 | |
{ |
| 114 | |
BaseConnection connection = (BaseConnection) iterator.next(); |
| 115 | |
|
| 116 | |
connection.transactionCommit(); |
| 117 | |
connection.setTransaction(null); |
| 118 | |
} |
| 119 | |
|
| 120 | |
_connections.clear(); |
| 121 | |
_isInProgress = false; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public void checkpoint() |
| 130 | |
throws TransactionException |
| 131 | |
{ |
| 132 | |
if (!_isInProgress) |
| 133 | |
{ |
| 134 | |
throw new TransactionNotInProgressException( |
| 135 | |
"Transaction not in progress, cannot checkpoint"); |
| 136 | |
} |
| 137 | |
|
| 138 | |
for (Iterator iterator = _connections.iterator(); iterator.hasNext();) |
| 139 | |
{ |
| 140 | |
BaseConnection connection = (BaseConnection) iterator.next(); |
| 141 | |
ConcreteEditingContext context = |
| 142 | |
(ConcreteEditingContext) connection.getEditingContext(); |
| 143 | |
|
| 144 | |
context.checkpoint(); |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public void rollback() |
| 155 | |
throws TransactionException |
| 156 | |
{ |
| 157 | |
if (!_isInProgress) |
| 158 | |
{ |
| 159 | |
throw new TransactionNotInProgressException( |
| 160 | |
"Transaction not in progress, nothing to commit"); |
| 161 | |
} |
| 162 | |
|
| 163 | |
for (Iterator iterator = _listeners.iterator(); iterator.hasNext();) |
| 164 | |
{ |
| 165 | |
TransactionListener listener = (TransactionListener) iterator.next(); |
| 166 | |
listener.transactionRollingBack(this); |
| 167 | |
} |
| 168 | |
|
| 169 | |
for (Iterator iterator = _connections.iterator(); iterator.hasNext();) |
| 170 | |
{ |
| 171 | |
BaseConnection connection = (BaseConnection) iterator.next(); |
| 172 | |
ConcreteEditingContext context = |
| 173 | |
(ConcreteEditingContext) connection.getEditingContext(); |
| 174 | |
|
| 175 | |
context.rollback(); |
| 176 | |
connection.transactionRollback(); |
| 177 | |
connection.setTransaction(null); |
| 178 | |
} |
| 179 | |
|
| 180 | |
_connections.clear(); |
| 181 | |
_isInProgress = false; |
| 182 | |
} |
| 183 | |
|
| 184 | |
public boolean isInProgress() |
| 185 | |
{ |
| 186 | |
return _isInProgress; |
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public void registerConnection(OTMConnection connection) |
| 198 | |
{ |
| 199 | |
Transaction connectionTx = connection.getTransaction(); |
| 200 | |
if ((connectionTx != null) && (connectionTx != this)) |
| 201 | |
{ |
| 202 | |
throw new TransactionException( |
| 203 | |
"Attempt to re-assign a different transaction to a open connection"); |
| 204 | |
} |
| 205 | |
|
| 206 | |
if (!_connections.contains(connection)) |
| 207 | |
{ |
| 208 | |
_connections.add(connection); |
| 209 | |
|
| 210 | |
connection.setTransaction(this); |
| 211 | |
|
| 212 | |
} |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
public void registerListener(TransactionListener listener) |
| 224 | |
{ |
| 225 | |
if (_listeners.indexOf(listener) < 0) |
| 226 | |
{ |
| 227 | |
_listeners.add(listener); |
| 228 | |
} |
| 229 | |
} |
| 230 | |
} |