OleDbTransaction.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #if NET_2_0
  35. public sealed class OleDbTransaction : DbTransaction, IDbTransaction
  36. #else
  37. public sealed class OleDbTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  38. #endif
  39. {
  40. #region Fields
  41. bool disposed;
  42. OleDbConnection connection;
  43. IntPtr gdaTransaction;
  44. int depth;
  45. bool isOpen;
  46. #endregion // Fields
  47. #region Constructors
  48. internal OleDbTransaction (OleDbConnection connection, int depth)
  49. : this (connection, depth, IsolationLevel.ReadCommitted)
  50. {
  51. }
  52. internal OleDbTransaction (OleDbConnection connection)
  53. : this (connection, 1)
  54. {
  55. }
  56. internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel)
  57. {
  58. this.connection = connection;
  59. gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
  60. switch (isolevel) {
  61. case IsolationLevel.ReadCommitted :
  62. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  63. GdaTransactionIsolation.ReadCommitted);
  64. break;
  65. case IsolationLevel.ReadUncommitted :
  66. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  67. GdaTransactionIsolation.ReadUncommitted);
  68. break;
  69. case IsolationLevel.RepeatableRead :
  70. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  71. GdaTransactionIsolation.RepeatableRead);
  72. break;
  73. case IsolationLevel.Serializable :
  74. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  75. GdaTransactionIsolation.Serializable);
  76. break;
  77. }
  78. libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
  79. isOpen = true;
  80. }
  81. internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel)
  82. : this (connection, 1, isolevel)
  83. {
  84. }
  85. #endregion // Constructors
  86. #region Properties
  87. public new OleDbConnection Connection {
  88. get {
  89. return connection;
  90. }
  91. }
  92. #if NET_2_0
  93. protected override DbConnection DbConnection {
  94. get { return connection; }
  95. }
  96. #else
  97. IDbConnection IDbTransaction.Connection {
  98. get {
  99. return connection;
  100. }
  101. }
  102. #endif
  103. public
  104. #if NET_2_0
  105. override
  106. #endif
  107. IsolationLevel IsolationLevel {
  108. get {
  109. if (!isOpen)
  110. throw ExceptionHelper.TransactionNotUsable (GetType ());
  111. switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
  112. case GdaTransactionIsolation.ReadCommitted :
  113. return IsolationLevel.ReadCommitted;
  114. case GdaTransactionIsolation.ReadUncommitted :
  115. return IsolationLevel.ReadUncommitted;
  116. case GdaTransactionIsolation.RepeatableRead :
  117. return IsolationLevel.RepeatableRead;
  118. case GdaTransactionIsolation.Serializable :
  119. return IsolationLevel.Serializable;
  120. }
  121. return IsolationLevel.Unspecified;
  122. }
  123. }
  124. #endregion // Properties
  125. #region Methods
  126. public OleDbTransaction Begin ()
  127. {
  128. if (!isOpen)
  129. throw ExceptionHelper.TransactionNotUsable (GetType ());
  130. return new OleDbTransaction (connection, depth + 1);
  131. }
  132. public OleDbTransaction Begin (IsolationLevel isolevel)
  133. {
  134. if (!isOpen)
  135. throw ExceptionHelper.TransactionNotUsable (GetType ());
  136. return new OleDbTransaction (connection, depth + 1, isolevel);
  137. }
  138. public
  139. #if NET_2_0
  140. override
  141. #endif
  142. void Commit ()
  143. {
  144. if (!isOpen)
  145. throw ExceptionHelper.TransactionNotUsable (GetType ());
  146. if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
  147. gdaTransaction))
  148. throw new InvalidOperationException ();
  149. connection = null;
  150. isOpen = false;
  151. }
  152. #if ONLY_1_1
  153. ~OleDbTransaction ()
  154. {
  155. libgda.FreeObject (gdaTransaction);
  156. gdaTransaction = IntPtr.Zero;
  157. }
  158. #endif
  159. #if NET_2_0
  160. protected override
  161. #endif
  162. void Dispose (bool disposing)
  163. {
  164. if (!disposed) {
  165. if (disposing && isOpen)
  166. Rollback ();
  167. disposed = true;
  168. }
  169. #if NET_2_0
  170. base.Dispose (disposing);
  171. #endif
  172. }
  173. #if !NET_2_0
  174. void IDisposable.Dispose ()
  175. {
  176. Dispose (true);
  177. }
  178. #endif
  179. public
  180. #if NET_2_0
  181. override
  182. #endif
  183. void Rollback ()
  184. {
  185. if (!isOpen)
  186. throw ExceptionHelper.TransactionNotUsable (GetType ());
  187. if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
  188. gdaTransaction))
  189. throw new InvalidOperationException ();
  190. connection = null;
  191. isOpen = false;
  192. }
  193. #endregion // Methods
  194. }
  195. }