Question

I want to auto increment the Primary Key in Berkeley Database. I use @PrimaryKey(sequence="Id"). It worked fine. but when i enter a another data the sequence is getting wrong. As a Example when i execute database.put the primary key is "1", but next time it is "101" and next time it is "201". This is my code. are there any thing to add. I+ didn't use SequenceConfig config = new SequenceConfig(); config.setAllowCreate(true);. Do i need to use it. Please help me.

@PrimaryKey(sequence="ID")

long id; String name;

 Login_Audit(String name) 
 {

         this.name = name;
     }
Was it helpful?

Solution

The sequence would only be wrong if it ever returned the same value twice. There's no requirement that values of a sequence should be consecutive, nor should you ever rely on them being so. The reason you're not getting consecutive numbers is probably the way BDB JE handles multi-threading efficiently: opening a handle to a sequence will "pre-allocate" a range of values to be used exclusively by that handle, so that it can give you new values without having to do an expensive database lock operation every time.

You can either just not care about the actual values of your IDs (this is the preferred option) or open the sequence manually using Database.openSequence() and manipulate it directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top