Page 1 of 1

Jiiwa and Multiple Logins in the Same Application Space

PostPosted: Thu May 26, 2016 9:32 am
by SBarnes
Hi Guys

Is it possible to have Jiwa Login as multiple users within the one application space?

As currentlyly logging in involves a call like the line below:

JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Logon("MYSERVER", "JiwaDemo", JiwaFinancials.Jiwa.JiwaODBC.database.AuthenticationModes.JiwaAuthentication, "Admin", "password");

My question is related to the use of Jiwa through say an ASP.Net site or self hosted server where multiple users may need to login as themselves (Jiwa Login details) but obviously the code that then interacts with Jiwa will be running within the one application that could possilbly have Jiwa talking to multiple users at the same time.

Whilst as each action takes place the server can logon do what it needs to do and log off rather than try and keep connections open, I think there is still the possiblility of conflict.

Re: Jiiwa and Multiple Logins in the Same Application Space  Topic is solved

PostPosted: Thu May 26, 2016 10:13 pm
by Mike.Sheen
SBarnes wrote:Hi Guys

Is it possible to have Jiwa Login as multiple users within the one application space?

As currentlyly logging in involves a call like the line below:

JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Logon("MYSERVER", "JiwaDemo", JiwaFinancials.Jiwa.JiwaODBC.database.AuthenticationModes.JiwaAuthentication, "Admin", "password");

My question is related to the use of Jiwa through say an ASP.Net site or self hosted server where multiple users may need to login as themselves (Jiwa Login details) but obviously the code that then interacts with Jiwa will be running within the one application that could possilbly have Jiwa talking to multiple users at the same time.

Whilst as each action takes place the server can logon do what it needs to do and log off rather than try and keep connections open, I think there is still the possiblility of conflict.


Yes, this is possible but I'm not sure how performant it will be.

The JiwaApplication.Manager appears as a singleton, but you can coerce it to behave as a multiton - take a look at the code in this plugin in the samples an examples forum.

The code of interest is in the "SalesOrder_SaveEnd" method:

Code: Select all
Public Sub SalesOrder_SaveEnd(sender As Object, e As System.EventArgs)
      'Grab a copy of the sessionid that is for the currently logged in database
      Dim databaseSessionID As String = JiwaFinancials.Jiwa.JiwaApplication.Session.Instance.SessionID
      
      'Generate a new sessionid to use to identify the connection to the remote jiwa database
      Dim remoteDatabaseSessionID = System.Guid.NewGuid().ToString
      Try
         'Switch sessions (to a new one)
         JiwaFinancials.Jiwa.JiwaApplication.Session.Instance.SessionID = remoteDatabaseSessionID
         Try
            'Logon to remote database
            JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.Logon("ServerName", "DatabaseName", JiwaFinancials.Jiwa.JiwaODBC.database.AuthenticationModes.JiwaAuthentication, "Admin", "password")
            
            'Create a stock transfer
            Dim stockTransferObject As JiwaStockTransfer.StockTransfer
            stockTransferObject = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaStockTransfer.StockTransfer)(Nothing)
            stockTransferObject.CreateNew
            
            'Add a line
            Dim someInventoryID As String = "000000000K00000000BQ" 'Part No. 1170 in jiwa demo database
            stockTransferObject.Lines.AddLine("", "", someInventoryID)
            stockTransferObject.Lines(stockTransferObject.Lines.Count).TransferQuantity = 1
            
            'Save
            stockTransferObject.Save
            
            'Re-read after the save
            stockTransferObject.Read(stockTransferObject.RecID)
            
            'Activate
            stockTransferObject.ActivateRecord
         Finally
            'Logoff from remote database
            JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.LogOff
         End Try
      Finally
         'Set our session back to the currently logged in database
         JiwaFinancials.Jiwa.JiwaApplication.Session.Instance.SessionID = databaseSessionID
      End Try
   End Sub


You can see we note the current "SessionID", and then establish a new connection with a new sessionID, perform some actions then restore the sessionID in the finally block. In the context of a web server, you can maintain multiple simultaneous users from one process. You'd need to track and associate Jiwa SessionID's with a session variable in your webserver, but this should be quite easy. You can index the particular session when you have many currently established with the JiwaApplication.Manager using the Instances property - i.e.: JiwaApplication.Manager.Instances(MySessionID).

Re: Jiiwa and Multiple Logins in the Same Application Space

PostPosted: Fri May 27, 2016 9:16 am
by SBarnes
Hi Mike,

Thanks for the help, this should give me what I need as I can store the database session for the user either into a session variable for each user or back to the client in an encrypted cookie, I am leaning more towards the session, although I am not keen on using server memory, as it will give the option of cleaning up the connection and logging off the user properly if they just close the client when the session times out.

One last question which I suspect the answer to is going to be yes to do the Jiwa objects use a transaction and commit on save, I asking because otherwise I will probably start one on the database object before each section of the save code and do a commit or rollback based on the outcome.

By the way still no forums email, but I appreciate given the time of year like all of us you have a lot on your plate so when you get it it is fine, I'm just checking back every day or so in the meantime if I have posted anything as a work around.

Again thanks for the help.

Re: Jiiwa and Multiple Logins in the Same Application Space

PostPosted: Fri May 27, 2016 10:02 am
by Mike.Sheen
SBarnes wrote:One last question which I suspect the answer to is going to be yes to do the Jiwa objects use a transaction and commit on save, I asking because otherwise I will probably start one on the database object before each section of the save code and do a commit or rollback based on the outcome.


Yes - we start a transaction if one is not already started, and we will commit or rollback that transaction only IF we started that transaction.

You can start your own transaction using the BeginNewTransaction method of the JiwaApplication.Manager.Instance.Database object (or JiwaApplication.Manager.Instances(x).Database). You should use the BeginNewTransaction method because our objects depend on an internal SQLTransaction property being set. We also have some nice fault tolerence / resilience going on in there:

Code: Select all
Public Sub BeginNewTransaction(ByVal IsolationLevel As System.Data.IsolationLevel)
   Dim retryCount As Integer = 0
   Dim retryInterval As Integer

   Try
      SQLTransaction = Me.SQLConnection.BeginTransaction(IsolationLevel, Left(NewGUID.ToString, 32))
   Catch ex As SqlException
      If IsTransientFailure(ex) Then
         If _RetriesOnTransientFailure > 0 Then
            Do While retryCount <= _RetriesOnTransientFailure
               Try
                  If retryCount > 0 Then
                     Thread.Sleep(retryInterval) 'Exponential back-off
                  End If
                  EnsureConnectionValid(SQLConnection)
                  SQLTransaction = Me.SQLConnection.BeginTransaction(IsolationLevel, Left(NewGUID.ToString, 32))
                  Exit Do
               Catch ex2 As SqlException
                  If IsTransientFailure(ex2) Then
                     retryCount += 1
                     EnsureConnectionValid(SQLConnection)
                  Else
                     Throw
                  End If
               End Try
               retryInterval += RetryBaseMillisecondsInterval * retryCount
            Loop
            If SQLTransaction Is Nothing Then
               Throw New Exceptions.SQLTransientFailure
            End If
         Else
            Throw
         End If
      Else
         Throw
      End If
   End Try
   End Sub

Re: Jiiwa and Multiple Logins in the Same Application Space

PostPosted: Fri May 27, 2016 12:28 pm
by SBarnes
Hi Mike,

Thanks for the help.