OdbcTransaction.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. namespace System.Data.Odbc
  34. {
  35. public sealed class OdbcTransaction : MarshalByRefObject, IDbTransaction
  36. {
  37. private bool disposed = false;
  38. private OdbcConnection connection;
  39. private IsolationLevel isolationlevel;
  40. internal OdbcTransaction(OdbcConnection conn, IsolationLevel isolationlevel)
  41. {
  42. // Set Auto-commit (102) to false
  43. OdbcReturn ret=libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.AutoCommit, IntPtr.Zero, 0);
  44. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  45. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  46. // Handle isolation level
  47. int lev=0;
  48. switch (isolationlevel)
  49. {
  50. case IsolationLevel.ReadUncommitted:
  51. lev=1;
  52. break;
  53. case IsolationLevel.ReadCommitted:
  54. lev=2;
  55. break;
  56. case IsolationLevel.RepeatableRead:
  57. lev=3;
  58. break;
  59. case IsolationLevel.Serializable:
  60. lev=4;
  61. break;
  62. case IsolationLevel.Unspecified:
  63. lev=0;
  64. break;
  65. default:
  66. throw new NotSupportedException();
  67. }
  68. libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.TransactionIsolation, (IntPtr) lev, 0);
  69. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  70. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  71. this.isolationlevel=isolationlevel;
  72. connection=conn;
  73. }
  74. #region Implementation of IDisposable
  75. private void Dispose(bool disposing) {
  76. if (!disposed) {
  77. if (disposing) {
  78. Rollback();
  79. }
  80. disposed = true;
  81. }
  82. }
  83. void IDisposable.Dispose() {
  84. Dispose(true);
  85. GC.SuppressFinalize(this);
  86. }
  87. #endregion Implementation of IDisposable
  88. #region Implementation of IDbTransaction
  89. public void Commit()
  90. {
  91. if (connection.transaction==this)
  92. {
  93. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  94. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  95. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  96. connection.transaction=null;
  97. }
  98. else
  99. throw new InvalidOperationException();
  100. }
  101. public void Rollback()
  102. {
  103. if (connection.transaction==this)
  104. {
  105. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  106. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  107. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  108. connection.transaction=null;
  109. }
  110. else
  111. throw new InvalidOperationException();
  112. }
  113. IDbConnection IDbTransaction.Connection
  114. {
  115. get
  116. {
  117. return Connection;
  118. }
  119. }
  120. public IsolationLevel IsolationLevel
  121. {
  122. get
  123. {
  124. return isolationlevel;
  125. }
  126. }
  127. #endregion Implementation of IDbTransaction
  128. #region Public Instance Properties
  129. public OdbcConnection Connection
  130. {
  131. get
  132. {
  133. return connection;
  134. }
  135. }
  136. #endregion Public Instance Properties
  137. }
  138. }