public void Rollback() { if (!transaction.WasCommitted && !transaction.WasRolledBack) { transaction.Rollback(); } }
/// <summary> /// Rollback the Transaction and Close Session /// </summary> /// <remarks> /// if the tranasaction has already been rollback or the session closed this will do nothing. /// You can perform this method multiple times, only the first time will take effect. /// </remarks> public void Rollback() { try { if (nhtransaction != null && !nhtransaction.WasCommitted && !nhtransaction.WasRolledBack) { nhtransaction.Rollback(); } } catch (Exception e) { //Catch the exception thrown from to prevent the original exception from being swallowed. try { ILog log = LogManager.GetLogger(typeof(SessionAndTransactionManager)); if (log.IsErrorEnabled) { log.Error("NHibernate.Burrow Rollback failed", e); } else { Console.WriteLine(e); } } catch (Exception) {} } finally { nhtransaction = null; } }
/// <summary> /// Implementors should rollback the /// transaction on the underlying resource /// </summary> public void Rollback() { //HACK: It was supossed to only a test but it fixed the escalated tx rollback issue. not sure if // this the right way to do it (probably not). if (!isAmbient) { transaction.Rollback(); } }
public void Rollback() { if (_disposed) { throw new ObjectDisposedException("NHibernateTransaction", "The object has been disposed"); } _transaction.Rollback(); _rolledBack(this); }
private void SaveOrUpdateValue() { NHibernate.ITransaction tran = iSabayaContext.PersistencySession.BeginTransaction(); try { int id = 0; if (!string.IsNullOrEmpty(hddValueID.Value)) { id = int.Parse(hddValueID.Value); } Incognito inc = null; if (id > 0) { inc = Incognito.Find(iSabayaContext, id); } else { inc = new Incognito(); } inc.Reference = txtReference.Text; inc.Remark = txtRemark.Text; inc.OrderedDate = ctrlOrderedDate.Date; inc.Agent = ctrlAgent.SelectedOrg; inc.Alias = txtAlias.Text; inc.Email = txtEmail.Text; inc.Faxes = txtFaxs.Text; inc.MobilePhone = txtMobile.Text; inc.Phone = txtPhone.Text; inc.CitizenOf = ctrlCitizenOf.Country; inc.Nationality = ctrlNationality.SelectedNode; inc.Occupation = ctrlOccupation.SelectedNode; inc.Religion = ctrlReligion.SelectedNode; inc.EffectivePeriod = ctrlEffectivePeriod.Period; inc.UpdatedBy = base.User; inc.UpdatedTS = DateTime.Now; inc.Save(iSabayaContext); tran.Commit(); hddValueID.Value = inc.PartyID.ToString(); } catch (Exception ex) { tran.Rollback(); if (ex.InnerException != null) { throw ex.InnerException; } else { throw ex; } } }
void ITransaction.Rollback() => nhTran.Rollback();
IMethodReturn IInterceptionBehavior.Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { IMethodReturn retour = null; TransactionalAttribute transactional = null; object[] atts = input.MethodBase.GetCustomAttributes(typeof(TransactionalAttribute), false); if (atts.Count() > 0) { transactional = (TransactionalAttribute)atts[0]; } if (Utility.MyNHibernateHelper.SessionManager.Configuration == null) { SessionManager.Configuration = TransactionInterceptorConfiguration.Configuration; TransactionInterceptorConfiguration.SessionManager = new SessionManager(); } if (NHibernate.Context.CurrentSessionContext.HasBind(SessionManager.SessionFactory) == false) { NHibernate.Context.CurrentSessionContext.Bind(SessionManager.SessionFactory.OpenSession()); } NHibernate.ISession session = null; NHibernate.ITransaction transaction = null; if (transactional != null) { session = TransactionInterceptorConfiguration.SessionManager.CurrentSession; if (transactional.OpenTransaction) { transaction = session.BeginTransaction(); } } try { retour = getNext()(input, getNext); if (transaction != null && transactional.RollBack == true) { transaction.Rollback(); //if (typeof(retour.Exception) == typeof(NHibernate.Exceptions.GenericADOException)) retour.Exception = null; } else if (retour.Exception == null && transaction != null) { transaction.Commit(); } else if (retour.Exception != null) { throw retour.Exception; } } catch (Exception ex) { if (transaction != null) { transaction.Rollback(); } else { throw ex; } } finally { if (transaction != null) { transaction.Dispose(); } if (session != null) { session.Close(); session.Dispose(); NHibernate.Context.CurrentSessionContext.Unbind(SessionManager.SessionFactory); } } return(retour); }
public void Rollback() { transaction.Rollback(); }
public void Rollback() { _tx.Rollback(); }
public void TestMethod1() { Customer c1 = new Customer() { Code = "C001", Name = "BlueHat Inc.", Email = "*****@*****.**" }; c1.Save(); Product prod1 = new Product() { Reference = "P01", Name = "Apple jus 50cl" }; prod1.Save(); Product prod2 = new Product() { Reference = "P02", Name = "Orange jus 25cl" }; prod2.Save(); NHibernate.ITransaction tx = SessionManager.GetSession().BeginTransaction(); try { PurchaseOrder po1 = new PurchaseOrder() { Number = "PO0001", Customer = c1, IssueDate = DateTime.Now.Date }; po1.Save(tx); PurchaseOrderLine line1 = new PurchaseOrderLine() { PurchaseOrder = po1, Position = 1, Product = prod1, Price = 10.5M, Quantity = 10 }; line1.Save(tx); PurchaseOrderLine line2 = new PurchaseOrderLine() { PurchaseOrder = po1, Position = 2, Product = prod2, Price = 4.35M, Quantity = 20 }; line2.Save(tx); tx.Commit(); } catch (Exception) { tx.Rollback(); throw; } }
/// <summary> /// 数据复制 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <param name="strTableName"></param> public static void BulkCopy <T>(DataTable dt, string strTableName) { MemoryStream ms = null; NHibernate.ITransaction txn = null; try { if (dt == null || dt.Rows.Count == 0) { return; } if (dt.Columns.Count == 0) { throw new Exception("The length of column cannot be zero."); } //从datatable中获取列名 List <string> lstField = new List <string>(); for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++) { lstField.Add(dt.Columns[colIndex].ColumnName); } string strFiledList = string.Format("({0})", string.Join(",", lstField.ToArray())); //拼写copy语句 const char RowSplit = '\n'; const char ColSplit = '\t'; string strCopyStatement = string.Format("copy {0}{1} from stdin record terminator E'{2}' delimiter E'{3}' enforcelength no commit", strTableName, strFiledList, RowSplit, ColSplit); //按照copy语句中的分隔符,分隔数据源 StringBuilder sbText = new StringBuilder(); foreach (DataRow dr in dt.Rows) { bool bFirstField = true; for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++) { string strVal = GetDataString(dr, colIndex); if (bFirstField) { sbText.Append(strVal); bFirstField = false; } else { sbText.AppendFormat("{0}{1}", ColSplit, strVal); } } sbText.Append(RowSplit); } //数据源写入内存流 string strTemp = sbText.ToString(); byte[] buff = Encoding.UTF8.GetBytes(strTemp); using (ms = new MemoryStream()) { ms.Write(buff, 0, buff.Length); ms.Flush(); ms.Position = 0; //建立vertica数据库连接 using (var session = NHibernateHelper <T> .OpenSession()) { txn = session.BeginTransaction(); var vcs = new VerticaCopyStream((VerticaConnection)session.Connection, strCopyStatement); vcs.Start(); vcs.AddStream(ms, false); vcs.Execute(); long insertedCount = vcs.Finish(); IList <long> lstRejected = vcs.Rejects; if (lstRejected.Count > 0) { txn.Rollback(); // Maybe need more detail info to show throw new Exception("Bulk copy failure."); } else { txn.Commit(); } } ms.Close(); } } catch (Exception ex) { txn.Rollback(); ms.Close(); throw (ex); } }