This discussion is archived
10 Replies Latest reply: Nov 30, 2010 8:59 AM by 818001 RSS

How to specify an initial value for a sequence? (using DPL)

818001 Newbie
Currently Being Moderated
I've been trying to come up with a piece of code that would initialize the sequences defined within the annotations in my persistent classes, with no success. According to the documentation I could find, I figured that the following method would work:

public void initSequence(String sequenceName, String svalue) {
     SequenceConfig sequenceConfig = null;     
     try {
     sequenceConfig = m_store.getSequenceConfig(sequenceName);
     sequenceConfig.setInitialValue(Long.parseLong(svalue));
m_store.setSequenceConfig(sequenceName, sequenceConfig);
     System.out.println("Successfully initialized sequence: " + sequenceName);
     } catch (IllegalStateException e) {
// ignore; the sequence had already been opened
     }
     
} (where m_store is an EntityStore instance)

Before this method gets called, all my primary and secondary indices get created as in:

m_linkIndex = m_store.getPrimaryIndex(Integer.class, Class1.class);
etc.

And my classes look like the following:
@Entity
public class Class1 {
     
     @SecondaryKey(relate=MANY_TO_ONE)
     private String m_contentUrl;
     
     private String m_creatorId;
     
     @PrimaryKey(sequence="linkID")
     private int m_key;
     
etc.
}

Problem: when I execute the 'initSequence' method with proper parameters (e.g. 'linkID', '10000000'), it has no apparent effect on the values the sequence uses. In fact, the sequence starts at number 1 :-(.

Could somebody explain what is that I am doing wrong? (assuming the software is not buggy). I am using version
3.3.69. Thanks in advance!
  • 1. Re: How to specify an initial value for a sequence? (using DPL)
    greybird Expert
    Currently Being Moderated
    Hi,

    I think you forgot to call EntityStore.setSequenceConfig, after changing the initial value.

    --mark                                                                                                                                                                                                                       
  • 2. Re: How to specify an initial value for a sequence? (using DPL)
    818001 Newbie
    Currently Being Moderated
    Thanks for your reply, but I think you did not read my message thoroughly, since I am including that statement within the 'try' loop (m_store is my EntityStore instance) :-(. Any other suggestions?
  • 3. Re: How to specify an initial value for a sequence? (using DPL)
    greybird Expert
    Currently Being Moderated
    Sorry, you're right.
    Before this method gets called, all my primary and secondary indices get created as in:
    Perhaps the problem is that you're configuring the sequence after you open the index. The sequence (and database) configuration must occur before opening the index.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  • 4. Re: How to specify an initial value for a sequence? (using DPL)
    greybird Expert
    Currently Being Moderated
    } catch (IllegalStateException e) {
    // ignore; the sequence had already been opened
    } 
    You're ignoring the error that is telling you that you tried to configure the sequence after it was opened.

    But I see in the implementation that this exception is not thrown. That's a bug, and we'll fix it. But if you configure the sequence before getting the indexes that use it, it will work.

    Sorry for all the confusion.

    --mark

    Edited by: greybird on Nov 25, 2010 9:24 AM
  • 5. Re: How to specify an initial value for a sequence? (using DPL)
    818001 Newbie
    Currently Being Moderated
    Thanks again for your answer. Are you suggesting that I call the method before I create the indices? But then, can I be sure that the sequence already exists at that time? (the initSequence is called from a within a servlet init method that gets everything started). I am unclear on this, since the only place in which the sequence is defined is within the Java annotations in the class. Please clarify. Thanks again!
  • 6. Re: How to specify an initial value for a sequence? (using DPL)
    greybird Expert
    Currently Being Moderated
    If you change the SequenceConfig (using EntityStore.setSequenceConfig) before you call EntityStore.getPrimaryIndex (which creates the index and the sequence), it should work. This is the pattern for changing sequence and database configuration.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  • 7. Re: How to specify an initial value for a sequence? (using DPL)
    818001 Newbie
    Currently Being Moderated
    So I guess that means I should create the sequenceConfig instance myself then? Note that currently I retrieve the already existing sequenceConfig, then change it and set it back again in the EntityStore. And if so, will this be consistent with using the DPL? Would be very useful if you could send a simple example of how one would do this (e.g. what parameters should I send in the sequenceConfig constructor, etc.). Thanks again for your help.
  • 8. Re: How to specify an initial value for a sequence? (using DPL)
    greybird Expert
    Currently Being Moderated
    What you were doing before is correct. You get the config from the EntityStore, change it, then set it on the EntityStore. If you do this before you call getPrimaryIndex, then it will work. Please try it.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 9. Re: How to specify an initial value for a sequence? (using DPL)
    818001 Newbie
    Currently Being Moderated
    Ok, I'll try this out. Thanks again!
  • 10. Re: How to specify an initial value for a sequence? (using DPL)
    818001 Newbie
    Currently Being Moderated
    Working fine now. Thanks!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points