/// <summary> /// Decreases the number of active used connections /// </summary> internal void DecreaseActiveConnectionCount(DatabaseContainer database) { lock (activeConnectionLock) { activeConnectionCount--; if (activeConnectionCount < 0) { throw new Exception(DataAccessResources.ParallelMultithreadingMalfunctionMessage); } lock (connections) { database.InUse = false; } Monitor.Pulse(activeConnectionLock); } }
/// <summary> /// Initializes a new instance of the DataBufferResourceLock class /// </summary> /// <param name="buffer">The Queue into which to put the connection after disposal of this lock</param> /// <param name="target">The container object used to cache the connection</param> /// <param name="innerLock">the inner lock of this resource lock</param> public DataBufferResourceLock(DatabaseConnectionBuffer buffer, DatabaseContainer target, IResourceLock innerLock) : this(buffer, target) { InnerLock = innerLock; }
/// <summary> /// Initializes a new instance of the DataBufferResourceLock class /// </summary> /// <param name="buffer">The Queue into which to put the connection after disposal of this lock</param> /// <param name="target">The container object used to cache the connection</param> public DataBufferResourceLock(DatabaseConnectionBuffer buffer, DatabaseContainer target) { this.buffer = buffer; this.target = target; }
/// <summary> /// Gets an available Database Connection from the pool of available connections /// </summary> /// <param name="useTransaction">indicates whether to begin a transaction for the returned database connection</param> /// <param name="database">the database that is available for usage</param> /// <returns>a connection that is unused at the moment</returns> public IResourceLock AcquireConnection(bool useTransaction, out IDbWrapper database) { string threadId = Thread.CurrentThread.LocalOwner(); DatabaseContainer db; IResourceLock inner = null; while (!disposing) { try { lock (connections) { var tmp = (from t in connections where !t.InUse && !t.Disposed && t.ThreadId == threadId select t).FirstOrDefault (); if (tmp != null) { tmp.InUse = true; tmp.LastUsage = DateTime.Now; } db = tmp; } if (db == null) { db = new DatabaseContainer { Database = factory.LoadPlugin <IDbWrapper>(DataAccessResources.ParallelDummyInstanceName, databaseConstructor, false), LastUsage = DateTime.Now, InUse = true, ThreadId = threadId }; lock (connections) { connections.Add(db); } } if (useTransaction) { inner = db.Database.AcquireTransaction(); } database = db.Database; lock (activeConnectionLock) { activeConnectionCount++; } OnConnectionAcquiring(database); return(new DataBufferResourceLock(this, db, inner)); } catch (Exception ex) { LogEnvironment.LogEvent(ex.Message, LogSeverity.Error, "DataAccess"); } } database = null; return(null); }