SqlTransaction.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. public sealed class SqlTransaction : DbTransaction, IDbTransaction, IDisposable
  37. {
  38. #region Fields
  39. bool disposed;
  40. SqlConnection connection;
  41. IsolationLevel isolationLevel;
  42. bool isOpen;
  43. #endregion
  44. #region Constructors
  45. internal SqlTransaction (SqlConnection connection, IsolationLevel isolevel)
  46. {
  47. this.connection = connection;
  48. this.isolationLevel = isolevel;
  49. isOpen = true;
  50. }
  51. #endregion // Constructors
  52. #region Properties
  53. public
  54. new
  55. SqlConnection Connection {
  56. get { return connection; }
  57. }
  58. internal bool IsOpen {
  59. get { return isOpen; }
  60. }
  61. public
  62. override
  63. IsolationLevel IsolationLevel {
  64. get {
  65. if (!isOpen)
  66. throw ExceptionHelper.TransactionNotUsable (GetType ());
  67. return isolationLevel;
  68. }
  69. }
  70. protected override DbConnection DbConnection {
  71. get { return Connection; }
  72. }
  73. #endregion // Properties
  74. #region Methods
  75. public
  76. override
  77. void Commit ()
  78. {
  79. if (!isOpen)
  80. throw ExceptionHelper.TransactionNotUsable (GetType ());
  81. connection.Tds.Execute ("COMMIT TRANSACTION");
  82. connection.Transaction = null;
  83. connection = null;
  84. isOpen = false;
  85. }
  86. protected override
  87. void Dispose (bool disposing)
  88. {
  89. if (!disposed) {
  90. if (disposing) {
  91. if (isOpen) // in case it is called in the dispose of the class, then the isOpen is already false
  92. Rollback ();
  93. }
  94. disposed = true;
  95. }
  96. }
  97. public
  98. override
  99. void Rollback ()
  100. {
  101. Rollback (String.Empty);
  102. }
  103. public void Rollback (string transactionName)
  104. {
  105. if (disposed)
  106. return;
  107. if (!isOpen)
  108. throw ExceptionHelper.TransactionNotUsable (GetType ());
  109. if (connection.Tds.IsConnected)
  110. connection.Tds.Execute (String.Format ("IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION {0}",
  111. transactionName));
  112. isOpen = false;
  113. connection.Transaction = null;
  114. connection = null;
  115. }
  116. public void Save (string savePointName)
  117. {
  118. if (!isOpen)
  119. throw ExceptionHelper.TransactionNotUsable (GetType ());
  120. connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
  121. }
  122. #endregion // Methods
  123. }
  124. }