The PER_SESSION memory duration

A PER_SESSION memory pool is associated with each session. A session begins when a client connects to the database server, and it ends when the connection terminates.

When a C UDR allocates PER_SESSION memory, this memory is available to all routine instances that execute before the current session ends. When the current session ends, the database server reclaims any PER_SESSION shared memory in the memory context. It does not reclaim any memory in the memory context with a duration higher than PER_SESSION.

The PER_SESSION memory duration is useful for the following tasks:
  • External-resource management
  • Session initialization
  • Allocating memory within an end-of-session callback (if you have information to pass to the callback)
  • Using cursors defined as hold cursors (hold cursors can span transactions)
  • Caching expensive information between transactions for the life of the session or information that pertains to the session connection

Allocate PER_SESSION memory as named memory because this memory requires locking. To access it, a C UDR must know the name of the memory and it must be within the scope of the session. Such a UDR can explicitly free this memory with the mi_named_free() function. However, consider PER_SESSION memory as permanent to the session.

Several DataBlade® API constructor functions allocate their DataBlade API data type structures with a PER_SESSION memory duration. The following table shows the DataBlade API data type structures that have a memory duration of PER_SESSION.
Table 1. DataBlade API data type structures with a PER_SESSION memory duration
DataBlade API data type structure DataBlade API constructor function DataBlade API destructor function
Session-duration connection descriptor (MI_CONNECTION) mi_get_session_connection() End of session
Session-duration function descriptor (MI_FUNC_DESC) mi_cast_get(), mi_func_desc_by_typeid(), mi_routine_get(), mi_routine_get_by_typeid(), mi_td_cast_get()

(when these functions receive a session-duration connection descriptor as an argument)

End of session
File descriptor mi_file_open() mi_file_close()
Transient smart large object mi_lo_copy(), mi_lo_create(), mi_lo_expand(), mi_lo_from_file(), mi_lo_from_string()

(but do not insert the LO handle into a column of the database)

mi_lo_release(), mi_lo_delete_immediate()

Switching the current memory duration before one of the constructor functions in the preceding table does not change the PER_SESSION memory duration of the allocated DataBlade API structure. These data type structures are freed by their destructor function or when the current session ends.