OdbcTransaction.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Data.Odbc.OdbcTransaction
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 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. #if NET_2_0
  34. using System.Data.Common;
  35. #endif // NET_2_0
  36. namespace System.Data.Odbc
  37. {
  38. #if NET_2_0
  39. public sealed class OdbcTransaction : DbTransaction
  40. #else
  41. public sealed class OdbcTransaction : MarshalByRefObject, IDbTransaction
  42. #endif // NET_2_0
  43. {
  44. private bool disposed = false;
  45. private OdbcConnection connection;
  46. private IsolationLevel isolationlevel;
  47. internal OdbcTransaction(OdbcConnection conn, IsolationLevel isolationlevel)
  48. {
  49. // Set Auto-commit (102) to false
  50. OdbcReturn ret=libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.AutoCommit, IntPtr.Zero, 0);
  51. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  52. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  53. // Handle isolation level
  54. int lev=0;
  55. switch (isolationlevel)
  56. {
  57. case IsolationLevel.ReadUncommitted:
  58. lev=1;
  59. break;
  60. case IsolationLevel.ReadCommitted:
  61. lev=2;
  62. break;
  63. case IsolationLevel.RepeatableRead:
  64. lev=3;
  65. break;
  66. case IsolationLevel.Serializable:
  67. lev=4;
  68. break;
  69. case IsolationLevel.Unspecified:
  70. lev=0;
  71. break;
  72. default:
  73. throw new NotSupportedException();
  74. }
  75. libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.TransactionIsolation, (IntPtr) lev, 0);
  76. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  77. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  78. this.isolationlevel=isolationlevel;
  79. connection=conn;
  80. }
  81. #region Implementation of IDisposable
  82. private void Dispose(bool disposing) {
  83. if (!disposed) {
  84. if (disposing) {
  85. Rollback();
  86. }
  87. disposed = true;
  88. }
  89. }
  90. #if ONLY_1_1
  91. void IDisposable.Dispose() {
  92. Dispose(true);
  93. GC.SuppressFinalize(this);
  94. }
  95. #else
  96. public override void Dispose ()
  97. {
  98. Dispose (true);
  99. GC.SuppressFinalize (this);
  100. }
  101. #endif // ONLY_1_1
  102. #endregion Implementation of IDisposable
  103. #region Implementation of IDbTransaction
  104. public
  105. #if NET_2_0
  106. override
  107. #endif //NET_2_0
  108. void Commit()
  109. {
  110. if (connection.transaction==this)
  111. {
  112. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  113. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  114. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  115. connection.transaction=null;
  116. }
  117. else
  118. throw new InvalidOperationException();
  119. }
  120. public
  121. #if NET_2_0
  122. override
  123. #endif //NET_2_0
  124. void Rollback()
  125. {
  126. if (connection.transaction==this)
  127. {
  128. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  129. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  130. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  131. connection.transaction=null;
  132. }
  133. else
  134. throw new InvalidOperationException();
  135. }
  136. #if ONLY_1_1
  137. IDbConnection IDbTransaction.Connection
  138. {
  139. get
  140. {
  141. return Connection;
  142. }
  143. }
  144. #endif // ONLY_1_1
  145. #if NET_2_0
  146. protected override DbConnection DbConnection
  147. {
  148. get {return Connection;}
  149. }
  150. #endif // NET_2_0
  151. public
  152. #if NET_2_0
  153. override
  154. #endif //NET_2_0
  155. IsolationLevel IsolationLevel
  156. {
  157. get
  158. {
  159. return isolationlevel;
  160. }
  161. }
  162. #endregion Implementation of IDbTransaction
  163. #region Public Instance Properties
  164. #if ONLY_1_1
  165. public OdbcConnection Connection
  166. {
  167. get
  168. {
  169. return connection;
  170. }
  171. }
  172. #endif // ONLY_1_1
  173. #endregion Public Instance Properties
  174. }
  175. }