SqlTransaction.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // System.Data.SqlClient.SqlTransaction.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. namespace System.Data.SqlClient {
  35. #if NET_2_0
  36. public sealed class SqlTransaction : DbTransaction, IDbTransaction, IDisposable
  37. #else
  38. public sealed class SqlTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  39. #endif // NET_2_0
  40. {
  41. #region Fields
  42. bool disposed = false;
  43. SqlConnection connection;
  44. IsolationLevel isolationLevel;
  45. bool isOpen;
  46. bool isRolledBack = false;
  47. #endregion
  48. #region Constructors
  49. internal SqlTransaction (SqlConnection connection, IsolationLevel isolevel)
  50. {
  51. this.connection = connection;
  52. this.isolationLevel = isolevel;
  53. isOpen = true;
  54. isRolledBack = false;
  55. }
  56. #endregion // Constructors
  57. #region Properties
  58. public
  59. #if NET_2_0
  60. new
  61. #endif // NET_2_0
  62. SqlConnection Connection {
  63. get { return connection; }
  64. }
  65. internal bool IsOpen {
  66. get { return isOpen; }
  67. }
  68. public
  69. #if NET_2_0
  70. override
  71. #endif // NET_2_0
  72. IsolationLevel IsolationLevel {
  73. get { return isolationLevel; }
  74. }
  75. IDbConnection IDbTransaction.Connection {
  76. get { return Connection; }
  77. }
  78. #endregion // Properties
  79. #region Methods
  80. public
  81. #if NET_2_0
  82. override
  83. #endif // NET_2_0
  84. void Commit ()
  85. {
  86. if (!isOpen)
  87. throw new InvalidOperationException ("The Transaction was not open.");
  88. connection.Tds.Execute ("COMMIT TRANSACTION");
  89. connection.Transaction = null;
  90. isOpen = false;
  91. }
  92. #if NET_2_0
  93. protected override
  94. #endif
  95. void Dispose (bool disposing)
  96. {
  97. if (!disposed) {
  98. if (disposing) {
  99. if (isOpen) // in case it is called in the dispose of the class, then the isOpen is already false
  100. Rollback ();
  101. }
  102. disposed = true;
  103. }
  104. }
  105. public
  106. #if NET_2_0
  107. override
  108. #endif // NET_2_0
  109. void Dispose ()
  110. {
  111. Dispose (true);
  112. GC.SuppressFinalize (this);
  113. }
  114. public
  115. #if NET_2_0
  116. override
  117. #endif // NET_2_0
  118. void Rollback ()
  119. {
  120. Rollback (String.Empty);
  121. }
  122. public void Rollback (string transactionName)
  123. {
  124. if (!isRolledBack) {
  125. if (!isOpen)
  126. throw new InvalidOperationException ("The Transaction was not open.");
  127. connection.Tds.Execute (String.Format ("ROLLBACK TRANSACTION {0}", transactionName));
  128. isRolledBack = true;
  129. isOpen = false;
  130. connection.Transaction = null;
  131. connection = null;
  132. }
  133. }
  134. public void Save (string savePointName)
  135. {
  136. if (!isOpen)
  137. throw new InvalidOperationException ("The Transaction was not open.");
  138. connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
  139. }
  140. #if NET_2_0
  141. protected override DbConnection DbConnection
  142. {
  143. get {return (DbConnection) Connection;}
  144. }
  145. #endif // NET_2_0
  146. #endregion // Methods
  147. }
  148. }