cancel
Showing results for 
Search instead for 
Did you mean: 

Auto increment variable (global)

Former Member
0 Kudos

hi all,

let say i've a db table with columns sno, sname, marks. and i want to make sno as global auto incremental variable so tht when i insert record sno should get the current incremented value on screen even though the appn restarted/refreshed. how do i do it?

i've created sno in comp.controller and mapped to view controller. it works on insert action, but when i refresh the page sno will set to ZERO. will it be possbile in wd-java?

tnx,

-JB

Accepted Solutions (1)

Accepted Solutions (1)

former_member197348
Active Contributor
0 Kudos

Hi,

It is not possible to get back the value after restarting the application. For this you must store the value in the backend and you need to fetch the value when application restarts.

Regards,

Siva

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi

We had this situation some time ago and solved it doing these steps:

-Create a java class like this:

public class GlobalCounter{

	private static long longId = -2; 
	
	private static synchronized long getNext(){
		return ++longId;
	}

	public GlobalCounter() {
		if(this.getNext()==-1){
			//get the last id of your DB or Backend
			long lastId = getLastNumberFromBackend();
			this.longId = ++lastId;
		}
	}

}

What you do here is that you hava a static value that is initialized to -2 (you can change this if you want) and in the constructor method you ask if the getNext value equals -1 you will ask your backend or database to get you the last number, this is only done once because that number should be at least 0 or greater, this static variable will keep the number from the backend and each time you want to increment that value you will call GlobalCounter.getNext(); method. If the server is restarted the you will call the backend or database just once again to get the last number.

To make it short, you only call your backend one time to get the last number every time the AS is restarted but not each time you want to create a new record.

Hope this helps.

Cheers

JV

Former Member
0 Kudos

Hi,

It will work in component controller and in view.

Regards,

Sunaina reddy T

Former Member
0 Kudos

HI,

you can declare in between the tags

//@@begin others

//@@end

for global declaration

Regards,

Satya.

Former Member
0 Kudos

hi satya,

is it in view or comp controller? i've done this before in view, but this never worked for me..!

tnx,

JB