2 // System.Data.SqlClient.SqlTransaction.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
8 // (C) Ximian, Inc. 2002
11 // use #define DEBUG_SqlTransaction if you want to spew debug messages
12 // #define DEBUG_SqlTransaction
17 using System.Data.Common;
19 namespace System.Data.SqlClient
22 /// Represents a transaction to be performed on a SQL database.
24 // public sealed class SqlTransaction : MarshalByRefObject,
25 // IDbTransaction, IDisposable
26 public sealed class SqlTransaction : IDbTransaction
30 private bool doingTransaction = false;
31 private SqlConnection conn = null;
32 private IsolationLevel isolationLevel =
33 IsolationLevel.ReadCommitted;
34 // There are only two IsolationLevel's for PostgreSQL:
35 // ReadCommitted and Serializable,
36 // but ReadCommitted is the default
40 #region Public Methods
45 if(doingTransaction == false)
46 throw new InvalidOperationException(
47 "Begin transaction was not " +
49 "thus PostgreSQL can not " +
50 "Commit transaction.");
52 SqlCommand cmd = new SqlCommand("COMMIT", conn);
53 cmd.ExecuteNonQuery();
55 doingTransaction = false;
59 public void Rollback()
61 if(doingTransaction == false)
62 throw new InvalidOperationException(
63 "Begin transaction was not " +
65 "thus PostgreSQL can not " +
66 "Rollback transaction.");
68 SqlCommand cmd = new SqlCommand("ROLLBACK", conn);
69 cmd.ExecuteNonQuery();
71 doingTransaction = false;
74 // For PostgreSQL, Rollback(string) will not be implemented
75 // because PostgreSQL does not support Savepoints
77 public void Rollback(string transactionName) {
78 // throw new NotImplementedException ();
82 // For PostgreSQL, Save(string) will not be implemented
83 // because PostgreSQL does not support Savepoints
85 public void Save (string savePointName) {
86 // throw new NotImplementedException ();
89 #endregion // Public Methods
91 #region Internal Methods to System.Data.dll Assembly
95 if(doingTransaction == true)
96 throw new InvalidOperationException(
97 "Transaction has begun " +
98 "and PostgreSQL does not " +
99 "support nested transactions.");
101 SqlCommand cmd = new SqlCommand("BEGIN", conn);
102 cmd.ExecuteNonQuery();
104 doingTransaction = true;
107 internal void SetIsolationLevel(IsolationLevel isoLevel)
109 String sSql = "SET TRANSACTION ISOLATION LEVEL ";
113 case IsolationLevel.ReadCommitted:
114 sSql += "READ COMMITTED";
117 case IsolationLevel.Serializable:
118 sSql += "SERIALIZABLE";
122 // FIXME: generate exception here
123 // PostgreSQL only supports:
124 // ReadCommitted or Serializable
127 SqlCommand cmd = new SqlCommand(sSql, conn);
128 cmd.ExecuteNonQuery();
130 this.isolationLevel = isoLevel;
133 internal void SetConnection(SqlConnection connection)
135 this.conn = connection;
138 #endregion // Internal Methods to System.Data.dll Assembly
142 IDbConnection IDbTransaction.Connection {
148 public SqlConnection Connection {
154 public IsolationLevel IsolationLevel {
156 return isolationLevel;
160 internal bool DoingTransaction {
162 return doingTransaction;
166 #endregion Properties
170 // Destructors aka Finalize and Dispose
173 public void Dispose()
175 // FIXME: need to properly release resources
182 // [ClassInterface(ClassInterfaceType.AutoDual)]
184 // FIXME: need to properly release resources
188 #endregion // Destructors