2 using System.Collections;
3 using System.Collections.Specialized;
5 using System.Runtime.InteropServices;
12 public sealed class DB2Connection : System.ComponentModel.Component, IDbConnection, ICloneable
14 #region private data members
16 private ArrayList refCommands;
\r
17 private WeakReference refTransaction;
\r
18 private DB2ConnectionSettings connectionSettings = null;
\r
19 private int connectionTimeout;
\r
20 internal DB2OpenConnection openConnection;
\r
21 private bool disposed = false;
27 public DB2Connection()
31 public DB2Connection(string conString)
34 SetConnectionString(conString);
40 #region ConnectionString property
42 public string ConnectionString
46 return connectionSettings.ConnectionString;
50 SetConnectionString(value);
56 #region ConnectionTimeout property
57 public int ConnectionTimeout
61 return connectionTimeout;
65 connectionTimeout = value;
70 #region Database property
71 public string Database
75 return connectionSettings.DatabaseAlias;
80 #region State property
83 unsafe public ConnectionState State
87 if (openConnection == null)
88 return ConnectionState.Closed;
89 return ConnectionState.Open;
96 public event DB2InfoMessageEventHandler InfoMessage;
\r
97 public event StateChangeEventHandler StateChange;
\r
99 internal void OnInfoMessage(short handleType, IntPtr handle)
\r
101 if(InfoMessage != null)
\r
103 // Don't get error information until we know for sure someone is listening
\r
107 new DB2InfoMessageEventArgs(new DB2ErrorCollection(handleType, handle)));
\r
114 private void OnStateChange(StateChangeEventArgs args)
\r
116 if(StateChange != null)
\r
117 StateChange(this, args);
\r
124 internal IntPtr DBHandle
128 return (openConnection == null) ? IntPtr.Zero : openConnection.DBHandle;
133 #region BeginTransaction Method
135 IDbTransaction IDbConnection.BeginTransaction()
137 return BeginTransaction();
140 IDbTransaction IDbConnection.BeginTransaction(IsolationLevel isolationL)
142 return BeginTransaction(isolationL);
145 public DB2Transaction BeginTransaction()
147 return BeginTransaction(IsolationLevel.ReadCommitted);
150 public DB2Transaction BeginTransaction(IsolationLevel isolationL)
152 if ((refTransaction != null) && (refTransaction.IsAlive))
\r
153 throw new InvalidOperationException("Cannot open another transaction");
\r
154 if(State != ConnectionState.Open)
\r
155 throw new InvalidOperationException("BeginTransaction needs an open connection");
\r
157 if(refTransaction != null)
\r
159 if(refTransaction.IsAlive)
\r
160 throw new InvalidOperationException("Parallel transactions not supported");
\r
162 openConnection.RollbackDeadTransaction();
\r
163 refTransaction = null;
\r
165 openConnection.transactionOpen = true;
\r
166 DB2Transaction tran = new DB2Transaction(this, isolationL);
\r
167 refTransaction = new WeakReference(tran);
\r
173 #region ChangeDatabase
174 unsafe public void ChangeDatabase(string newDBName)
\r
176 if(connectionSettings == null)
\r
178 throw new InvalidOperationException("No connection string");
\r
182 SetConnectionString(connectionSettings.ConnectionString.Replace(connectionSettings.DatabaseAlias, newDBName));
\r
188 #region ReleaseObjectPool
189 public static void ReleaseObjectPool()
\r
191 DB2Environment.Instance.Dispose();
\r
196 public void Close()
\r
198 DB2Transaction transaction = null;
\r
199 if(refTransaction != null)
\r
200 transaction = (DB2Transaction)refTransaction.Target;
\r
201 if((transaction != null) && refTransaction.IsAlive)
\r
203 transaction.Dispose();
\r
205 if(refCommands != null)
\r
207 for(int i = 0; i < refCommands.Count; i++)
\r
209 DB2Command command = null;
\r
210 if(refCommands[i] != null)
\r
212 command = (DB2Command)((WeakReference)refCommands[i]).Target;
\r
214 if((command != null) && ((WeakReference)refCommands[i]).IsAlive)
\r
218 command.ConnectionClosed();
\r
222 //?? refCommands[i] = null;
\r
226 if(openConnection != null)
\r
228 openConnection.Close();
\r
229 openConnection = null;
\r
235 #region CreateCommand
236 IDbCommand IDbConnection.CreateCommand()
238 return CreateCommand();
241 public DB2Command CreateCommand()
244 return new DB2Command(null, this);
249 unsafe public void Open()
\r
253 throw new ObjectDisposedException("DB2Connection");
\r
256 if (this.State == ConnectionState.Open || this.State == ConnectionState.Connecting || this.State == ConnectionState.Executing || this.State == ConnectionState.Fetching)
\r
258 throw new InvalidOperationException("Connection already open");
\r
263 openConnection = connectionSettings.GetRealOpenConnection(this);
\r
265 catch (DB2Exception)
\r
274 public new void Dispose()
\r
277 GC.SuppressFinalize(this);
\r
280 protected override void Dispose(bool disposing)
\r
286 // dispose managed resources
\r
290 base.Dispose(disposing);
\r
301 private void CheckState()
\r
303 if (ConnectionState.Closed == State)
\r
304 throw new InvalidOperationException("Connection is currently closed.");
\r
307 void SetConnectionString (string connectionString)
\r
309 if (State != ConnectionState.Closed)
\r
310 throw new InvalidOperationException("Connection is not closed.");
\r
312 this.connectionSettings = DB2ConnectionSettings.GetConnectionSettings(connectionString);
\r
315 internal WeakReference WeakRefTransaction
\r
319 return refTransaction;
\r
323 refTransaction = value;
\r
328 internal void AddCommand(DB2Command command)
\r
330 if(refCommands == null)
\r
332 refCommands = new ArrayList();
\r
334 for(int i = 0; i < refCommands.Count; i++)
\r
336 WeakReference reference = (WeakReference)refCommands[i];
\r
337 if((reference == null) || !reference.IsAlive)
\r
339 refCommands[i] = new WeakReference(command);
\r
343 refCommands.Add(new WeakReference(command));
\r
347 internal void RemoveCommand(DB2Command command)
\r
349 for(int i = 0; i < refCommands.Count; i++)
\r
351 WeakReference reference = (WeakReference)refCommands[i];
\r
352 if(object.ReferenceEquals(reference, command))
\r
354 refCommands[i] = null;
\r
360 #region ICloneable Members
\r
362 object ICloneable.Clone()
\r
364 DB2Connection clone = new DB2Connection();
\r
366 clone.connectionSettings = connectionSettings;
\r
367 clone.connectionTimeout = connectionTimeout;
\r