protected Session(string connectionString, IsolationLevel isoLevel) { //New session this.SessionID = Guid.NewGuid(); //Create new connection unique to this session this._Connection = Connection.GetConnection(this.SessionID.ToString(), connectionString, true); this._Transaction = new Transaction(this._Connection, this.SessionID, isoLevel); //Add it to the session manager Session._manager.Add(this); }
protected Session(Session parent, IsolationLevel isoLevel) { //Nested Session this.SessionID = Guid.NewGuid(); //Create savepoint transaction this._Connection = parent._Connection; this._Transaction = new Transaction(parent.Connection, this.SessionID, isoLevel); //Set parent/child stuff this.Parent = parent; this.Parent.Children.Add(this); }
public Transaction(Connection conn, Guid TransactionID, System.Data.IsolationLevel isolationLevel) { this.Initialize(conn, TransactionID, isolationLevel); }
public Transaction(Connection conn, Guid TransactionID) { this.Initialize(conn, TransactionID, System.Data.IsolationLevel.Unspecified); }
/// <summary> /// Handles starting the transaction /// </summary> protected void Initialize(Connection conn, Guid TransactionID, IsolationLevel isoLevel) { // New Transaction ID this._Id = TransactionID; this._Connection = conn; // That means no transaction exists on the connection - brand spankin' new transaction. if (this._Connection.ActiveTransaction == null) { this.Connection.ActiveTransaction = this; this._Type = TransactionType.Transaction; this._Transaction = this._Connection.SqlConnection.BeginTransaction(isoLevel); } else { //Nested transaction this._Type = TransactionType.SavePoint; this._Connection.ActiveTransaction.SqlTransaction.Save(this.Name); } }
/// <summary> /// Removes the connection from the Pool. /// </summary> public void Remove(Connection conn) { lock (ConnectionPool._pool) { ConnectionIdentifier id = new ConnectionIdentifier(Thread.CurrentThread.ManagedThreadId, conn.UniqueConnectionName); if(_pool.ContainsKey(id)) { ConnectionPool._pool.Remove(id); } else { throw new Exception("Could not find a connection in the ConnectionPool, Name = " + conn.UniqueConnectionName + " ConnectionString: " + conn.ConnectionString); } } }
/// <summary> /// On end of session, we need to commit or rollback the nested transaction /// </summary> public void Dispose() { //The transaction MUST be disposed FIRST to remove themselves from the connection this.Transaction.Dispose(); this._Transaction = null; //Only root sessions should dispose of the connection if (this.Parent == null) { this.Connection.Dispose(); Session._manager.Remove(); this._Connection = null; } }