AbstractTransaction.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Data.ProviderBase.AbstractTransaction
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Data.Common;
  31. namespace System.Data.ProviderBase
  32. {
  33. using java.sql;
  34. using System.Data;
  35. public abstract class AbstractTransaction : DbTransaction
  36. {
  37. protected String _transactionName;
  38. protected AbstractDBConnection _connection;
  39. protected IsolationLevel _isolationLevel;
  40. public AbstractTransaction(
  41. IsolationLevel isolationLevel,
  42. AbstractDBConnection connection,
  43. String transactionName)
  44. {
  45. connection.ValidateBeginTransaction();
  46. _transactionName = transactionName;
  47. _connection = connection;
  48. _isolationLevel = isolationLevel;
  49. try
  50. {
  51. _connection.JdbcConnection.setAutoCommit(false);
  52. _connection.JdbcConnection.setTransactionIsolation(
  53. convertIsolationLevel(isolationLevel));
  54. }
  55. catch (SQLException exp)
  56. {
  57. throw new System.InvalidOperationException(exp.Message, exp);
  58. }
  59. }
  60. /**
  61. * @see System.Data.IDbTransaction#Connection
  62. */
  63. protected override DbConnection DbConnection
  64. {
  65. get
  66. {
  67. return _connection;
  68. }
  69. }
  70. /**
  71. * @see System.Data.IDbTransaction#IsolationLevel
  72. */
  73. public override IsolationLevel IsolationLevel
  74. {
  75. get
  76. {
  77. return _isolationLevel;
  78. }
  79. }
  80. /**
  81. * @see System.Data.IDbTransaction#Commit()
  82. */
  83. public override void Commit()
  84. {
  85. if (_connection == null)
  86. return;
  87. try
  88. {
  89. _connection.JdbcConnection.commit();
  90. _connection.JdbcConnection.setAutoCommit(true);
  91. _connection = null;
  92. }
  93. catch (SQLException exp)
  94. {
  95. throw new SystemException(exp.Message, exp);
  96. }
  97. }
  98. /**
  99. * @see System.Data.IDbTransaction#Rollback()
  100. */
  101. public override void Rollback()
  102. {
  103. if (_connection == null)
  104. return;
  105. try
  106. {
  107. _connection.JdbcConnection.rollback();
  108. _connection.JdbcConnection.setAutoCommit(true);
  109. _connection = null;
  110. }
  111. catch (SQLException exp)
  112. {
  113. throw new SystemException(exp.Message, exp);
  114. }
  115. }
  116. internal AbstractTransaction ActiveTransaction {
  117. get {
  118. // recoursively return parent transaction when nesting will
  119. // be implemented
  120. return _connection != null ? this : null;
  121. }
  122. }
  123. private int convertIsolationLevel(IsolationLevel isolationLevel)
  124. {
  125. if (isolationLevel == IsolationLevel.Unspecified)
  126. return [email protected]__Finals.TRANSACTION_NONE;
  127. if (isolationLevel == IsolationLevel.ReadCommitted)
  128. return [email protected]__Finals.TRANSACTION_READ_COMMITTED;
  129. if (isolationLevel == IsolationLevel.ReadUncommitted)
  130. return [email protected]__Finals.TRANSACTION_READ_UNCOMMITTED;
  131. if (isolationLevel == IsolationLevel.RepeatableRead)
  132. return [email protected]__Finals.TRANSACTION_REPEATABLE_READ;
  133. if (isolationLevel == IsolationLevel.Serializable)
  134. return [email protected]__Finals.TRANSACTION_SERIALIZABLE;
  135. throw new NotSupportedException("The Isolation level '" + isolationLevel + "' is not supported");
  136. }
  137. }
  138. }