SqlTransaction.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // System.Data.SqlClient.SqlTransaction.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. //
  8. // (C) Ximian, Inc. 2002
  9. //
  10. // use #define DEBUG_SqlTransaction if you want to spew debug messages
  11. // #define DEBUG_SqlTransaction
  12. using System;
  13. using System.Data;
  14. using System.Data.Common;
  15. namespace System.Data.SqlClient
  16. {
  17. /// <summary>
  18. /// Represents a transaction to be performed on a SQL database.
  19. /// </summary>
  20. // public sealed class SqlTransaction : MarshalByRefObject,
  21. // IDbTransaction, IDisposable
  22. public sealed class SqlTransaction : IDbTransaction
  23. {
  24. #region Fields
  25. private bool doingTransaction = false;
  26. private SqlConnection conn = null;
  27. private IsolationLevel isolationLevel =
  28. IsolationLevel.ReadCommitted;
  29. // There are only two IsolationLevel's for PostgreSQL:
  30. // ReadCommitted and Serializable,
  31. // but ReadCommitted is the default
  32. #endregion
  33. #region Public Methods
  34. [MonoTODO]
  35. public void Commit ()
  36. {
  37. if(doingTransaction == false)
  38. throw new InvalidOperationException(
  39. "Begin transaction was not " +
  40. "done earlier " +
  41. "thus PostgreSQL can not " +
  42. "Commit transaction.");
  43. SqlCommand cmd = new SqlCommand("COMMIT", conn);
  44. cmd.ExecuteNonQuery();
  45. doingTransaction = false;
  46. }
  47. [MonoTODO]
  48. public void Rollback()
  49. {
  50. if(doingTransaction == false)
  51. throw new InvalidOperationException(
  52. "Begin transaction was not " +
  53. "done earlier " +
  54. "thus PostgreSQL can not " +
  55. "Rollback transaction.");
  56. SqlCommand cmd = new SqlCommand("ROLLBACK", conn);
  57. cmd.ExecuteNonQuery();
  58. doingTransaction = false;
  59. }
  60. // For PostgreSQL, Rollback(string) will not be implemented
  61. // because PostgreSQL does not support Savepoints
  62. [Obsolete]
  63. public void Rollback(string transactionName) {
  64. // throw new NotImplementedException ();
  65. Rollback();
  66. }
  67. // For PostgreSQL, Save(string) will not be implemented
  68. // because PostgreSQL does not support Savepoints
  69. [Obsolete]
  70. public void Save (string savePointName) {
  71. // throw new NotImplementedException ();
  72. }
  73. #endregion // Public Methods
  74. #region Internal Methods to System.Data.dll Assembly
  75. internal void Begin()
  76. {
  77. if(doingTransaction == true)
  78. throw new InvalidOperationException(
  79. "Transaction has begun " +
  80. "and PostgreSQL does not " +
  81. "support nested transactions.");
  82. SqlCommand cmd = new SqlCommand("BEGIN", conn);
  83. cmd.ExecuteNonQuery();
  84. doingTransaction = true;
  85. }
  86. internal void SetIsolationLevel(IsolationLevel isoLevel)
  87. {
  88. String sSql = "SET TRANSACTION ISOLATION LEVEL ";
  89. switch (isoLevel)
  90. {
  91. case IsolationLevel.ReadCommitted:
  92. sSql += "READ COMMITTED";
  93. break;
  94. case IsolationLevel.Serializable:
  95. sSql += "SERIALIZABLE";
  96. break;
  97. default:
  98. // FIXME: generate exception here
  99. // PostgreSQL only supports:
  100. // ReadCommitted or Serializable
  101. break;
  102. }
  103. SqlCommand cmd = new SqlCommand(sSql, conn);
  104. cmd.ExecuteNonQuery();
  105. this.isolationLevel = isoLevel;
  106. }
  107. internal void SetConnection(SqlConnection connection)
  108. {
  109. this.conn = connection;
  110. }
  111. #endregion // Internal Methods to System.Data.dll Assembly
  112. #region Properties
  113. IDbConnection IDbTransaction.Connection {
  114. get {
  115. return Connection;
  116. }
  117. }
  118. public SqlConnection Connection {
  119. get {
  120. return conn;
  121. }
  122. }
  123. public IsolationLevel IsolationLevel {
  124. get {
  125. return isolationLevel;
  126. }
  127. }
  128. internal bool DoingTransaction {
  129. get {
  130. return doingTransaction;
  131. }
  132. }
  133. #endregion Properties
  134. #region Destructors
  135. // Destructors aka Finalize and Dispose
  136. [MonoTODO]
  137. public void Dispose()
  138. {
  139. // FIXME: need to properly release resources
  140. // Dispose(true);
  141. }
  142. // Destructor
  143. [MonoTODO]
  144. // [Serializable]
  145. // [ClassInterface(ClassInterfaceType.AutoDual)]
  146. ~SqlTransaction() {
  147. // FIXME: need to properly release resources
  148. // Dispose(false);
  149. }
  150. #endregion // Destructors
  151. }
  152. }