AbstractTransaction.cs 4.9 KB

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