SqlTransaction.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. {
  36. #if NET_2_0
  37. public sealed class SqlTransaction : DbTransaction, IDbTransaction, IDisposable
  38. #else
  39. public sealed class SqlTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  40. #endif // NET_2_0
  41. {
  42. #region Fields
  43. bool disposed = false;
  44. SqlConnection connection;
  45. IsolationLevel isolationLevel;
  46. bool isOpen;
  47. bool isRolledBack = false;
  48. #endregion
  49. #region Constructors
  50. internal SqlTransaction (SqlConnection connection, IsolationLevel isolevel)
  51. {
  52. this.connection = connection;
  53. this.isolationLevel = isolevel;
  54. isOpen = true;
  55. isRolledBack = false;
  56. }
  57. #endregion // Constructors
  58. #region Properties
  59. public
  60. #if NET_2_0
  61. new
  62. #endif // NET_2_0
  63. SqlConnection Connection {
  64. get { return connection; }
  65. }
  66. internal bool IsOpen {
  67. get { return isOpen; }
  68. }
  69. public
  70. #if NET_2_0
  71. override
  72. #endif // NET_2_0
  73. IsolationLevel IsolationLevel {
  74. get { return isolationLevel; }
  75. }
  76. IDbConnection IDbTransaction.Connection {
  77. get { return Connection; }
  78. }
  79. #endregion // Properties
  80. #region Methods
  81. public
  82. #if NET_2_0
  83. override
  84. #endif // NET_2_0
  85. void Commit ()
  86. {
  87. if (!isOpen)
  88. throw new InvalidOperationException ("The Transaction was not open.");
  89. connection.Tds.Execute ("COMMIT TRANSACTION");
  90. connection.Transaction = null;
  91. isOpen = false;
  92. }
  93. #if NET_2_0
  94. protected override
  95. #endif
  96. void Dispose (bool disposing)
  97. {
  98. if (!disposed) {
  99. if (disposing) {
  100. if (isOpen) // in case it is called in the dispose of the class, then the isOpen is already false
  101. Rollback ();
  102. }
  103. disposed = true;
  104. }
  105. }
  106. #if !NET_2_0
  107. public void Dispose ()
  108. {
  109. Dispose (true);
  110. GC.SuppressFinalize (this);
  111. }
  112. #endif
  113. public
  114. #if NET_2_0
  115. override
  116. #endif // NET_2_0
  117. void Rollback ()
  118. {
  119. Rollback (String.Empty);
  120. }
  121. public void Rollback (string transactionName)
  122. {
  123. if (!isRolledBack) {
  124. if (!isOpen)
  125. throw new InvalidOperationException ("The Transaction was not open.");
  126. connection.Tds.Execute (String.Format ("ROLLBACK TRANSACTION {0}", transactionName));
  127. isRolledBack = true;
  128. isOpen = false;
  129. connection.Transaction = null;
  130. connection = null;
  131. }
  132. }
  133. public void Save (string savePointName)
  134. {
  135. if (!isOpen)
  136. throw new InvalidOperationException ("The Transaction was not open.");
  137. connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
  138. }
  139. #if NET_2_0
  140. protected override DbConnection DbConnection {
  141. get { return Connection; }
  142. }
  143. #endif // NET_2_0
  144. #endregion // Methods
  145. }
  146. }