Nhibernate: Handling an ITransaction Exception So That New Transactions Can Continue with same ISession -
I have a list of 10 data objects that I want to add / update to the database using NHibernate. If someone puts an exception (say a primary key infringement), then I still want to add / update the other 9. If I rolled each object operation into its nuclear transaction, and there is no exception, then roll back the transaction. The problem is that if a transaction causes an exception and it is brought back, then on the next transaction, Nhibernate complains with the error: An empty ID in the Nexus.Data.PortfolioCorporateEntity entry (being an exception Do not flush the session after)
My main program is simple This session creates sessions from session, creates data access layer, does some work on data object, and then those data in the database Releasing objects Tries to eat Session session = new NHibernateSessionManager (); Session = sessionsManager.GetSession (); DALC = New NHibernateDataProvider (session); ... foreign (different paces in pence) {try {DALC.UpdateOrAddObject & lt; Portfolio Corporate Entity & gt; (PS); } Hold (Exception Pre) {Console.WriteLine ("Could not add corporate entity ID" + pce.CorporateEntity.CorporateEntityID.ToString ()); }}
This is the AddOn Add process in my Nhibernate Data Access Layer, which is called 10 times for 10 objects.
Public Zero UpdateOrAddObject & lt; T & gt; (T WorkObject) {Use (ITRsaction tx = msation.beneferenceaction) {try {mSession.SaveOrUpdate (workObject); MSession.Flush (); Tx.Commit (); } Hold (exception before) {tx.Rollback (); throw; }}}
To clarify the point, the session is immediately given by the calling program and the data is given to the access layer object, whose creator is below.
Public NHibernateDataProvider (Asnation Session) {m session = session; }
Except exception, it works fine, it says that do not flush the session after the exception. I'm not sure - the transaction was rolled back well and should the database be ready to accept another transaction? What am i doing wrong
It is not possible to reuse the NHibernate session after an exception is made. :
If an issue throws an exception, you should immediately withdraw the transaction, call it at the ISAAS. Discard () and ignore the eassion example. In some ways the dispute will not leave the session in constant state.
So the answer is that you can not do what you are trying to do. You need to create a new session and retry the updates.
Comments
Post a Comment