/// <summary> /// Implementors should commit the /// transaction on the underlying resource /// </summary> public void Commit() { transaction.Commit(); }
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); }
void ITransaction.Commit() => nhTran.Commit();
public void ImportFromDb() { using (NHibernate.ISession session = HibernateHelper.Open()) using (NHibernate.ITransaction transaction = session.BeginTransaction()) try { stagione_corrente = null; IList <Stagione> dbstagioni = session.QueryOver <Stagione>().OrderBy(x => x.DataFine).Desc.List(); foreach (Stagione p in dbstagioni) { fetchStagione(p); stagioni.Add(p); if (stagione_corrente == null || p.DataInizio > stagione_corrente.DataInizio) // calcola stagione corrente { stagione_corrente = p; } } IList <ListinoCorsi> dblistino = session.QueryOver <ListinoCorsi>().List(); foreach (ListinoCorsi p in dblistino) { fetchListino(p); listini.Add(p); } IList <Chiusura> dbchiusure = session.QueryOver <Chiusura>().OrderBy(x => x.DataFine).Desc.List(); foreach (Chiusura p in dbchiusure) { chiusure.Add(p); } IList <Persona> dbpersone = session.QueryOver <Persona>().OrderBy(x => x.Cognome).Asc.ThenBy(x => x.Nome).Asc.ThenBy(x => x.DataNascita).Desc.List(); foreach (Persona p in dbpersone) { fetchPersona(p); persone.Add(p); } IList <Corso> dbcorsi = session.QueryOver <Corso>().OrderBy(x => x.Attivo).Desc.ThenBy(x => x.Codice).Asc.ThenBy(x => x.DataFine).Desc.List(); //.ThenBy(x => x.DataFine).Asc.ThenBy(x => x.DataInizio).Asc.List(); foreach (Corso p in dbcorsi) { fetchCorso(p); corsi.Add(p); } IList <Istruttore> dbistruttori = session.QueryOver <Istruttore>().OrderBy(x => x.Cognome).Asc.ThenBy(x => x.Nome).Asc.List(); foreach (Istruttore p in dbistruttori) { fetchIstruttore(p); istruttori.Add(p); } IList <Iscrizione> dbiscrizioni = session.QueryOver <Iscrizione>().List(); foreach (Iscrizione p in dbiscrizioni) { iscrizioni.Add(p); } transaction.Commit(); } catch (Exception exc) { String errorString = "ImportFromDb::" + exc.Message; Log.Instance.WriteLine(Log.LogLevel.Error, errorString); } }
public void Commit() { _tx.Commit(); }
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; } }
public int UpdatefacebookReport_15(Domain.Socioboard.Domain.FacebookReport_15 _FacebookReport) { //Creates a database connection and opens up a session using (NHibernate.ISession session = SessionFactory.GetNewSession()) { //After Session creation, start and open Transaction. using (NHibernate.ITransaction transaction = session.BeginTransaction()) { //Proceed action to save data. int i = session.CreateQuery("update FacebookReport_15 set TotalLikes =:TotalLikes,TalkingAbout=:TalkingAbout,Likes=:Likes,PerDayLikes=:PerDayLikes,Unlikes=:Unlikes,PerDayUnlikes=:PerDayUnlikes,Impression=:Impression,PerDayImpression=:PerDayImpression,UniqueUser=:UniqueUser,StoryShare=:StoryShare,PerDayStoryShare=:PerDayStoryShare,ImpressionFans=:ImpressionFans,ImpressionPagePost=:ImpressionPagePost,ImpressionuserPost=:ImpressionuserPost,ImpressionCoupn=:ImpressionCoupn,ImpressionOther=:ImpressionOther,ImpressionMention=:ImpressionMention,ImpressionCheckin=:ImpressionCheckin,ImpressionQuestion=:ImpressionQuestion,ImpressionEvent=:ImpressionEvent,Organic=:Organic,Viral=:Viral,Paid=:Paid,M_13_17=:M_13_17,M_18_24=:M_18_24,M_25_34=:M_25_34,M_35_44=:M_35_44,M_45_54=:M_45_54,M_55_64=:M_55_64,M_65=:M_65,F_13_17=:F_13_17,F_18_24=:F_18_24,F_25_34=:F_25_34,F_35_44=:F_35_44,F_45_54=:F_45_54,F_55_64=:F_55_64,F_65=:F_65,Sharing_M_13_17=:Sharing_M_13_17,Sharing_M_18_24=:Sharing_M_18_24,Sharing_M_25_34=:Sharing_M_25_34,Sharing_M_35_44=:Sharing_M_35_44,Sharing_M_45_54=:Sharing_M_45_54,Sharing_M_55_64=:Sharing_M_55_64,Sharing_M_65=:Sharing_M_65,Sharing_F_13_17=:Sharing_F_13_17,Sharing_F_18_24=:Sharing_F_18_24,Sharing_F_25_34=:Sharing_F_25_34,Sharing_F_35_44=:Sharing_F_35_44,Sharing_F_45_54=:Sharing_F_45_54,Sharing_F_55_64=:Sharing_F_55_64,Sharing_F_65=:Sharing_F_65,Story_Fans=:Story_Fans,Story_PagePost=:Story_PagePost,Story_Other=:Story_Other,Story_Checkin=:Story_Checkin,Story_Coupon=:Story_Coupon,Story_Event=:Story_Event,Story_Mention=:Story_Mention,Story_Question=:Story_Question,Story_UserPost=:Story_UserPost where FacebookId =:FacebookId") .SetParameter("TotalLikes", _FacebookReport.TotalLikes) .SetParameter("TalkingAbout", _FacebookReport.TalkingAbout) .SetParameter("Likes", _FacebookReport.Likes) .SetParameter("PerDayLikes", _FacebookReport.PerDayLikes) .SetParameter("Unlikes", _FacebookReport.Unlikes) .SetParameter("PerDayUnlikes", _FacebookReport.PerDayUnlikes) .SetParameter("Impression", _FacebookReport.Impression) .SetParameter("PerDayImpression", _FacebookReport.PerDayImpression) .SetParameter("UniqueUser", _FacebookReport.UniqueUser) .SetParameter("StoryShare", _FacebookReport.StoryShare) .SetParameter("PerDayStoryShare", _FacebookReport.PerDayStoryShare) .SetParameter("ImpressionFans", _FacebookReport.ImpressionFans) .SetParameter("ImpressionPagePost", _FacebookReport.ImpressionPagePost) .SetParameter("ImpressionuserPost", _FacebookReport.ImpressionuserPost) .SetParameter("ImpressionCoupn", _FacebookReport.ImpressionCoupn) .SetParameter("ImpressionOther", _FacebookReport.ImpressionOther) .SetParameter("ImpressionMention", _FacebookReport.ImpressionMention) .SetParameter("ImpressionCheckin", _FacebookReport.ImpressionCheckin) .SetParameter("ImpressionQuestion", _FacebookReport.ImpressionQuestion) .SetParameter("ImpressionEvent", _FacebookReport.ImpressionEvent) .SetParameter("Organic", _FacebookReport.Organic) .SetParameter("Viral", _FacebookReport.Viral) .SetParameter("Paid", _FacebookReport.Paid) .SetParameter("M_13_17", _FacebookReport.M_13_17) .SetParameter("M_18_24", _FacebookReport.M_18_24) .SetParameter("M_25_34", _FacebookReport.M_25_34) .SetParameter("M_35_44", _FacebookReport.M_35_44) .SetParameter("M_45_54", _FacebookReport.M_45_54) .SetParameter("M_55_64", _FacebookReport.M_55_64) .SetParameter("M_65", _FacebookReport.M_65) .SetParameter("F_13_17", _FacebookReport.F_13_17) .SetParameter("F_18_24", _FacebookReport.F_18_24) .SetParameter("F_25_34", _FacebookReport.F_25_34) .SetParameter("F_35_44", _FacebookReport.F_35_44) .SetParameter("F_45_54", _FacebookReport.F_45_54) .SetParameter("F_55_64", _FacebookReport.F_55_64) .SetParameter("F_65", _FacebookReport.F_65) .SetParameter("Sharing_M_13_17", _FacebookReport.Sharing_M_13_17) .SetParameter("Sharing_M_18_24", _FacebookReport.Sharing_M_18_24) .SetParameter("Sharing_M_25_34", _FacebookReport.Sharing_M_25_34) .SetParameter("Sharing_M_35_44", _FacebookReport.Sharing_M_35_44) .SetParameter("Sharing_M_45_54", _FacebookReport.Sharing_M_45_54) .SetParameter("Sharing_M_55_64", _FacebookReport.Sharing_M_55_64) .SetParameter("Sharing_M_65", _FacebookReport.Sharing_M_65) .SetParameter("Sharing_F_13_17", _FacebookReport.Sharing_F_13_17) .SetParameter("Sharing_F_18_24", _FacebookReport.Sharing_F_18_24) .SetParameter("Sharing_F_25_34", _FacebookReport.Sharing_F_25_34) .SetParameter("Sharing_F_35_44", _FacebookReport.Sharing_F_35_44) .SetParameter("Sharing_F_45_54", _FacebookReport.Sharing_F_45_54) .SetParameter("Sharing_F_55_64", _FacebookReport.Sharing_F_55_64) .SetParameter("Sharing_F_65", _FacebookReport.Sharing_F_65) .SetParameter("Story_Fans", _FacebookReport.Story_Fans) .SetParameter("Story_PagePost", _FacebookReport.Story_PagePost) .SetParameter("Story_Other", _FacebookReport.Story_Other) .SetParameter("Story_Checkin", _FacebookReport.Story_Checkin) .SetParameter("Story_Coupon", _FacebookReport.Story_Coupon) .SetParameter("Story_Event", _FacebookReport.Story_Event) .SetParameter("Story_Mention", _FacebookReport.Story_Mention) .SetParameter("Story_Question", _FacebookReport.Story_Question) .SetParameter("Story_UserPost", _FacebookReport.Story_UserPost) .SetParameter("FacebookId", _FacebookReport.FacebookId) .ExecuteUpdate(); transaction.Commit(); return(i); } //End Using trasaction } //End Using session }
/// <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); } }