By Mouhamadou Diaw

As of Oracle Database 12c Release 2 (12.2.0.1), when a physical standby database is converted into a primary you have the option to keep any sessions connected to the physical standby, without disruption, during the switchover/failover. When the database is reopened as the primary, the suspended sessions resume their operations as if nothing had happened. If the database (or an individual PDB) is not opened in the primary role, the sessions will be terminated.
To enable this feature, the STANDBY_DB_PRESERVE_STATES initialization parameter in the standby side is used. This parameter can have following values:
NONE — No sessions on the standby are retained during a switchover/failover.
SESSION or ALL — User sessions are retained during switchover/failover.
This parameter is only meaningful on a physical standby database that is open in real-time query mode. This needs Active dataguard option
In this blog we are going  to do a demonstration of this new feature. First we present below our configuration

DGMGRL> show configuration;
Configuration - ORCL_DR
Protection Mode: MaxProtection
Members:
ORCL_SITE - Primary database
ORCL_SITE1 - Physical standby database
ORCL_SITE2 - Physical standby database
Fast-Start Failover: DISABLED
Configuration Status:
SUCCESS (status updated 32 seconds ago)
DGMGRL>

Now let’s connect to the standby ORCL_SITE1 and let’s note our session’s info (sid, serial#)
SQL>
select username,sid, serial# from v$session where sid=SYS_CONTEXT('USERENV','SID');
USERNAME SID SERIAL#
--------------- ---------- ----------
SYSTEM 65 2869


SQL> show parameter db_unique_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_unique_name string ORCL_SITE1


SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY WITH APPLY

With the default value NONE for the parameter standby_db_preserve_states on ORCL_SITE1 let’s do a switchover to ORCL_SITE1.
SQL>
show parameter standby_db_preserve_states;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
standby_db_preserve_states string NONE
SQL>


DGMGRL> switchover to 'ORCL_SITE1';
Performing switchover NOW, please wait...
Operation requires a connection to database "ORCL_SITE1"
Connecting ...
Connected to "ORCL_SITE1"
Connected as SYSDBA.
New primary database "ORCL_SITE1" is opening...
Operation requires start up of instance "ORCL" on database "ORCL_SITE"
Starting instance "ORCL"...
ORACLE instance started.
Database mounted.
Database opened.
Connected to "ORCL_SITE"
Switchover succeeded, new primary is "ORCL_SITE1"

While the switchover going on, let’s start a query on ORCL_SITE1. As expected we get an error, the session was disconnected

SQL> select * from dba_objects;
select * from dba_objects
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel
Process ID: 1915
Session ID: 65 Serial number: 2869
SQL>

Our new configuration is now like this

DGMGRL> show configuration;
Configuration - ORCL_DR
Protection Mode: MaxProtection
Members:
ORCL_SITE1 - Primary database
ORCL_SITE - Physical standby database
ORCL_SITE2 - Physical standby database
Fast-Start Failover: DISABLED
Configuration Status:
SUCCESS (status updated 58 seconds ago)
DGMGRL>

Now let’s connect to the standby ORCL_SITE with the standby_db_preserve_states set to ALL

SQL> select username,sid, serial# from v$session where sid=SYS_CONTEXT('USERENV','SID');
USERNAME SID SERIAL#
--------------- ---------- ----------
SYSTEM 58 58847


SQL> show parameter db_unique_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_unique_name string ORCL_SITE


SQL> show parameter standby_db_preserve_states
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
standby_db_preserve_states string ALL

Now let’s do a switchover back to SITE_ORCL and let’s monitor the connection.

DGMGRL> switchover to 'ORCL_SITE';
Performing switchover NOW, please wait...
Operation requires a connection to database "ORCL_SITE"
Connecting ...
Connected to "ORCL_SITE"
Connected as SYSDBA.
New primary database "ORCL_SITE" is opening...
Operation requires start up of instance "ORCL" on database "ORCL_SITE1"
Starting instance "ORCL"...
ORACLE instance started.
Database mounted.
Database opened.
Connected to "ORCL_SITE1"
Switchover succeeded, new primary is "ORCL_SITE"
DGMGRL>

As expected, after the switchover I see that my session is still connected with the same SID and SERIAL#. Indeed user sessions are retained and when the database is reopened as the primary, the suspended sessions resume their operations as if nothing had happened.

SQL> select username,sid, serial# from v$session where sid=SYS_CONTEXT('USERENV','SID');
USERNAME SID SERIAL#
--------------- ---------- ----------
SYSTEM 58 58847

Just in the documentation it is mentioned that “Sessions that have long running queries or are using database links will not be retained regardless of the setting of this parameter”.