122
Views
4
Comments
Handle Database session
Application Type
Traditional Web, Mobile, Reactive, Service

How can I run a database command whenever a database session is used, and then run another command whenever the session is released?

I understand that there is a pool of database sessions shared among users. For each request, a database session is assigned, and then released at the end of this request.

I'm using an Oracle database and I want to pass the user ID to the database session context:

  • When a session is acquired: set session context with user ID as input. (dbms_sessions.set_context)
  • When a session is released: Clear the context (dbms_sessions.clear_context)

I was able to do this for each server action but there is a large number of server actions. So I'm looking for a centralized solution where this is done in the background without developer intervention.

2018-10-29 08-31-03
João Marques
 
MVP

Hi Kahlil,


Have you taken a look at System Events?


In here you can centralize actions to run when a session starts (action to be executed in the first request to the server, after logging in, or on the first request after having logged out) or when a web request begins (action to be executed on each request to the server.), so you don't have to repeat it every time.


Note: this only applies for Traditional Web where session is maintained server side.


Hope it helps.


Regards,
João


2017-10-19 17-43-31
Khalil Dahham

OnBeginWebRequest would have been helpful if not for the following:

  • There is no alternative for mobile and reactive applications
  • There is no (OnEndWebRequest) to undo what I did in (OnBeginWebRequest) which is crucial.
2018-10-29 08-31-03
João Marques
 
MVP

Hi Khalil,


In mobile and reactive, you can take advantage of screen lifecycle events to control this (you have for instance the OnDestroy event to "undo what you did" in the OnInitialize / OnReady).

In order to avoid repeating the same actions for all screens, you can set those lifecycle events on the layout block.

In Traditional Web, you can also set your logic on the Preparation of your layout block.

2017-10-19 17-43-31
Khalil Dahham

Another limitation for OnBeginWebRequest:

  • Service actions are run in a separate transaction (different database session). So any changes done to the DB session in OnBeginWebRequest will not affect the service action's DB session.
  • The same goes for REST calls and timers
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.