PageRenderTime 30ms CodeModel.GetById 24ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/appengine/datanucleus/DatastoreXAResource.java

http://datanucleus-appengine.googlecode.com/
Java | 97 lines | 61 code | 10 blank | 26 comment | 12 complexity | 1a57cf7c732e0348105dc4604488890e MD5 | raw file
 1/**********************************************************************
 2Copyright (c) 2009 Google Inc.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15**********************************************************************/
16package com.google.appengine.datanucleus;
17
18import com.google.appengine.api.datastore.DatastoreService;
19import com.google.appengine.api.datastore.Transaction;
20import com.google.appengine.api.datastore.TransactionOptions;
21
22import org.datanucleus.util.NucleusLogger;
23
24import javax.transaction.xa.XAException;
25import javax.transaction.xa.Xid;
26
27/**
28 * Extension to {@link EmulatedXAResource} that manages a transaction against the datastore.
29 * Currently only supports a single, non-distributed transaction.  Instances of this class are
30 * instantiated and used when the datasource has been configured as "autoCreateTransaction" or the user is
31 * explicitly doing transaction management.
32 *
33 * @author Erick Armbrust <earmbrust@google.com>
34 * @author Max Ross <maxr@google.com>
35 */
36class DatastoreXAResource extends EmulatedXAResource {
37
38  /** The current datastore transaction. */
39  private DatastoreTransaction currentTxn;
40  private final TransactionOptions txnOpts;
41
42  public DatastoreXAResource(DatastoreService datastoreService, TransactionOptions txnOpts) {
43    super(datastoreService);
44    this.txnOpts = txnOpts;
45  }
46
47  @Override
48  DatastoreTransaction getCurrentTransaction() {
49    return currentTxn;
50  }
51
52  @Override
53  public void start(Xid xid, int flags) throws XAException {
54    super.start(xid, flags);
55    if (currentTxn == null) {
56      // No currentTxn, and DatastoreService will have been created by DatastoreConnectionFactoryImpl, so call beginTxn
57      Transaction datastoreTxn = datastoreService.beginTransaction(txnOpts);
58      currentTxn = new DatastoreTransaction(datastoreTxn);
59      if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
60        NucleusLogger.TRANSACTION.debug(
61            LOCALISER.msg("AppEngine.Transaction.Started", currentTxn.getInnerTxn().getId()));
62      }
63    } else {
64      throw new XAException(LOCALISER.msg("AppEngine.Transaction.AlreadyStarted"));
65    }
66  }
67
68  @Override
69  public void commit(Xid xid, boolean onePhase) throws XAException {
70    super.commit(xid, onePhase);
71    if (currentTxn != null) {
72      currentTxn.commit();
73      if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
74        NucleusLogger.TRANSACTION.debug(
75            LOCALISER.msg("AppEngine.Transaction.Committed", currentTxn.getInnerTxn().getId()));
76      }
77      currentTxn = null;
78    } else {
79      throw new XAException(LOCALISER.msg("AppEngine.Transaction.CommitInvalid"));
80    }
81  }
82
83  @Override
84  public void rollback(Xid xid) throws XAException {
85    super.rollback(xid);
86    if (currentTxn != null) {
87      currentTxn.rollback();
88      if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
89        NucleusLogger.TRANSACTION.debug(
90            LOCALISER.msg("AppEngine.Transaction.RolledBack", currentTxn.getInnerTxn().getId()));
91      }
92      currentTxn = null;
93    } else {
94      throw new XAException(LOCALISER.msg("AppEngine.Transaction.RollbackInvalid"));
95    }
96  }
97}