1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.transaction.xa;
  6. /** <p>The XAResource interface is a Java mapping of the industry standard
  7. * XA interface based on the X/Open CAE Specification (Distributed
  8. * Transaction Processing: The XA Specification).
  9. *
  10. * <p>The XA interface defines the contract between a Resource Manager
  11. * and a Transaction Manager in a distributed transaction processing
  12. * (DTP) environment. A JDBC driver or a JMS provider implements the
  13. * this interface to support association between a global transaction
  14. * and a database or message service connection.
  15. *
  16. * <p>The XAResource interface can be supported by any transactional
  17. * resource that is intended to be used by application programs in an
  18. * environment where transactions are controlled by an external
  19. * transaction manager. An example of such a resource is a database
  20. * management system. An application may access data through multiple
  21. * database connections. Each database connection is enlisted with
  22. * the transaction manager as a transactional resource. The transaction
  23. * manager obtains an XAResource for each connection participating
  24. * in a global transaction. The transaction manager uses the start method
  25. * to associate the global transaction with the resource, and it uses the
  26. * end method to disassociate the transaction from the resource. The resource
  27. * manager is responsible for associating the global transaction to all
  28. * work performed on its data between the start and end method invocation.
  29. *
  30. * <p>At transaction commit time, the resource managers are informed by
  31. * the transaction manager to prepare, commit, or rollback a transaction
  32. * according to the two-phase commit protocol.</p>
  33. *
  34. */
  35. public interface XAResource
  36. {
  37. /** Commit the global transaction specified by xid.
  38. *
  39. * @param xid A global transaction identifier
  40. *
  41. * @param onePhase If true, the resource manager should use a one-phase
  42. * commit protocol to commit the work done on behalf of xid.
  43. *
  44. * @exception XAException An error has occurred. Possible XAExceptions
  45. * are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
  46. * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
  47. *
  48. * <P>If the resource manager did not commit the transaction and the
  49. * paramether onePhase is set to true, the resource manager may throw
  50. * one of the XA_RB* exceptions. Upon return, the resource manager has
  51. * rolled back the branch's work and has released all held resources.
  52. */
  53. void commit(Xid xid, boolean onePhase) throws XAException;
  54. /** Ends the work performed on behalf of a transaction branch.
  55. * The resource manager disassociates the XA resource from the
  56. * transaction branch specified and let the transaction be
  57. * completed.
  58. *
  59. * <p>If TMSUSPEND is specified in flags, the transaction branch
  60. * is temporarily suspended in incomplete state. The transaction
  61. * context is in suspened state and must be resumed via start
  62. * with TMRESUME specified.</p>
  63. *
  64. * <p>If TMFAIL is specified, the portion of work has failed.
  65. * The resource manager may mark the transaction as rollback-only</p>
  66. *
  67. * <p>If TMSUCCESS is specified, the portion of work has completed
  68. * successfully.</p>
  69. *
  70. * @param xid A global transaction identifier that is the same as
  71. * what was used previously in the start method.
  72. *
  73. * @param flags One of TMSUCCESS, TMFAIL, or TMSUSPEND
  74. *
  75. * @exception XAException An error has occurred. Possible XAException
  76. * values are XAER_RMERR, XAER_RMFAILED, XAER_NOTA, XAER_INVAL,
  77. * XAER_PROTO, or XA_RB*.
  78. */
  79. void end(Xid xid, int flags) throws XAException;
  80. /** Tell the resource manager to forget about a heuristically
  81. * completed transaction branch.
  82. *
  83. * @param xid A global transaction identifier
  84. *
  85. * @exception XAException An error has occurred. Possible exception
  86. * values are XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or
  87. * XAER_PROTO.
  88. */
  89. void forget(Xid xid) throws XAException;
  90. /** Obtain the current transaction timeout value set for this
  91. * XAResource instance. If <CODE>XAResource.setTransactionTimeout</CODE>
  92. * was not use prior to invoking this method, the return value
  93. * is the default timeout set for the resource manager; otherwise,
  94. * the value used in the previous <CODE>setTransactionTimeout</CODE> call is
  95. * returned.
  96. *
  97. * @return the transaction timeout value in seconds.
  98. *
  99. * @exception XAException An error has occurred. Possible exception
  100. * values are XAER_RMERR, XAER_RMFAIL.
  101. */
  102. int getTransactionTimeout() throws XAException;
  103. /** This method is called to determine if the resource manager
  104. * instance represented by the target object is the same as the
  105. * resouce manager instance represented by the parameter <i>xares</i>.
  106. *
  107. * @param xares An XAResource object whose resource manager instance
  108. * is to be compared with the resource manager instance of the
  109. * target object.
  110. *
  111. * @return <i>true</i> if it's the same RM instance; otherwise
  112. * <i>false</i>.
  113. *
  114. * @exception XAException An error has occurred. Possible exception
  115. * values are XAER_RMERR, XAER_RMFAIL.
  116. *
  117. */
  118. boolean isSameRM(XAResource xares) throws XAException;
  119. /** Ask the resource manager to prepare for a transaction commit
  120. * of the transaction specified in xid.
  121. *
  122. * @param xid A global transaction identifier
  123. *
  124. * @exception XAException An error has occurred. Possible exception
  125. * values are: XA_RB*, XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL,
  126. * or XAER_PROTO.
  127. *
  128. * @return A value indicating the resource manager's vote on the
  129. * outcome of the transaction. The possible values are: XA_RDONLY
  130. * or XA_OK. If the resource manager wants to roll back the
  131. * transaction, it should do so by raising an appropriate XAException
  132. * in the prepare method.
  133. */
  134. int prepare(Xid xid) throws XAException;
  135. /** Obtain a list of prepared transaction branches from a resource
  136. * manager. The transaction manager calls this method during recovery
  137. * to obtain the list of transaction branches that are currently in
  138. * prepared or heuristically completed states.
  139. *
  140. * @param flag One of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS. TMNOFLAGS
  141. * must be used when no other flags are set in flags.
  142. *
  143. * @exception XAException An error has occurred. Possible values are
  144. * XAER_RMERR, XAER_RMFAIL, XAER_INVAL, and XAER_PROTO.
  145. *
  146. * @return The resource manager returns zero or more XIDs for the
  147. * transaction branches that are currently in a prepared or
  148. * heuristically completed state. If an error occurs during the
  149. * operation, the resource manager should throw the appropriate
  150. * XAException.
  151. *
  152. */
  153. Xid[] recover(int flag) throws XAException;
  154. /** Inform the resource manager to roll back work done on behalf
  155. * of a transaction branch
  156. *
  157. * @param xid A global transaction identifier
  158. *
  159. * @exception XAException An error has occurred
  160. */
  161. void rollback(Xid xid) throws XAException;
  162. /** <P>Set the current transaction timeout value for this <CODE>XAResource</CODE>
  163. * instance. Once set, this timeout value is effective until
  164. * <code>setTransactionTimeout</code> is invoked again with a different
  165. * value. To reset the timeout value to the default value used by the resource
  166. * manager, set the value to zero.
  167. *
  168. * If the timeout operation is performed successfully, the method returns
  169. * <i>true</i> otherwise <i>false</i>. If a resource manager does not
  170. * support transaction timeout value to be set explicitly, this method
  171. * returns <i>false</i>.
  172. *
  173. * @param the transaction timeout value in seconds.
  174. *
  175. * @return <i>true</i> if transaction timeout value is set successfully;
  176. * otherwise <i>false</i>.
  177. *
  178. * @exception XAException An error has occurred. Possible exception values
  179. * are XAER_RMERR, XAER_RMFAIL, or XAER_INVAL.
  180. */
  181. boolean setTransactionTimeout(int seconds) throws XAException;
  182. /** Start work on behalf of a transaction branch specified in xid
  183. *
  184. * If TMJOIN is specified, the start is for joining a transaction
  185. * previously seen by the resource manager. If TMRESUME is specified,
  186. * the start is to resume a suspended transaction specified in the
  187. * parameter xid.
  188. *
  189. * If neither TMJOIN nor TMRESUME is specified and the transaction
  190. * specified by xid has previously been seen by the resource manager,
  191. * the resource manager throws the XAException exception with
  192. * XAER_DUPID error code.
  193. *
  194. * @param xid A global transaction identifier to be associated
  195. * with the resource
  196. *
  197. * @param flags One of TMNOFLAGS, TMJOIN, or TMRESUME
  198. *
  199. * @exception XAException An error has occurred. Possible exceptions
  200. * are XA_RB*, XAER_RMERR, XAER_RMFAIL, XAER_DUPID, XAER_OUTSIDE,
  201. * XAER_NOTA, XAER_INVAL, or XAER_PROTO.
  202. *
  203. */
  204. void start(Xid xid, int flags) throws XAException;
  205. /**
  206. * End a recovery scan.
  207. */
  208. public final static int TMENDRSCAN = 0x00800000;
  209. /**
  210. * Disassociates the caller and mark the transaction branch
  211. * rollback-only.
  212. */
  213. public final static int TMFAIL = 0x20000000;
  214. /**
  215. * Caller is joining existing transaction branch.
  216. */
  217. public final static int TMJOIN = 0x00200000;
  218. /**
  219. * Use TMNOFLAGS to indicate no flags value is selected.
  220. */
  221. public final static int TMNOFLAGS = 0x00000000;
  222. /**
  223. * Caller is using one-phase optimization.
  224. */
  225. public final static int TMONEPHASE = 0x40000000;
  226. /**
  227. * Caller is resuming association with with suspended
  228. * transaction branch.
  229. */
  230. public final static int TMRESUME = 0x08000000;
  231. /**
  232. * Start a recovery scan.
  233. */
  234. public final static int TMSTARTRSCAN = 0x01000000;
  235. /**
  236. * Disassociate caller from transaction branch.
  237. */
  238. public final static int TMSUCCESS = 0x04000000;
  239. /**
  240. * Caller is suspending (not ending) association with
  241. * transaction branch.
  242. */
  243. public final static int TMSUSPEND = 0x02000000;
  244. /**
  245. * The transaction branch has been read-only and has been committed.
  246. */
  247. public final static int XA_RDONLY = 0x00000003;
  248. /**
  249. * The transaction work has been prepared normally.
  250. */
  251. public final static int XA_OK = 0;
  252. }