OleDbTransaction.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // System.Data.OleDb.OleDbTransaction
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Rodrigo Moya, 2002
  9. // Copyright (C) Tim Coleman, 2002
  10. // Copyright (C) 2004-2005 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.Data.Common;
  32. namespace System.Data.OleDb
  33. {
  34. public sealed class OleDbTransaction : DbTransaction, IDbTransaction
  35. {
  36. #region Fields
  37. bool disposed;
  38. OleDbConnection connection;
  39. IntPtr gdaTransaction;
  40. int depth;
  41. bool isOpen;
  42. #endregion // Fields
  43. #region Constructors
  44. internal OleDbTransaction (OleDbConnection connection, int depth)
  45. : this (connection, depth, IsolationLevel.ReadCommitted)
  46. {
  47. }
  48. internal OleDbTransaction (OleDbConnection connection)
  49. : this (connection, 1)
  50. {
  51. }
  52. internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel)
  53. {
  54. this.connection = connection;
  55. gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
  56. switch (isolevel) {
  57. case IsolationLevel.ReadCommitted :
  58. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  59. GdaTransactionIsolation.ReadCommitted);
  60. break;
  61. case IsolationLevel.ReadUncommitted :
  62. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  63. GdaTransactionIsolation.ReadUncommitted);
  64. break;
  65. case IsolationLevel.RepeatableRead :
  66. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  67. GdaTransactionIsolation.RepeatableRead);
  68. break;
  69. case IsolationLevel.Serializable :
  70. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  71. GdaTransactionIsolation.Serializable);
  72. break;
  73. }
  74. libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
  75. isOpen = true;
  76. }
  77. internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel)
  78. : this (connection, 1, isolevel)
  79. {
  80. }
  81. #endregion // Constructors
  82. #region Properties
  83. public new OleDbConnection Connection {
  84. get {
  85. return connection;
  86. }
  87. }
  88. protected override DbConnection DbConnection {
  89. get { return connection; }
  90. }
  91. public
  92. override
  93. IsolationLevel IsolationLevel {
  94. get {
  95. if (!isOpen)
  96. throw ExceptionHelper.TransactionNotUsable (GetType ());
  97. switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
  98. case GdaTransactionIsolation.ReadCommitted :
  99. return IsolationLevel.ReadCommitted;
  100. case GdaTransactionIsolation.ReadUncommitted :
  101. return IsolationLevel.ReadUncommitted;
  102. case GdaTransactionIsolation.RepeatableRead :
  103. return IsolationLevel.RepeatableRead;
  104. case GdaTransactionIsolation.Serializable :
  105. return IsolationLevel.Serializable;
  106. }
  107. return IsolationLevel.Unspecified;
  108. }
  109. }
  110. #endregion // Properties
  111. #region Methods
  112. public OleDbTransaction Begin ()
  113. {
  114. if (!isOpen)
  115. throw ExceptionHelper.TransactionNotUsable (GetType ());
  116. return new OleDbTransaction (connection, depth + 1);
  117. }
  118. public OleDbTransaction Begin (IsolationLevel isolevel)
  119. {
  120. if (!isOpen)
  121. throw ExceptionHelper.TransactionNotUsable (GetType ());
  122. return new OleDbTransaction (connection, depth + 1, isolevel);
  123. }
  124. public
  125. override
  126. void Commit ()
  127. {
  128. if (!isOpen)
  129. throw ExceptionHelper.TransactionNotUsable (GetType ());
  130. if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
  131. gdaTransaction))
  132. throw new InvalidOperationException ();
  133. connection = null;
  134. isOpen = false;
  135. }
  136. protected override
  137. void Dispose (bool disposing)
  138. {
  139. if (!disposed) {
  140. if (disposing && isOpen)
  141. Rollback ();
  142. disposed = true;
  143. }
  144. base.Dispose (disposing);
  145. }
  146. public
  147. override
  148. void Rollback ()
  149. {
  150. if (!isOpen)
  151. throw ExceptionHelper.TransactionNotUsable (GetType ());
  152. if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
  153. gdaTransaction))
  154. throw new InvalidOperationException ();
  155. connection = null;
  156. isOpen = false;
  157. }
  158. #endregion // Methods
  159. }
  160. }