Database Transactions in Object Save  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Database Transactions in Object Save

Postby SBarnes » Fri May 11, 2018 11:45 am

Hi Mike,

If a business logic object is created and saved in the save of a Jiwa collection item and there is a transaction in progress will everything be covered by the same transaction i.e. if any part of the save fails then the business logic save will get rolled back as well?

Thanks.

Further to this there may be a thread safety issue with the Sales Order Business Logic Object because if I try to in the save of a Jiwa Collection Item try and create a credit note the following line throws an exception about indexes and collections and values being out of bounds as soon as there is more than one object in the collection, I've gotten around the problem by use of a for loop and starting my own transaction.

Code: Select all
fullorder.CreateNew(JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder.NewSalesOrderTypes.e_NewSalesOrderCreditNote, _Debtor.DebtorID, true, "",  "", " ", this.Manager.CurrentLogicalWarehouse.IN_LogicalID);
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Database Transactions in Object Save  Topic is solved

Postby Mike.Sheen » Mon May 14, 2018 2:59 pm

SBarnes wrote:If a business logic object is created and saved in the save of a Jiwa collection item and there is a transaction in progress will everything be covered by the same transaction i.e. if any part of the save fails then the business logic save will get rolled back as well?


Yes - provided that collection item save was invoked by some process which started a transaction.

We designed around the save of the business logic being what starts a new transaction if one is not already started and the collection items all use the current transaction. So if using our business logic and only ever invoking the iSave of a business logic object (as opposed to the iSave of any collection items) when everything should work as expected.

The basic pattern we follow is as follows:
Code: Select all
With Manager.Database
   Try
      If .SQLTransaction Is Nothing Then
         ' flag that we created the transaction, so we know if we should be the ones to .commit or .rollback later.
         .BeginNewTransaction()
         weStartedTransaction = True
      End If
      
      OnSaveStart

      ... Save code in here
      
      OnSaveEnding()

      If weStartedTransaction Then
         .SQLTransaction.Commit()
         .SQLTransaction.Dispose()
         .SQLTransaction = Nothing      
      End If

      OnSaveEnd()
   Catch ex As Exception
      If weStartedTransaction AndAlso .SQLTransaction IsNot Nothing Then
         If .SQLTransaction.Connection IsNot Nothing Then
            .SQLTransaction.Rollback()
         End If
         .SQLTransaction.Dispose()
         .SQLTransaction = Nothing
      End If

      Throw
   Finally
      If weStartedTransaction Then
         .SQLTransaction = Nothing
      End If
   End Try
End With


SBarnes wrote:there may be a thread safety issue with the Sales Order Business Logic Object because if I try to in the save of a Jiwa Collection Item try and create a credit note the following line throws an exception about indexes and collections and values being out of bounds as soon as there is more than one object in the collection


That shouldn't happen - we do something similar to that with recurring orders - the iSave method of the recurring Batch Line creates a new sales order business logic and adds lines to it and saves it.

How would I reproduce this? I'm guessing you've created your own business logic with a collection and within the iSave of the items of the collection is where you're creating and saving a credit note - would that be accurate?

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Database Transactions in Object Save

Postby SBarnes » Mon May 14, 2018 3:51 pm

Hi Mike,

This may be a bit of a ferfie, I haven't tried putting the code back to being asynchronous (in the save of the collection), but one thing I had forgot to mention is that it's saving and processing the order and one thing that I have discovered in a previous plugin is that if you save a new order and then don't reload it before calling process you do get an error sometimes that way.

I'll have a look at it and let you know, as I can always email you the plugin if the issue persists after that if you want to see if replicating the issue might highlight a threading problem.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 21 guests

cron