OdbcTransaction.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #if NET_2_0
  83. protected override
  84. #endif
  85. void Dispose(bool disposing) {
  86. if (!disposed) {
  87. if (disposing) {
  88. Rollback();
  89. }
  90. disposed = true;
  91. }
  92. }
  93. #if !NET_2_0
  94. public
  95. #endif
  96. void Dispose() {
  97. Dispose(true);
  98. GC.SuppressFinalize(this);
  99. }
  100. #endregion Implementation of IDisposable
  101. #region Implementation of IDbTransaction
  102. public
  103. #if NET_2_0
  104. override
  105. #endif //NET_2_0
  106. void Commit()
  107. {
  108. if (connection.transaction==this)
  109. {
  110. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  111. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  112. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  113. connection.transaction=null;
  114. }
  115. else
  116. throw new InvalidOperationException();
  117. }
  118. public
  119. #if NET_2_0
  120. override
  121. #endif //NET_2_0
  122. void Rollback()
  123. {
  124. if (connection.transaction==this)
  125. {
  126. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  127. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  128. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  129. connection.transaction=null;
  130. }
  131. else
  132. throw new InvalidOperationException();
  133. }
  134. #if ONLY_1_1
  135. IDbConnection IDbTransaction.Connection
  136. {
  137. get
  138. {
  139. return Connection;
  140. }
  141. }
  142. #endif // ONLY_1_1
  143. #if NET_2_0
  144. protected override DbConnection DbConnection
  145. {
  146. get {return Connection;}
  147. }
  148. #endif // NET_2_0
  149. public
  150. #if NET_2_0
  151. override
  152. #endif //NET_2_0
  153. IsolationLevel IsolationLevel
  154. {
  155. get
  156. {
  157. return isolationlevel;
  158. }
  159. }
  160. #endregion Implementation of IDbTransaction
  161. #region Public Instance Properties
  162. #if ONLY_1_1
  163. public OdbcConnection Connection
  164. {
  165. get
  166. {
  167. return connection;
  168. }
  169. }
  170. #endif // ONLY_1_1
  171. #endregion Public Instance Properties
  172. }
  173. }