2 // Mono.Data.PostgreSqlClient.PgSqlTransaction.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
8 // (C) Ximian, Inc. 2002
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 // use #define DEBUG_SqlTransaction if you want to spew debug messages
33 // #define DEBUG_SqlTransaction
38 using System.Data.Common;
40 namespace Mono.Data.PostgreSqlClient
43 /// Represents a transaction to be performed on a SQL database.
45 // public sealed class PgSqlTransaction : MarshalByRefObject,
46 // IDbTransaction, IDisposable
47 public sealed class PgSqlTransaction : IDbTransaction
51 private bool doingTransaction = false;
52 private PgSqlConnection conn = null;
53 private IsolationLevel isolationLevel =
54 IsolationLevel.ReadCommitted;
55 // There are only two IsolationLevel's for PostgreSQL:
56 // ReadCommitted and Serializable,
57 // but ReadCommitted is the default
61 #region Public Methods
66 if(doingTransaction == false)
67 throw new InvalidOperationException(
68 "Begin transaction was not " +
70 "thus PostgreSQL can not " +
71 "Commit transaction.");
73 PgSqlCommand cmd = new PgSqlCommand("COMMIT", conn);
74 cmd.ExecuteNonQuery();
76 doingTransaction = false;
80 public void Rollback()
82 if(doingTransaction == false)
83 throw new InvalidOperationException(
84 "Begin transaction was not " +
86 "thus PostgreSQL can not " +
87 "Rollback transaction.");
89 PgSqlCommand cmd = new PgSqlCommand("ROLLBACK", conn);
90 cmd.ExecuteNonQuery();
92 doingTransaction = false;
95 // For PostgreSQL, Rollback(string) will not be implemented
96 // because PostgreSQL does not support Savepoints
98 public void Rollback(string transactionName) {
99 // throw new NotImplementedException ();
103 // For PostgreSQL, Save(string) will not be implemented
104 // because PostgreSQL does not support Savepoints
106 public void Save (string savePointName) {
107 // throw new NotImplementedException ();
110 #endregion // Public Methods
112 #region Internal Methods to Mono.Data.PostgreSqlClient.dll Assembly
114 internal void Begin()
116 if(doingTransaction == true)
117 throw new InvalidOperationException(
118 "Transaction has begun " +
119 "and PostgreSQL does not " +
120 "support nested transactions.");
122 PgSqlCommand cmd = new PgSqlCommand("BEGIN", conn);
123 cmd.ExecuteNonQuery();
125 doingTransaction = true;
128 internal void SetIsolationLevel(IsolationLevel isoLevel)
130 String sSql = "SET TRANSACTION ISOLATION LEVEL ";
134 case IsolationLevel.ReadCommitted:
135 sSql += "READ COMMITTED";
138 case IsolationLevel.Serializable:
139 sSql += "SERIALIZABLE";
143 // FIXME: generate exception here
144 // PostgreSQL only supports:
145 // ReadCommitted or Serializable
148 PgSqlCommand cmd = new PgSqlCommand(sSql, conn);
149 cmd.ExecuteNonQuery();
151 this.isolationLevel = isoLevel;
154 internal void SetConnection(PgSqlConnection connection)
156 this.conn = connection;
159 #endregion // Internal Methods to System.Data.dll Assembly
163 IDbConnection IDbTransaction.Connection {
169 public PgSqlConnection Connection {
175 public IsolationLevel IsolationLevel {
177 return isolationLevel;
181 internal bool DoingTransaction {
183 return doingTransaction;
187 #endregion Properties
191 // Destructors aka Finalize and Dispose
194 public void Dispose()
196 // FIXME: need to properly release resources
203 // [ClassInterface(ClassInterfaceType.AutoDual)]
204 ~PgSqlTransaction() {
205 // FIXME: need to properly release resources
209 #endregion // Destructors