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