AbstractTransaction.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. namespace System.Data.Common
  31. {
  32. using java.sql;
  33. using System.Data;
  34. public abstract class AbstractTransaction : DbTransaction
  35. {
  36. protected String _transactionName;
  37. protected AbstractDBConnection _connection;
  38. protected IsolationLevel _isolationLevel;
  39. public AbstractTransaction(
  40. IsolationLevel isolationLevel,
  41. AbstractDBConnection connection,
  42. String transactionName)
  43. {
  44. connection.ValidateBeginTransaction();
  45. _transactionName = transactionName;
  46. _connection = connection;
  47. _isolationLevel = isolationLevel;
  48. try
  49. {
  50. _connection.JdbcConnection.setAutoCommit(false);
  51. _connection.JdbcConnection.setTransactionIsolation(
  52. convertIsolationLevel(isolationLevel));
  53. }
  54. catch (SQLException exp)
  55. {
  56. throw new System.InvalidOperationException(exp.Message);
  57. }
  58. }
  59. /**
  60. * @see System.Data.IDbTransaction#Connection
  61. */
  62. protected override DbConnection DbConnection
  63. {
  64. get
  65. {
  66. return _connection;
  67. }
  68. }
  69. /**
  70. * @see System.Data.IDbTransaction#IsolationLevel
  71. */
  72. public override IsolationLevel IsolationLevel
  73. {
  74. get
  75. {
  76. return _isolationLevel;
  77. }
  78. }
  79. /**
  80. * @see System.Data.IDbTransaction#Commit()
  81. */
  82. public override void Commit()
  83. {
  84. if (_connection == null)
  85. return;
  86. try
  87. {
  88. _connection.JdbcConnection.commit();
  89. _connection.JdbcConnection.setAutoCommit(true);
  90. _connection = null;
  91. }
  92. catch (SQLException exp)
  93. {
  94. throw new SystemException(exp.Message);
  95. }
  96. }
  97. /**
  98. * @see System.Data.IDbTransaction#Rollback()
  99. */
  100. public override void Rollback()
  101. {
  102. if (_connection == null)
  103. return;
  104. try
  105. {
  106. _connection.JdbcConnection.rollback();
  107. _connection.JdbcConnection.setAutoCommit(true);
  108. _connection = null;
  109. }
  110. catch (SQLException exp)
  111. {
  112. throw new SystemException(exp.Message);
  113. }
  114. }
  115. public override void Dispose()
  116. {
  117. Rollback();
  118. }
  119. internal AbstractTransaction ActiveTransaction {
  120. get {
  121. // recoursively return parent transaction when nesting will
  122. // be implemented
  123. return _connection != null ? this : null;
  124. }
  125. }
  126. private int convertIsolationLevel(IsolationLevel isolationLevel)
  127. {
  128. if (isolationLevel == IsolationLevel.Unspecified)
  129. return [email protected]__Finals.TRANSACTION_NONE;
  130. if (isolationLevel == IsolationLevel.ReadCommitted)
  131. return [email protected]__Finals.TRANSACTION_READ_COMMITTED;
  132. if (isolationLevel == IsolationLevel.ReadUncommitted)
  133. return [email protected]__Finals.TRANSACTION_READ_UNCOMMITTED;
  134. if (isolationLevel == IsolationLevel.RepeatableRead)
  135. return [email protected]__Finals.TRANSACTION_REPEATABLE_READ;
  136. if (isolationLevel == IsolationLevel.Serializable)
  137. return [email protected]__Finals.TRANSACTION_SERIALIZABLE;
  138. throw new NotSupportedException("The Isolation level '" + isolationLevel + "' is not supported");
  139. }
  140. }
  141. }