1 // created on 17/11/2002 at 19:04
3 // Npgsql.NpgsqlTransaction.cs
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 using System
.Resources
;
36 /// Represents a transaction to be made in a PostgreSQL database. This class cannot be inherited.
38 public sealed class NpgsqlTransaction
: MarshalByRefObject
, IDbTransaction
40 private static readonly String CLASSNAME
= "NpgsqlTransaction";
41 private static ResourceManager resman
= new ResourceManager(typeof(NpgsqlTransaction
));
43 private NpgsqlConnection _conn
= null;
44 private IsolationLevel _isolation
= IsolationLevel
.ReadCommitted
;
45 private bool _disposing
= false;
47 internal NpgsqlTransaction(NpgsqlConnection conn
) : this(conn
, IsolationLevel
.ReadCommitted
)
50 internal NpgsqlTransaction(NpgsqlConnection conn
, IsolationLevel isolation
)
52 resman
= new System
.Resources
.ResourceManager(this.GetType());
54 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, CLASSNAME
);
55 if ((isolation
!= IsolationLevel
.ReadCommitted
) &&
56 (isolation
!= IsolationLevel
.Serializable
))
57 throw new ArgumentOutOfRangeException(resman
.GetString("Exception_UnsopportedIsolationLevel"), "isolation");
60 _isolation
= isolation
;
62 StringBuilder commandText
= new StringBuilder("SET TRANSACTION ISOLATION LEVEL ");
64 if (isolation
== IsolationLevel
.ReadCommitted
)
65 commandText
.Append("READ COMMITTED");
67 commandText
.Append("SERIALIZABLE");
69 commandText
.Append("; BEGIN");
71 NpgsqlCommand command
= new NpgsqlCommand(commandText
.ToString(), conn
.Connector
);
72 command
.ExecuteNonQuery();
73 _conn
.Connector
.Transaction
= this;
77 /// Gets the <see cref="Npgsql.NpgsqlConnection">NpgsqlConnection</see>
78 /// object associated with the transaction, or a null reference if the
79 /// transaction is no longer valid.
81 /// <value>The <see cref="Npgsql.NpgsqlConnection">NpgsqlConnection</see>
82 /// object associated with the transaction.</value>
83 public NpgsqlConnection Connection
92 IDbConnection IDbTransaction
.Connection
101 /// Specifies the <see cref="System.Data.IsolationLevel">IsolationLevel</see> for this transaction.
103 /// <value>The <see cref="System.Data.IsolationLevel">IsolationLevel</see> for this transaction.
104 /// The default is <b>ReadCommitted</b>.</value>
105 public IsolationLevel IsolationLevel
110 throw new InvalidOperationException(resman
.GetString("Exception_NoTransaction"));
118 /// Releases the unmanaged resources used by the
119 /// <see cref="Npgsql.NpgsqlTransaction">NpgsqlTransaction</see>
120 /// and optionally releases the managed resources.
122 public void Dispose()
127 private void Dispose(bool disposing
)
129 if(disposing
&& this._conn
!= null)
131 this._disposing
= true;
132 if (_conn
.Connector
.Transaction
!= null)
138 /// Commits the database transaction.
143 throw new InvalidOperationException(resman
.GetString("Exception_NoTransaction"));
146 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Commit");
148 NpgsqlCommand command
= new NpgsqlCommand("COMMIT", _conn
.Connector
);
149 command
.ExecuteNonQuery();
150 _conn
.Connector
.Transaction
= null;
155 /// Rolls back a transaction from a pending state.
157 public void Rollback()
160 throw new InvalidOperationException(resman
.GetString("Exception_NoTransaction"));
163 NpgsqlEventLog
.LogMethodEnter(LogLevel
.Debug
, CLASSNAME
, "Rollback");
165 NpgsqlCommand command
= new NpgsqlCommand("ROLLBACK", _conn
.Connector
);
166 command
.ExecuteNonQuery();
167 _conn
.Connector
.Transaction
= null;
172 /// Cancel the transaction without telling the backend about it. This is
173 /// used to make the transaction go away when closing a connection.
175 internal void Cancel()
178 _conn
.Connector
.Transaction
= null;
183 internal bool Disposing
{