2
0

SqlTransaction.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public sealed class SqlTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  36. {
  37. #region Fields
  38. bool disposed = false;
  39. SqlConnection connection;
  40. IsolationLevel isolationLevel;
  41. bool isOpen;
  42. bool isRolledBack = false;
  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. isRolledBack = false;
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. public SqlConnection Connection {
  55. get { return connection; }
  56. }
  57. internal bool IsOpen {
  58. get { return isOpen; }
  59. }
  60. public IsolationLevel IsolationLevel {
  61. get { return isolationLevel; }
  62. }
  63. IDbConnection IDbTransaction.Connection {
  64. get { return Connection; }
  65. }
  66. #endregion // Properties
  67. #region Methods
  68. public void Commit ()
  69. {
  70. if (!isOpen)
  71. throw new InvalidOperationException ("The Transaction was not open.");
  72. connection.Tds.Execute ("COMMIT TRANSACTION");
  73. connection.Transaction = null;
  74. isOpen = false;
  75. }
  76. private void Dispose (bool disposing)
  77. {
  78. if (!disposed) {
  79. if (disposing) {
  80. Rollback ();
  81. }
  82. disposed = true;
  83. }
  84. }
  85. public void Dispose ()
  86. {
  87. Dispose (true);
  88. GC.SuppressFinalize (this);
  89. }
  90. public void Rollback ()
  91. {
  92. Rollback (String.Empty);
  93. }
  94. public void Rollback (string transactionName)
  95. {
  96. if (!isRolledBack) {
  97. if (!isOpen)
  98. throw new InvalidOperationException ("The Transaction was not open.");
  99. connection.Tds.Execute (String.Format ("ROLLBACK TRANSACTION {0}", transactionName));
  100. isRolledBack = true;
  101. isOpen = false;
  102. }
  103. }
  104. public void Save (string savePointName)
  105. {
  106. if (!isOpen)
  107. throw new InvalidOperationException ("The Transaction was not open.");
  108. connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
  109. }
  110. #endregion // Methods
  111. }
  112. }