/// <summary> /// Initializes a new instance of the <see cref="AdoTransaction"/> class. /// </summary> /// <param name="p_ses"></param> internal AdoTransaction(ISessionTX p_ses) { //SharpLogger.Nfo("* AdoTransaction() construction. p_ses= {0}", p_ses.ToString()); this.session = p_ses; //SharpLogger.CallerOut(); }
/// <summary> /// Manejador de conexiones contra la BBDD /// </summary> /// <param name="p_ses">Session que contiene este Connection Manager</param> /// <param name="p_connectionReleaseMode">Pues eso...</param> internal CnnManager(ISessionTX p_ses, ConnectionReleaseMode p_connectionReleaseMode) { IDbConnection suppliedConnection = null; TraceLog.LogEntry("CnnManager(p_ses= {0} p_connectionReleaseMode= {1})", p_ses.SessionId, p_connectionReleaseMode); this.m_session = p_ses; this.m_connection = suppliedConnection; this.connectionReleaseMode = p_connectionReleaseMode; ownConnection = (suppliedConnection == null); // De esta forma, siempre será True. //SharpLogger.CallerOut(); }
private void AfterTransactionCompletion(bool successful) { //TraceLog.LogEntry("* AfterTransactionCompletion(): Hash= 0x{0:X} successful= {1} ", this.trans.GetHashCode(), successful); //using (new SessionIdLoggingContext(sessionId)) { this.session.AfterTransactionCompletion(successful, this); this.NotifyLocalSynchsAfterTransactionCompletion(successful); this.session = null; this.begun = false; } }
///<summary> ///Use this construction when requires 'BaseDAL' works opening a indepent connection. ///</summary> ///<param name="p_ses">Session instance to be used by DAL repository. Validity of internal connection string will be checked.</param> //internal BaseDAL(ISession p_ses) public BaseDAL(ISession p_ses) { this.m_session = p_ses as ISessionTX; //SharpLogger.Nfo("Instanciado {0} SessionId {1}",this.GetType().Name,this.m_session.SessionId); //ValidationUtility.ValidateArgument("CnnStrName", this.m_session.CnnStrName); }
/// <summary> /// Gets the SqlConnection to be used as part of the current transaction. /// </summary> /// <param name="p_cnnMgr">The name of the connection string to use.</param> /// <returns>The SqlConnection associated with the current transaction.</returns> /// <remarks>If no SqlConnection exists, one will be created.</remarks> private static SqlConnection GetTransactedSqlConnection(ISessionTX p_sessionTX) { LocalDataStoreSlot connectionDictionarySlot = Thread.GetNamedDataSlot("ConnectionDictionary"); Dictionary<string, SqlConnection> connectionDictionary = (Dictionary<string, SqlConnection>)Thread.GetData(connectionDictionarySlot); if (connectionDictionary == null) { Thread.SetData(connectionDictionarySlot, connectionDictionary = new Dictionary<string, SqlConnection>()); TraceLog.LogEntry("GetTransactedSqlConnection()", "No Connection Dictionary Cache found in ThreadSlot -> Create and stored one."); } IDbConnection cnn = null; if (connectionDictionary.ContainsKey(p_sessionTX.CnnStrName)) { cnn = connectionDictionary[p_sessionTX.CnnStrName]; if (cnn!=null) TraceLog.LogEntry("GetTransactedSqlConnection(): Key '{0}' CACHED in ThreadSlot, Maps -> SqlConn Hash 0x{1:X} State: {2}", p_sessionTX.CnnStrName, cnn.GetHashCode(), cnn.State); } else { if ((cnn = (p_sessionTX as SessionExt).ConnectionManager.GetConnection()) != null) // Si es necesario, el manager la instancia y además la conecta... { connectionDictionary.Add(p_sessionTX.CnnStrName, (cnn as SqlConnection)); TraceLog.LogEntry("GetTransactedSqlConnection(): Key '{0}' NOT CACHED in ThreadSlot: Added SqlConn Hash 0x{1:X} State: {2}", p_sessionTX.CnnStrName, cnn.GetHashCode(), cnn.State); Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted); } } return cnn as SqlConnection; }
/// <summary> /// /// </summary> /// <param name="p_cnnMgr">Name for connection manager on current session.</param> /// <param name="p_sqlCmd">Built instance of SqlCommand to execute expecting a reader.</param> /// <returns>A <see cref="SqlDataReader"/>Containing the results of the stored procedure execution.</returns> public static SqlDataReader ExecuteReader(ISessionTX p_sessionTX, string commandText, params SqlParameter[] parameters) { CommandBehavior beh = CommandBehavior.Default; IDbConnection m_sqlCnn = null; string strInfoExcep = String.Empty; TraceLog.LogEntry("ExecuteReader({0} par: {1}) ", commandText, parameters.Length); try { if (Transaction.Current == null) { m_sqlCnn = p_sessionTX.Connection; //p_sesTX.ConnectionManager.GetConnection() as SqlConnection; beh = CommandBehavior.CloseConnection; } else { TraceLog.LogEntry("ExecuteReader(): Transaction.Current: LocalId {0} \t - Status {1} CurrentTime: {2}", Transaction.Current.TransactionInformation.LocalIdentifier, Transaction.Current.TransactionInformation.Status, Transaction.Current.TransactionInformation.CreationTime); m_sqlCnn = SqlClientUtility.GetTransactedSqlConnection(p_sessionTX); } return SqlClientUtility.CreateCommand(m_sqlCnn, CommandType.StoredProcedure, commandText, parameters).ExecuteReader(beh); } catch (Exception ex) { if (ex.InnerException != null) strInfoExcep = ex.ToString() + "/" + ex.InnerException.ToString(); else strInfoExcep = ex.ToString() + "/" + "-"; TraceLog.LogEntry("Error:", strInfoExcep); return null; } }
public static void ExecuteNonQuery(ISessionTX p_sessionTX, List<SqlClientCommand> p_lstSqlCmd) { int res = -1; TraceLog.LogEntry("ExecuteNonQuery(): p_lstSqlCmd.Count= {0}", p_lstSqlCmd.Count); long ticks_t0 = DateTime.Now.Ticks; IDbConnection m_sqlCnn = null; if (Transaction.Current == null) m_sqlCnn = p_sessionTX.Connection; //(p_cnnMgr.GetConnection()); else m_sqlCnn = SqlClientUtility.GetTransactedSqlConnection(p_sessionTX); ITransaction tx = null; if (p_sessionTX.IsInActiveTransaction) { tx = (p_sessionTX as SessionExt).Transaction; TraceLog.LogEntry("ExecuteNonQuery(): p_sessionTX is in active Transaction (Hash 0x{0:X}) WasCommitted= {1} WasRolledBack= {2}). Command batch will be enlisted to...", tx.GetHashCode(), tx.WasCommitted, tx.WasRolledBack); } else { TraceLog.LogEntry("ExecuteNonQuery(): p_sessionTX not in active Transaction, SqlConnection (Hash 0x{0:X} State {1}) will create one to enlist command batch...", m_sqlCnn.GetHashCode(), m_sqlCnn.State); tx = (p_sessionTX as SessionExt).BeginTransaction(); } using(tx) { //bool commitBatch = false; try { SqlCommand p_cmdi = null; for (int i = 0; i < p_lstSqlCmd.Count; i++) { tx.Enlist(p_cmdi= SqlClientUtility.CreateCommand(m_sqlCnn as SqlConnection, CommandType.StoredProcedure, p_lstSqlCmd[i].CommandText, p_lstSqlCmd[i].Parameters)); TraceLog.LogEntry("ExecuteNonQuery(): Command ({2} {3}) SqlConn. 0x{0:X} Trans. 0x{1:X}", p_cmdi.Connection.GetHashCode(), p_cmdi.Transaction.GetHashCode(), p_cmdi.CommandText, p_lstSqlCmd[i].ParametersDescr); res = p_cmdi.ExecuteNonQuery(); } //throw new Exception("Por joder... para forzar un RollBack justo ahora..."); //commitBatch = true; tx.Commit(); TraceLog.LogEntry("ExecuteNonQuery(): Command batch Commited. Time: {0} mseg.", TimeSpan.FromTicks(DateTime.Now.Ticks - ticks_t0).TotalMilliseconds); } catch(Exception ex) { TraceLog.LogEntry("Error ExecuteNonQuery(): " + ex.ToString()); tx.Rollback(); if (ex is SqlException) throw ex; } } }
public static void ExecuteNonQuery(ISessionTX p_sessionTX, SqlClientCommand p_command) { TraceLog.LogEntry("ExecuteNonQuery(): Command {0} {1}", p_command.CommandText, p_command.ParametersDescr); int res = -1; bool m_mustCloseSqlCnn = true; string m_tx = "Transact NONE"; IDbConnection m_sqlCnn = null; string strInfoExcep = String.Empty; long ticks_t0 = DateTime.Now.Ticks; if (Transaction.Current == null) m_sqlCnn= p_sessionTX.Connection; //p_sessionTX.ConnectionManager.GetConnection() as SqlConnection; // Root session connection will be used... else { TraceLog.LogEntry("ExecuteNonQuery(): Transaction.Current: LocalId {0}\t - Status {1}", Transaction.Current.TransactionInformation.LocalIdentifier, Transaction.Current.TransactionInformation.Status); m_sqlCnn = SqlClientUtility.GetTransactedSqlConnection(p_sessionTX); m_mustCloseSqlCnn = false; } SqlCommand sqlc = SqlClientUtility.CreateCommand(m_sqlCnn as SqlConnection, CommandType.StoredProcedure, p_command.CommandText, p_command.Parameters); if (p_sessionTX.IsInActiveTransaction) { ITransaction tx = (p_sessionTX as SessionExt).Transaction; TraceLog.LogEntry("ExecuteNonQuery(): p_sessionTX has an active Transaction (0x{0:X} WasCommitted= {1} WasRolledBack= {2}) Command will be enlisted to...", tx.GetHashCode(), tx.WasCommitted, tx.WasRolledBack); tx.Enlist(sqlc); m_tx = "Transact 0x" + sqlc.Transaction.GetHashCode().ToString("X"); m_mustCloseSqlCnn = false; } res = sqlc.ExecuteNonQuery(); if (m_mustCloseSqlCnn && sqlc.Connection.State == ConnectionState.Open) sqlc.Connection.Close(); double mseg = TimeSpan.FromTicks(DateTime.Now.Ticks - ticks_t0).TotalMilliseconds; TraceLog.LogEntry("ExecuteNonQuery(): Command Executed, {0} SqlConn 0x{1:X}. Time {2} mseg.", m_tx, sqlc.Connection.GetHashCode(), mseg); }
public IDbConnection Close() { TraceLog.LogEntry("CnnManager.Close(): this.transaction= ", this.transaction == null ? "NULL" : ("0x" + this.transaction.GetHashCode().ToString("X") + " :IsActive= " + this.transaction.IsActive.ToString())); IDbConnection res = null; if (transaction != null) { transaction.Dispose(); } // When the connection is null nothing needs to be done - if there // is a value for connection then Disconnect() was not called - so we // need to ensure it gets called. if (m_connection == null) { ownConnection = false; res = null; } else { res= this.Disconnect(); } if (this.m_session != null) { //SharpLogger.Nfo("m_session NO NULL.. se hará NULL ahora."); this.m_session = null; } //SharpLogger.CallerOut(); return res; }
/// <summary> /// Binds the specified session to the current context. /// </summary> public static void Bind(ISessionTX session) { GetCurrentSessionContext(session.SessionFactory).Session = session; //if(session!=null) SharpLogger.Nfo("* Session instance SessionId {0} binded to factory.",session.SessionId); }