OdbcTransaction.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //------------------------------------------------------------------------------
  2. // <copyright file="OdbcTransaction.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. using System;
  9. using System.Data;
  10. using System.Data.Common;
  11. using System.Threading;
  12. namespace System.Data.Odbc
  13. {
  14. public sealed class OdbcTransaction : DbTransaction {
  15. private OdbcConnection _connection;
  16. private IsolationLevel _isolevel = IsolationLevel.Unspecified;
  17. private OdbcConnectionHandle _handle;
  18. internal OdbcTransaction(OdbcConnection connection, IsolationLevel isolevel, OdbcConnectionHandle handle) {
  19. OdbcConnection.VerifyExecutePermission();
  20. _connection = connection;
  21. _isolevel = isolevel;
  22. _handle = handle;
  23. }
  24. new public OdbcConnection Connection { // MDAC 66655
  25. get {
  26. return _connection;
  27. }
  28. }
  29. override protected DbConnection DbConnection { // MDAC 66655
  30. get {
  31. return Connection;
  32. }
  33. }
  34. override public IsolationLevel IsolationLevel {
  35. get {
  36. OdbcConnection connection = _connection;
  37. if (null == connection ) {
  38. throw ADP.TransactionZombied(this);
  39. }
  40. //We need to query for the case where the user didn't set the isolevel
  41. //BeginTransaction(), but we should also query to see if the driver
  42. //"rolled" the level to a higher supported one...
  43. if(IsolationLevel.Unspecified == _isolevel) {
  44. //Get the isolation level
  45. int sql_iso= connection .GetConnectAttr(ODBC32.SQL_ATTR.TXN_ISOLATION, ODBC32.HANDLER.THROW);
  46. switch((ODBC32.SQL_TRANSACTION)sql_iso) {
  47. case ODBC32.SQL_TRANSACTION.READ_UNCOMMITTED:
  48. _isolevel = IsolationLevel.ReadUncommitted;
  49. break;
  50. case ODBC32.SQL_TRANSACTION.READ_COMMITTED:
  51. _isolevel = IsolationLevel.ReadCommitted;
  52. break;
  53. case ODBC32.SQL_TRANSACTION.REPEATABLE_READ:
  54. _isolevel = IsolationLevel.RepeatableRead;
  55. break;
  56. case ODBC32.SQL_TRANSACTION.SERIALIZABLE:
  57. _isolevel = IsolationLevel.Serializable;
  58. break;
  59. case ODBC32.SQL_TRANSACTION.SNAPSHOT:
  60. _isolevel = IsolationLevel.Snapshot;
  61. break;
  62. default:
  63. throw ODBC.NoMappingForSqlTransactionLevel(sql_iso);
  64. };
  65. }
  66. return _isolevel;
  67. }
  68. }
  69. override public void Commit() {
  70. OdbcConnection.ExecutePermission.Demand(); // MDAC 81476
  71. OdbcConnection connection = _connection;
  72. if (null == connection) {
  73. throw ADP.TransactionZombied(this);
  74. }
  75. connection.CheckState(ADP.CommitTransaction); // MDAC 68289
  76. //Note: SQLEndTran success if not actually in a transaction, so we have to throw
  77. //since the IDbTransaciton spec indicates this is an error for the managed packages
  78. if(null == _handle) {
  79. throw ODBC.NotInTransaction();
  80. }
  81. ODBC32.RetCode retcode = _handle.CompleteTransaction(ODBC32.SQL_COMMIT);
  82. if (retcode == ODBC32.RetCode.ERROR) {
  83. //If an error has occurred, we will throw an exception in HandleError,
  84. //and leave the transaction active for the user to retry
  85. connection.HandleError(_handle, retcode);
  86. }
  87. //Transaction is complete...
  88. connection.LocalTransaction = null;
  89. _connection = null;
  90. _handle = null;
  91. }
  92. protected override void Dispose(bool disposing) {
  93. if (disposing) {
  94. OdbcConnectionHandle handle = _handle;
  95. _handle = null;
  96. if (null != handle){
  97. try{
  98. ODBC32.RetCode retcode = handle.CompleteTransaction(ODBC32.SQL_ROLLBACK);
  99. if (retcode == ODBC32.RetCode.ERROR) {
  100. //don't throw an exception here, but trace it so it can be logged
  101. if (_connection != null) {
  102. Exception e = _connection.HandleErrorNoThrow(handle, retcode);
  103. ADP.TraceExceptionWithoutRethrow(e);
  104. }
  105. }
  106. }
  107. catch (Exception e){
  108. //
  109. if (!ADP.IsCatchableExceptionType(e)) {
  110. throw;
  111. }
  112. }
  113. }
  114. if (_connection != null) {
  115. if (_connection.IsOpen) {
  116. _connection.LocalTransaction = null;
  117. }
  118. }
  119. _connection = null;
  120. _isolevel = IsolationLevel.Unspecified;
  121. }
  122. base.Dispose(disposing);
  123. }
  124. override public void Rollback() {
  125. OdbcConnection connection = _connection;
  126. if (null == connection) {
  127. throw ADP.TransactionZombied(this);
  128. }
  129. connection.CheckState(ADP.RollbackTransaction); // MDAC 68289
  130. //Note: SQLEndTran success if not actually in a transaction, so we have to throw
  131. //since the IDbTransaciton spec indicates this is an error for the managed packages
  132. if(null == _handle) {
  133. throw ODBC.NotInTransaction();
  134. }
  135. ODBC32.RetCode retcode = _handle.CompleteTransaction(ODBC32.SQL_ROLLBACK);
  136. if (retcode == ODBC32.RetCode.ERROR) {
  137. //If an error has occurred, we will throw an exception in HandleError,
  138. //and leave the transaction active for the user to retry
  139. connection.HandleError(_handle, retcode);
  140. }
  141. connection.LocalTransaction = null;
  142. _connection = null;
  143. _handle = null;
  144. }
  145. }
  146. }