Programming Guide : Working with a Database : How Programming for Multiple Sessions Works : How You Can Switch Database Sessions
 
Share this page          
How You Can Switch Database Sessions
After you have opened additional database sessions for an application, you can switch between open sessions by setting the DBSession attribute. This attribute is available for both the FrameExec object and the ProcExec object, letting you switch sessions in a frame or in a 4GL procedure.
You can switch to an open session in any of the following locations:
The initialize block of the called frame
A frame-invoking statement (callframe, openframe, or gotoframe) in the calling frame or procedure
An event block
A 4GL procedure
A 3GL procedure
The following subsections describe how to switch databases in each of these situations.
How You Can Switch Sessions in a Called Frame
In addition to opening a new database session for a called frame in the initialize block of the called frame or the frame-invoking statement of the calling frame or procedure, you also can switch sessions for a called frame in either of these locations.
By default, a called frame inherits the value of the DBSession attribute from its calling frame or procedure. To switch sessions as you invoke the frame, you must change the value of this attribute in the frame-invoking statement. To switch sessions in the called frame, you use its initialize block.
The following examples demonstrate each of these methods:
Frame-invoking statement
You can use the with clause with the openframe, gotoframe, or callframe statement to place the application in a different open session. For example, the following statement opens the newframe frame and places the application in the session represented by the DBSessionObject third_session:
field1 = callframe newframe with dbsession = third_session;
Initialize block
You can use a statement in the called frame's initialize block to place the application in a different open session. If the reference variable that represents the DBSessionObject for the target session is a global variable, you can simply assign that variable value to the DBSession attribute of the called frame, for example:
initialize() =
begin
   CurFrame.DBSession = second_session;
   ...
end;
If the reference variable was declared as a local variable in the calling frame, you must pass the variable to the called frame as a parameter and then assign the value to the DBSession attribute, for example:
/* in the calling frame */
field1 = callframe newframe
   (sessno = second_session);

/* in the called frame */
initialize (
   sessno = DBSessionObject;) =
   begin
     CurFrame.DBSession = sessno;
   ...
   end;
If you use FrameExec's ParentFrame attribute to assign a called frame to a parent other than the calling frame, the called frame has the same session as the calling frame, not of the assigned parent. The following illustration of DBSessions and ParentFrames shows this process:
How You Can Switch Sessions in an Event Block
To change to another session only for one operation in a specific event block, set the DBSession attribute of the frame's FrameExec object in the event block. For example, the following event block switches to another session if the specified condition is true:
on add_customer =
begin
    /* If customer is rejoining the club, switch
    ** to session connected to the archived
    ** customer database. */
    if old_customer = TRUE
        CurFrame.DBsession = second_session;
    ...
    /* Switch back before exiting event block */
    CurFrame.DBsession = first_session;
end;
It is not necessary to switch back to the original session before the event block completes. Event block execution can begin in one session and end in another. However, if your frame has event blocks defined for any database events, the frame receives these events only when the application is running in the same session in which you registered the events for the frame. If you switch sessions in an event block to perform a task, be sure to switch back to the session in which the database events are expected.
For example, assume that session1 has registered for a database event and the current event block has changed sessions to session2. If a database event arrives while session2 is active, session1 does not receive the event.
For more information about database events, see Inter-Frame Communication Techniques.
If you open a new database session for the current frame in an event block, you must explicitly switch the current frame to the newly opened session. The following example opens a connection to the videos database and switches to the session for the current frame:
on click chg_session_btn =
begin
    status = videos_session.Connect (database =
            'videos');
    Curframe.DBsession = videos_session;
end;
How You Can Switch Sessions for a 4GL Procedure
To switch sessions from within a local 4GL procedure, you must set the CurProcedure.DBSession attribute for the ProcExec object. For example, the following statement switches the current procedure to the session represented by the DBSessionObject third_session:
CurProcedure.DBSession = third_session;
Note:  In contrast to the CurProcedure.DBSession attribute, setting the CurFrame.DBSession attribute from within a local procedure will not change the local procedure's session. Instead, it changes the calling frame's session after the local procedure returns.
How You Can Switch Sessions for 3GL and Database Procedures
To use 4GL code to switch the session for a 3GL procedure, you must do so before you call the procedure. For example, the following code could appear in an event block:
on click add =
begin
    CurFrame.DBSession = second_session;
    status = callproc check_name
            (name = cus_name);
    CurFrame.DBSession = first_session;
end;
You can use Embedded SQL to switch sessions within a 3GL procedure. However, when the procedure completes and control returns to OpenROAD, the application automatically returns to the session in which it was running when the procedure was called.
You cannot switch sessions within a database procedure. If you want the tasks in a database procedure to be performed in a different session, you must change the session before you call the procedure.