2 using System.Collections;
3 using System.Collections.Specialized;
5 using System.Runtime.InteropServices;
12 public 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()
29 connectionTimeout = 15;
33 public DB2Connection(string conString)
36 SetConnectionString(conString);
42 #region ConnectionString property
44 public string ConnectionString
48 return connectionSettings.ConnectionString;
52 SetConnectionString(value);
58 #region ConnectionTimeout property
59 public int ConnectionTimeout
63 return connectionTimeout;
67 connectionTimeout = value;
72 #region Database property
73 public string Database
77 return connectionSettings.DatabaseAlias;
82 #region State property
85 unsafe public ConnectionState State
89 //if ((long)dbHandle.ToPointer() == DB2Constants.SQL_NULL_HANDLE)
90 if (openConnection == null)
91 return ConnectionState.Closed;
92 return ConnectionState.Open;
99 public event DB2InfoMessageEventHandler InfoMessage;
\r
100 public event StateChangeEventHandler StateChange;
\r
102 internal void OnInfoMessage(short handleType, IntPtr handle)
\r
104 if(InfoMessage != null)
\r
106 // Don't get error information until we know for sure someone is listening
\r
110 new DB2InfoMessageEventArgs(new DB2ErrorCollection(handleType, handle)));
\r
117 private void OnStateChange(StateChangeEventArgs args)
\r
119 if(StateChange != null)
\r
120 StateChange(this, args);
\r
127 internal IntPtr DBHandle
131 return (openConnection == null) ? IntPtr.Zero : openConnection.DBHandle;
136 #region BeginTransaction Method
138 IDbTransaction IDbConnection.BeginTransaction()
140 return BeginTransaction();
143 IDbTransaction IDbConnection.BeginTransaction(IsolationLevel isolationL)
145 return BeginTransaction(isolationL);
148 public DB2Transaction BeginTransaction()
150 return BeginTransaction(IsolationLevel.ReadCommitted);
153 public DB2Transaction BeginTransaction(IsolationLevel isolationL)
155 if ((refTransaction != null) && (refTransaction.IsAlive))
\r
156 throw new InvalidOperationException("Cannot open another transaction");
\r
157 if(State != ConnectionState.Open)
\r
158 throw new InvalidOperationException("BeginTransaction needs an open connection");
\r
160 if(refTransaction != null)
\r
162 if(refTransaction.IsAlive)
\r
163 throw new InvalidOperationException("Parallel transactions not supported");
\r
165 openConnection.RollbackDeadTransaction();
\r
166 refTransaction = null;
\r
168 openConnection.transactionOpen = true;
\r
169 DB2Transaction tran = new DB2Transaction(this, isolationL);
\r
170 refTransaction = new WeakReference(tran);
\r
176 #region ChangeDatabase
177 unsafe public void ChangeDatabase(string newDBName)
\r
179 if(connectionSettings == null)
\r
181 throw new InvalidOperationException("No connection string");
\r
185 SetConnectionString(connectionSettings.ConnectionString.Replace(connectionSettings.DatabaseAlias, newDBName));
\r
191 #region ReleaseObjectPool
192 public static void ReleaseObjectPool()
\r
194 DB2Environment.Instance.Dispose();
\r
199 public void Close()
\r
201 DB2Transaction transaction = null;
\r
202 if(refTransaction != null)
\r
203 transaction = (DB2Transaction)refTransaction.Target;
\r
204 if((transaction != null) && refTransaction.IsAlive)
\r
206 transaction.Dispose();
\r
208 if(refCommands != null)
\r
210 for(int i = 0; i < refCommands.Count; i++)
\r
212 DB2Command command = null;
\r
213 if(refCommands[i] != null)
\r
215 command = (DB2Command)((WeakReference)refCommands[i]).Target;
\r
217 if((command != null) && ((WeakReference)refCommands[i]).IsAlive)
\r
221 command.ConnectionClosed();
\r
225 //?? refCommands[i] = null;
\r
229 if(openConnection != null)
\r
231 openConnection.Close();
\r
232 openConnection = null;
\r
238 #region CreateCommand
239 IDbCommand IDbConnection.CreateCommand()
241 return CreateCommand();
244 public DB2Command CreateCommand()
247 return new DB2Command(null, this);
253 unsafe public void Open()
\r
257 throw new ObjectDisposedException("DB2Connection");
\r
260 if (this.State == ConnectionState.Open || this.State == ConnectionState.Connecting || this.State == ConnectionState.Executing || this.State == ConnectionState.Fetching)
\r
262 throw new InvalidOperationException("Connection already open");
\r
267 openConnection = connectionSettings.GetRealOpenConnection(this);
\r
269 catch (DB2Exception)
\r
278 public new void Dispose()
\r
281 GC.SuppressFinalize(this);
\r
284 protected override void Dispose(bool disposing)
\r
290 // dispose managed resources
\r
294 base.Dispose(disposing);
\r
305 private void CheckState()
\r
307 if (ConnectionState.Closed == State)
\r
308 throw new InvalidOperationException("Connection is currently closed.");
\r
311 void SetConnectionString (string connectionString)
\r
313 if (State != ConnectionState.Closed)
\r
314 throw new InvalidOperationException("Connection is not closed.");
\r
316 this.connectionSettings = DB2ConnectionSettings.GetConnectionSettings(connectionString);
\r
319 internal WeakReference WeakRefTransaction
\r
323 return refTransaction;
\r
327 refTransaction = value;
\r
332 internal void AddCommand(DB2Command command)
\r
334 if(refCommands == null)
\r
336 refCommands = new ArrayList();
\r
338 for(int i = 0; i < refCommands.Count; i++)
\r
340 WeakReference reference = (WeakReference)refCommands[i];
\r
341 if((reference == null) || !reference.IsAlive)
\r
343 refCommands[i] = new WeakReference(command);
\r
347 refCommands.Add(new WeakReference(command));
\r
351 internal void RemoveCommand(DB2Command command)
\r
353 for(int i = 0; i < refCommands.Count; i++)
\r
355 WeakReference reference = (WeakReference)refCommands[i];
\r
356 if(object.ReferenceEquals(reference, command))
\r
358 refCommands[i] = null;
\r
364 #region ICloneable Members
\r
366 object ICloneable.Clone()
\r
368 DB2Connection clone = new DB2Connection();
\r
370 clone.connectionSettings = connectionSettings;
\r
371 clone.connectionTimeout = connectionTimeout;
\r