1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.transaction;
  17. import java.sql.Connection;
  18. /**
  19. * Interface for classes that define transaction properties.
  20. * Base interface for TransactionAttribute.
  21. *
  22. * <p>Note that isolation level, timeout and read-only settings will only
  23. * get applied when starting a new transaction. As only PROPAGATION_REQUIRED
  24. * and PROPAGATION_REQUIRES_NEW can actually cause that, it doesn't make sense
  25. * to specify any of those settings else. Furthermore, not all transaction
  26. * managers will support those features and thus throw respective exceptions
  27. * when given non-default values.
  28. *
  29. * @author Juergen Hoeller
  30. * @since 08.05.2003
  31. * @see org.springframework.transaction.support.DefaultTransactionDefinition
  32. * @see org.springframework.transaction.interceptor.TransactionAttribute
  33. */
  34. public interface TransactionDefinition {
  35. String PROPAGATION_CONSTANT_PREFIX = "PROPAGATION";
  36. String ISOLATION_CONSTANT_PREFIX = "ISOLATION";
  37. /**
  38. * Support a current transaction, create a new one if none exists.
  39. * Analogous to EJB transaction attribute of the same name.
  40. * <p>This is typically the default setting of a transaction definition.
  41. */
  42. int PROPAGATION_REQUIRED = 0;
  43. /**
  44. * Support a current transaction, execute non-transactionally if none exists.
  45. * Analogous to EJB transaction attribute of the same name.
  46. */
  47. int PROPAGATION_SUPPORTS = 1;
  48. /**
  49. * Support a current transaction, throw an exception if none exists.
  50. * Analogous to EJB transaction attribute of the same name.
  51. */
  52. int PROPAGATION_MANDATORY = 2;
  53. /**
  54. * Create a new transaction, suspending the current transaction if one exists.
  55. * Analogous to EJB transaction attribute of the same name.
  56. */
  57. int PROPAGATION_REQUIRES_NEW = 3;
  58. /**
  59. * Execute non-transactionally, suspending the current transaction if one exists.
  60. * Analogous to EJB transaction attribute of the same name.
  61. */
  62. int PROPAGATION_NOT_SUPPORTED = 4;
  63. /**
  64. * Execute non-transactionally, throw an exception if a transaction exists.
  65. * Analogous to EJB transaction attribute of the same name.
  66. */
  67. int PROPAGATION_NEVER = 5;
  68. /**
  69. * Use the default isolation level of the underlying datastore.
  70. * All other levels correspond to the JDBC isolation levels.
  71. * @see java.sql.Connection
  72. */
  73. int ISOLATION_DEFAULT = -1;
  74. int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;
  75. int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
  76. int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;
  77. int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
  78. /**
  79. * Use the default timeout of the underlying transaction system,
  80. * respectively none if timeouts are not supported.
  81. */
  82. int TIMEOUT_DEFAULT = -1;
  83. /**
  84. * Return the propagation behavior.
  85. * Must return one of the PROPAGATION constants.
  86. * @see #PROPAGATION_REQUIRED
  87. */
  88. int getPropagationBehavior();
  89. /**
  90. * Return the isolation level.
  91. * Must return one of the ISOLATION constants.
  92. * <p>Only makes sense in combination with PROPAGATION_REQUIRED or
  93. * PROPAGATION_REQUIRES_NEW.
  94. * <p>Note that a transaction manager that does not support custom isolation levels
  95. * will throw an exception when given any other level than ISOLATION_DEFAULT.
  96. * @see #ISOLATION_DEFAULT
  97. */
  98. int getIsolationLevel();
  99. /**
  100. * Return the transaction timeout.
  101. * Must return a number of seconds, or TIMEOUT_DEFAULT.
  102. * <p>Only makes sense in combination with PROPAGATION_REQUIRED or
  103. * PROPAGATION_REQUIRES_NEW.
  104. * <p>Note that a transaction manager that does not support timeouts will
  105. * throw an exception when given any other timeout than TIMEOUT_DEFAULT.
  106. * @see #TIMEOUT_DEFAULT
  107. */
  108. int getTimeout();
  109. /**
  110. * Return whether to optimize as read-only transaction.
  111. * This just serves as hint for the actual transaction subsystem,
  112. * it will <i>not necessarily</i> cause failure of write accesses.
  113. * <p>Only makes sense in combination with PROPAGATION_REQUIRED or
  114. * PROPAGATION_REQUIRES_NEW.
  115. * <p>A transaction manager that cannot interpret the read-only hint
  116. * will <i>not</i> throw an exception when given readOnly=true.
  117. */
  118. boolean isReadOnly();
  119. }