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

http://datanucleus-appengine.googlecode.com/ · Java · 125 lines · 67 code · 22 blank · 36 comment · 6 complexity · 4414ba05c4582425cdb89ea3912637fc MD5 · raw file

  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 javax.transaction.xa.XAException;
  15. import javax.transaction.xa.XAResource;
  16. import javax.transaction.xa.Xid;
  17. import org.datanucleus.util.Localiser;
  18. import com.google.appengine.api.datastore.DatastoreService;
  19. /**
  20. * This emulated XAResource only supports a small subset of XA functionality.
  21. * There's no underlying transaction, just some simple state management.
  22. * Instances of this class are instantiated and used when the datasource has
  23. * been configured as nontransactional and the user is not explicitly doing
  24. * any transaction management.
  25. *
  26. * @author Erick Armbrust <earmbrust@google.com>
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. class EmulatedXAResource implements XAResource {
  30. protected static final Localiser LOCALISER = Localiser.getInstance(
  31. "com.google.appengine.datanucleus.Localisation", DatastoreManager.class.getClassLoader());
  32. private enum State {NEW, ACTIVE, INACTIVE}
  33. private State state = State.NEW;
  34. private final KeyRegistry keyRegistry = new KeyRegistry();
  35. /** The datastore service we'll use to perform datastore operations. */
  36. protected final DatastoreService datastoreService;
  37. public EmulatedXAResource(DatastoreService ds) {
  38. this.datastoreService = ds;
  39. }
  40. public void start(Xid xid, int flags) throws XAException {
  41. if (state != State.NEW) {
  42. throw new XAException("Nested transactions are not supported");
  43. }
  44. state = State.ACTIVE;
  45. }
  46. public void commit(Xid xid, boolean onePhase) throws XAException {
  47. if (state != State.ACTIVE) {
  48. throw new XAException(LOCALISER.msg("AppEngine.Transaction.CommitInvalid"));
  49. }
  50. keyRegistry.clearParentKeys();
  51. keyRegistry.clearUnownedObjects();
  52. state = State.INACTIVE;
  53. }
  54. public void rollback(Xid xid) throws XAException {
  55. if (state != State.ACTIVE) {
  56. throw new XAException(LOCALISER.msg("AppEngine.Transaction.RollbackInvalid"));
  57. }
  58. keyRegistry.clearParentKeys();
  59. keyRegistry.clearUnownedObjects();
  60. state = State.INACTIVE;
  61. }
  62. public int prepare(Xid xid) throws XAException {
  63. return XA_OK;
  64. }
  65. public Xid[] recover(int flag) throws XAException {
  66. throw new XAException("Unsupported operation");
  67. }
  68. public void end(Xid xid, int flags) throws XAException {
  69. // TODO (earmbrust): Should we throw an unsupported error?
  70. }
  71. public void forget(Xid xid) throws XAException {
  72. // TODO (earmbrust): Should we throw an unsupported error?
  73. }
  74. public int getTransactionTimeout() throws XAException {
  75. return 0;
  76. }
  77. public boolean setTransactionTimeout(int seconds) throws XAException {
  78. return false;
  79. }
  80. public boolean isSameRM(XAResource xares) throws XAException {
  81. // We only support a single datastore, so this should always be true.
  82. return true;
  83. }
  84. KeyRegistry getKeyRegistry() {
  85. return keyRegistry;
  86. }
  87. /**
  88. * Accessor for the DatastoreService of this connection.
  89. * @return DatastoreService being used
  90. */
  91. DatastoreService getDatastoreService() {
  92. return datastoreService;
  93. }
  94. /**
  95. * @return The current transaction, or {@code null} if the datasource does not support transactions.
  96. */
  97. DatastoreTransaction getCurrentTransaction() {
  98. return null;
  99. }
  100. }