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

Problems with static variables.

Former Member
0 Likes
3,046

I am trying to use the locking concept inside my function module using static variables. In the first run i expect static variables to be set and in the next run if this variable is set, then the same code shall not be executed by another user. Unfortunately i observe that after the first run, the value is lost and in the next run, the value of the static variable is not present. Why is this so and is there any way to implement this ?

Jay

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,841

u cant use static variables this way...as one user runs & next some other use runs from some other machine...instead once first user runs..may be u can set flag in a table and when second user runs...check for the flag and stop running the code...

10 REPLIES 10
Read only

Former Member
0 Likes
1,842

u cant use static variables this way...as one user runs & next some other use runs from some other machine...instead once first user runs..may be u can set flag in a table and when second user runs...check for the flag and stop running the code...

Read only

0 Likes
1,841

Then whats the use of static variables ?

Also would a export/import to memory be a better option than storing a flag in a database table ?

Read only

0 Likes
1,841

Hi Jaydeep,

Where did you declare the staics variable? In the top include or in the source code section of the fm?

Regards,

Ravi

Read only

0 Likes
1,841

I used it within the function module. Unfortunately when i declare it in the top include, it says statics can only be used in forms and function modules !!

Read only

0 Likes
1,841

Hi again,

1. If you want to use the STATIC concept,

(so that value is retained)

2. then use EXPORT IMPORT.

3. like this, inside the FM.

DATA : NUM TYPE I.

IMPORT NUM FROM MEMORY ID 'NUM'.

IF NUM = 0.

NUM = 5.

export num to memory id 'NUM'.

ENDIF.

4. THIS WILL WORK, EVEN IF WE RUN THE PROGRAM

MANY TIMES.

I just tried it.

regards,

amit m.

Read only

0 Likes
1,841

Hi again,

1. If u want to use such kind of flag/lock

ACROSS USERS - ACROSS SESSIONs,

2. then use EXPORT TO DATABASE concept

(and not export to memory id)

3. This code in FM, will make it clear and distinguishable.

(The value in NUM_DB will be imported, in new sessions, new logins also.)

4.

DATA : NUM_MEMORY TYPE I.

DATA : NUM_DB TYPE I.

IMPORT NUM_MEMORY FROM MEMORY ID 'NUM_MEMORY'.

<b>IMPORT NUM_DB FROM DATABASE INDX(XX) ID 'NUM_DB'.</b>

IF NUM_MEMORY = 0.

NUM_MEMORY = 5.

export num_MEMORY to memory id 'NUM_MEMORY'.

ENDIF.

IF NUM_DB = 0.

NUM_DB = 10.

<b> export num_DB to DATABASE INDX(XX) ID 'NUM_DB'.</b>

ENDIF.

regards,

amit m.

Read only

Former Member
0 Likes
1,841

export/import is used within a session...means on a system if one user executes & then second user executes it works...But users using various systems at various locations will not help it out.

Read only

Former Member
0 Likes
1,841

Hi jaydeep,

1. the value is lost and in the next run, the value of the static variable is not present

Thats true.

If we come out of the program, and then call the FM,

its value will be lost.

(BCOS the session is different, each time we run the program)

2. If we call the SAME FM TWICE, in the same program,

then in the 2nd call,

it will RETAIN the value.

( I tried, it works like this only)

regards,

amit m.

Read only

0 Likes
1,841

Then i think the only way to do the locking concept would be to store it in a database table or do a import/export ?

Read only

former_member283648
Participant
0 Likes
1,841

Hello Jaydeep,

The Import/ Export wont work because two users wont be accessing the code at the same time. Hence once one user comes out of run mode, u will lose the data. Better u use SET/GET parameters. It will work for sure. But make sure to clear the flag once the first user wants to unlock the data for the others to update.