SqlTransaction.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. private void Dispose (bool disposing)
  93. {
  94. if (!disposed) {
  95. if (disposing) {
  96. if (isOpen) // in case it is called in the dispose of the class, then the isOpen is already false
  97. Rollback ();
  98. }
  99. disposed = true;
  100. }
  101. }
  102. public
  103. #if NET_2_0
  104. override
  105. #endif // NET_2_0
  106. void Dispose ()
  107. {
  108. Dispose (true);
  109. GC.SuppressFinalize (this);
  110. }
  111. public
  112. #if NET_2_0
  113. override
  114. #endif // NET_2_0
  115. void Rollback ()
  116. {
  117. Rollback (String.Empty);
  118. }
  119. public void Rollback (string transactionName)
  120. {
  121. if (!isRolledBack) {
  122. if (!isOpen)
  123. throw new InvalidOperationException ("The Transaction was not open.");
  124. connection.Tds.Execute (String.Format ("ROLLBACK TRANSACTION {0}", transactionName));
  125. isRolledBack = true;
  126. isOpen = false;
  127. }
  128. }
  129. public void Save (string savePointName)
  130. {
  131. if (!isOpen)
  132. throw new InvalidOperationException ("The Transaction was not open.");
  133. connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
  134. }
  135. #if NET_2_0
  136. protected override DbConnection DbConnection
  137. {
  138. get {return (DbConnection) Connection;}
  139. }
  140. #endif // NET_2_0
  141. #endregion // Methods
  142. }
  143. }