What does the Oracle APEX extend session time dialog actually do?

Matt MulvaneyMatt Mulvaney
4 min read

If you’ve been on your lunch break or just surfing the web for far too long… then you’ve probably returned to APEX with this dialog.

Have you ever wondered what actually happens when you click Extend?

To be clear, I’m talking about the idle timeout, this is the only thing that can be extended. It can be easily extended by refreshing the page, although you might not want to refresh the page (as you might be viewing some important information on it) and you just want to extend the idle timeout and keep viewing without refreshing.

Firstly, lets grab our timeouts using a special apex_session.emit_timeouts URL

 fetch('/ords/apex_session.emit_timeouts?p_app_id=' + apex.env.APP_ID + 
   '&p_session_id=' + apex.env.APP_SESSION)
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Running this returns

{
idle_time_ms: 3268000, 
life_time_ms: 27583000,
max_idle_time_ms: 3600000
}

In English, these are

Time TypeMillisecondsHoursMinutesSecondsTotal Time
Idle Time3,268,000054280h 54m 28s
Life Time27,583,000739437h 39m 43s
Max Idle Time3,600,0001001h 0m 0s

So basically, it idles out after 1 hour, I’ve got 54m 28s left and my total allowed session length remaining is 7h 39m 43s - down from 8hrs

When we click the extend button, the same URL is used however p_reset_idle=Y is appended to it

 fetch('/ords/apex_session.emit_timeouts?p_app_id=' + 
   apex.env.APP_ID + '&p_session_id=' + apex.env.APP_SESSION + '&p_reset_idle=Y')
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

This time we get

{
idle_time_ms: 3600000, 
life_time_ms: 27282000,
max_idle_time_ms: 3600000
}

In English, these are:

Time TypeMillisecondsHoursMinutesSecondsTotal Time
Idle Time3,600,0001001h 0m 0s
Life Time27,282,000734427h 34m 42s
Max Idle Time3,600,0001001h 0m 0s

So, the Idle Timeout has been reset and I’ve eaten another 5 mins into my total session life. In fact, thinking about this, I actually ate 5 mins of my actual life 🤔🤯

I’ll leave you with a present. If you want to simulate the message at any point…

… just run this

fetch('/ords/apex_session.emit_timeouts?p_app_id=' + apex.env.APP_ID + '&p_session_id=' + apex.env.APP_SESSION)
  .then(response => response.json())
  .then(timeoutData => {
    console.log("Current timeout data:", timeoutData);

    // Calculate actual session expiry time by adding idle_time_ms to current time
    var currentTime = new Date();
    var idleTimeSeconds = timeoutData.idle_time_ms / 1000;

    // Add the idle time to current time to get actual expiry
    currentTime.setSeconds(currentTime.getSeconds() + idleTimeSeconds);
    var expiryTimeString = currentTime.toLocaleTimeString(undefined, { timeStyle: "medium" });

    console.log("Session will actually expire at:", expiryTimeString);

    apex.message.showDialog(
      apex.lang.formatMessage("APEX.SESSION.ALERT.IDLE_WARN", expiryTimeString),
      {
        id: "apex_session_alert_dlg",
        okLabelKey: "APEX.SESSION.ALERT.EXTEND",
        confirm: true,
        callback: function(extendPressed) {
          if (extendPressed) {
            console.log("User chose to extend session");

            fetch('/ords/apex_session.emit_timeouts?p_app_id=' + apex.env.APP_ID + '&p_session_id=' + apex.env.APP_SESSION + '&p_reset_idle=Y')
              .then(response => response.json())
              .then(newTimeoutData => {

                $("#apex_session_alert_dlg").remove();
                console.log("Session extended with new timeout data:", newTimeoutData);

                // Calculate new expiry time using the new idle_time_ms
                var newCurrentTime = new Date();
                var newIdleTimeSeconds = newTimeoutData.idle_time_ms / 1000;

                newCurrentTime.setSeconds(newCurrentTime.getSeconds() + newIdleTimeSeconds);
                var newExpiryTimeString = newCurrentTime.toLocaleTimeString(undefined, { timeStyle: "medium" });

                console.log("NEW session expiry time:", newExpiryTimeString);
              });
          }
        }
      }
    );
  })

It produces sample output

> Current timeout data: {idle_time_ms: 3545000, life_time_ms: 26783000, max_idle_time_ms: 3600000}
> Session will actually expire at: 12:50:08
> User chose to extend session
> Session extended with new timeout data: {idle_time_ms: 3600000, life_time_ms: 26757000, max_idle_time_ms: 3600000}
> NEW session expiry time: 12:51:29

What next… A tampermonkey keep-alive modification? give me a few weeks.

ENJOY!

What’s the picture? Its The Old Swan Hotel, Harrogate. The Old Swan Hotel is most famously linked to the mysterious disappearance of crime writer Agatha Christie in 1926. After vanishing for 11 days, she was found staying at the hotel under a pseudonym, an event that became known as the “Harrogate Mystery”. Also The Beatles stayed here in 1963.

0
Subscribe to my newsletter

Read articles from Matt Mulvaney directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Matt Mulvaney
Matt Mulvaney

With around 20 years on the job, Matt is one of the most experienced software developers at Pretius. He likes meeting new people, traveling to conferences, and working on different projects. He’s also a big sports fan (regularly watches Leeds United, Formula 1, and boxing), and not just as a spectator – he often starts his days on a mountain bike, to tune his mind.