Home » SQL & PL/SQL » SQL & PL/SQL » getting last logon datetime (Oracle 10g)
getting last logon datetime [message #602789] Mon, 09 December 2013 03:06 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi all,

In many websites the login datetime and last logon datetime are shown to the user.In our application the front end is java so is there any process for getting user's last logon datetime. I mean from database side anythings needs to be done so that it can help to front end application.

Regards,
Nathan
Re: getting last logon datetime [message #602792 is a reply to message #602789] Mon, 09 December 2013 03:11 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
When you want to get the last logon date from the database, you have to store the logon date in the database during logon.
Re: getting last logon datetime [message #602795 is a reply to message #602792] Mon, 09 December 2013 03:38 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Yes Thomas,

Currently we are having the following procedure to update the last current datetime. But the problem is each time when the page get refreshed it's calling the procedure hence resulting the new value comes out in the same session .
It should be session specific. Please help me.
create table user_logon_info(user_id varchar2(20),last_logon date);

PROCEDURE last_login_info   (p_user_id       in varchar2,
                             p_current_login IN VARCHAR2,
                             p_last_login    OUT VARCHAR2)
AS  
  l_last_login DATE;
BEGIN  
    SELECT Max(login_date)
    INTO   l_last_login
    FROM   user_logon_info
    WHERE  user_id = p_user_id;

    p_last_login := To_char(l_last_login, 'dd/mm/yyyy hh24:mi:ss');

    INSERT INTO user_logon_info
                (user_id,login_date)
    VALUES      (p_user_id,( To_date(l_last_login, 'dd/mm/yyyy hh24:mi:ss') ));

    COMMIT;
EXCEPTION
  WHEN no_data_found THEN
             p_last_login := null;
END last_login_info;  
Re: getting last logon datetime [message #602797 is a reply to message #602795] Mon, 09 December 2013 03:49 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Sounds like the problem is the front end calling this procedure when it shouldn't.
Re: getting last logon datetime [message #602804 is a reply to message #602795] Mon, 09 December 2013 04:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
This function in itself, among many other things, sounds silly.. or I miss something.
It seems to insert in a table the row that it previously gets.
Without speaking about a TO_DATE on a DATE.
Also it commits.... VERY bad.
In the end, if the user never logged on then the function does nothing.

/forum/fa/917/0/

Re: getting last logon datetime [message #602806 is a reply to message #602789] Mon, 09 December 2013 04:39 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
You could make use of the audit trail, DBA_AUDIT_SESSION.
Re: getting last logon datetime [message #602809 is a reply to message #602806] Mon, 09 December 2013 05:00 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Sorry the actual insert statement is as follows. And why commit is bad here I am not getting.
INSERT INTO user_logon_info
                (user_id,login_date)
    VALUES      (p_user_id,( To_date(p_last_login, 'dd/mm/yyyy hh24:mi:ss') ));--for inserting the latest value.


And i do not know how to use dba_audit_session here.
Re: getting last logon datetime [message #602810 is a reply to message #602806] Mon, 09 December 2013 05:03 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
I doubt this suggestion a little.

I guess the OP is interested in the last-login-date of the end-user. I very much doubt that the end-user logs in to the database (via an application server).
Probably the application will use an application-specific user to login to oracle. The application then logs in the specific user.
DBA_AUDIT_SESSION supplies us with the logon information to the database (in this case probably from the application server).

However...I indeed have seen applications where the user more or less login to the database directly.
But since we are talking about a web application here, I'm quite sure that there is a web/application server in the middle.
Re: getting last logon datetime [message #602811 is a reply to message #602810] Mon, 09 December 2013 05:05 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I still don't understand what the procedure is trying to do.
Why are you not simply inserting sysdate when they login?
Re: getting last logon datetime [message #602812 is a reply to message #602811] Mon, 09 December 2013 05:17 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

So now we changed the code on session basis like for entire session when the same session comes again and again it will not insert any new record hence returning the old max logon datetime .
PROCEDURE last_login_info(p_user_id       IN VARCHAR2,
                          p_current_login IN VARCHAR2,
                          p_session_id    IN VARCHAR2,
                          p_last_login    OUT VARCHAR2)
AS
  l_last_login DATE;
  l_session_id VARCHAR2(30);
BEGIN
     BEGIN
          SELECT session_id,login_date
          INTO   l_session_id, l_last_login
          FROM   user_logon_info
          WHERE  login_date = (SELECT Max(login_date) FROM user_user_logon
                               WHERE  user_id = p_user_id);
      EXCEPTION
          WHEN no_data_found THEN
            l_last_login := NULL;
      END; 

    IF l_last_login IS NULL THEN
      INSERT INTO user_logon_info(user_id,login_date, session_id)
      VALUES      ( p_user_id,sysdate,p_session_id);

      p_last_login := NULL;
    ELSE     
      IF l_session_id=p_session_id THEN
        p_last_login := To_char(l_last_login,'dd/mm/yyyy hh24:mi:ss');
      ELSE 
       INSERT INTO user_logon_info (user_id, login_date, session_id) 
       VALUES ( p_user_id, sysdate, p_session_id); 
       p_last_login := To_char(l_last_login,'dd/mm/yyyy hh24:mi:ss');
    END IF;
  END IF;

    COMMIT;
END last_login_info; 

Please tell me if anything is wrong here.



[edit MC: Change "axis" to "user" at OP's request]

[Updated on: Mon, 09 December 2013 06:00] by Moderator

Report message to a moderator

Re: getting last logon datetime [message #602815 is a reply to message #602812] Mon, 09 December 2013 05:26 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Apart from the commit shouldn't be there - the front end application should issue commit when it's done - it's hard to say.
You still haven't really explained what the procedure is supposed to do. We don't know if your sessions and users relate to oracle sessions and users or not. We don't know under what circumstances the procedure is called. We don't know the structure of the tables.
Re: getting last logon datetime [message #602816 is a reply to message #602815] Mon, 09 December 2013 05:40 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

A user logged on to the website entering username and password .After that the web page should display the current login time and last successful login time. For getting last logon datetime we are calling the procedure. The front end application is java. User is not belongs to oracle it is other than of it. So there are 3 situation can arise.
1.First time when the user logged in (no data will be available in that table hence null will be returned and first insert should happen).
2.When the second time user logged in (the last login datetime will be returned from the table and first session will be getting inserted)
3.when the second time user logged in with same session then no insertion should happen and returned the old logon datetime.
4.When the third or more time the user logged in the new logged on datetime will be returned.
The table structure is
create table user_logon_info(user_id varchar2(20),last_logon date,session_id varchar2(30));
Re: getting last logon datetime [message #602818 is a reply to message #602816] Mon, 09 December 2013 05:44 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And where does user_logon fit into this logic?



EDIT PL: Removed bank name as per request

[Updated on: Mon, 09 December 2013 06:36] by Moderator

Report message to a moderator

Re: getting last logon datetime [message #602819 is a reply to message #602812] Mon, 09 December 2013 05:54 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Since the last logon datetime needs to be displayed in the frontend, the logic should be written in the frontend code. You do not require a stored procedure for this. You just need a SQL in your Java code which will just fetch the last record of session details for a particular user.

Your table needs to have following columns :
1. LOGIN_CD
2. SESSION_ID
3. DT_TM_STAMP

For every new session there will a new record inserted in this session details table for every user. Kind of audit. And before inserting your SQL would fetch the MAX(dt_tm_stamp) where LOGIN_CD=<current user>.
Previous Topic: Missing employees table
Next Topic: Please help me how concatenate all the error messages and display using OUT parameter value.
Goto Forum:
  


Current Time: Fri Apr 26 15:44:41 CDT 2024