Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Singleton class - does this work for a User Session or for whole client ??

Former Member
0 Likes
2,807

Hello

As per my understanding the Singleton is supposed to have only one instance of class per user session.

However, I am in doubt that whether Singleton is instantiated only once for multiple user sessions. Can any one validate this idea and have a definite answer ?

If singleton is instantiated only once per client (meaning multiple user sessions) will that be one singleton class used for all sessions ?

I would greatly appreciate your help and comments..

Best regards

Vijay

Edited by: jakk on Mar 23, 2009 8:41 PM

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
1,625

>

> I am in doubt that whether Singleton is instantiated only once for multiple user sessions.

No, that is not the case.

Hello

As per my understanding the Singleton is supposed to have only one instance of class per user session.

However, I am in doubt that whether Singleton is instantiated only once for multiple user sessions. Can any one validate this idea and have a definite answer ?

If singleton is instantiated only once per client (meaning multiple user sessions) will that be one singleton class used for all sessions ?

I would greatly appreciate your help and comments..

Best regards

Vijay

Edited by: jakk on Mar 23, 2009 8:41 PM

6 REPLIES 6
Read only

naimesh_patel
Active Contributor
0 Likes
1,625

Your understanding is correct: "one instance of class per user session".

It would create a new object for each different user session.

Try this sample code. Put a breakpoint in the method GET_OBJECT. Run the report, stop at the breakpoint. Now, start a new Session and run the report again and you will see that it would create a new object for the new session.


*----------------------------------------------------------------------*
*       CLASS lcl_add DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_add DEFINITION CREATE PRIVATE.

  PUBLIC SECTION.
    CLASS-DATA: w_time TYPE sy-uzeit.

    CLASS-METHODS:
      get_object
        EXPORTING
          eo_add TYPE REF TO lcl_add.

  PRIVATE SECTION.
    CLASS-DATA: lo_add TYPE REF TO lcl_add.

ENDCLASS.                    "lcl_add DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_add IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_add IMPLEMENTATION.

  METHOD get_object.

    IF lcl_add=>lo_add IS INITIAL.

      CREATE OBJECT lcl_add=>lo_add.

      GET TIME FIELD w_time.

    ENDIF.

    eo_add = lcl_add=>lo_add.

  ENDMETHOD.                    "get_object

ENDCLASS.                    "lcl_add IMPLEMENTATION


START-OF-SELECTION.

  DATA: o_add TYPE REF TO lcl_add.

  DATA: l_test TYPE char10.

  l_test = 'TEST'.

  CALL METHOD lcl_add=>get_object
    IMPORTING
      eo_add = o_add.

  WRITE: / lcl_add=>w_time.

  CLEAR o_add.

  CALL METHOD lcl_add=>get_object
    IMPORTING
      eo_add = o_add.

  WRITE: / lcl_add=>w_time.

Regards,

Naimesh Patel

Read only

0 Likes
1,625

The code that you gave is for a local singleton class. What would happen for Global Singleton class that is created from SE24 ?

Best regards

Vijay

Edited by: jakk on Mar 23, 2009 9:08 PM

Read only

0 Likes
1,625

That would remain same. Try with a global class same as this example local class.

Regards,

Naimesh Patel

Read only

matt
Active Contributor
0 Likes
1,626

>

> I am in doubt that whether Singleton is instantiated only once for multiple user sessions.

No, that is not the case.

Read only

Former Member
0 Likes
1,625

Thank you Matt.

I was thinking from conceptual point of view; that a Singleton (Global Class created in SE24) is instantiated only once for a user session.

Even though user is same (in case of RFC CALLS) each session (LUW) has separate Singleton intance. I hope this is correct understanding.

Best regards

Vijay.

Read only

naimesh_patel
Active Contributor
0 Likes
1,625

Yes. It is correct "Even though user is same (in case of RFC CALLS) each session (LUW) has separate Singleton intance".

Singleton object's life time is based on the internal ABAP Session.

Like:

If you have a Singleton object for some business requirement in the Sales Order Processing, than the singleton would be active till you left the order processing.

Regards

Naimesh Patel