protected T Do <T>(DBBuilder dbBuilder, Func <IDbConnection, T> fn) { if (dbBuilder.Transaction != null) { return(fn(dbBuilder.Transaction.Connection)); } else if (this.Transaction != null) { return(fn(dbBuilder.SetTransaction(this.Transaction).Transaction.Connection)); } else { using (var connection = DBAdapter.CreateConnection(this.connection.ConnectionString)) { if (connection.State != ConnectionState.Open) { connection.Open(); } T result = fn(connection); if (connection.State != ConnectionState.Closed) { connection.Close(); } return(result); } } }
/// <summary> /// 创建新连接开启事务 /// </summary> public void Begin(string connectionName = null) { IDbConnection connection = DBAdapter.CreateConnection(CFConfig.GetConnection(connectionName).ConnectionString); connection.Open(); this.Transaction = connection.BeginTransaction(IsolationLevel.Serializable); disposeConnection = true; }