OdbcTransaction.cs 4.7 KB

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