Hey guys,

I wanted to use a JDO persistence-backed Sequence to provide
monotonically increasing visitor #'s in my webapp. A static variable
doesn't cut it because I want the number to survive redeployments,
upgrades, etc.

I didn't see any info on Sequence in the wiki "Using JDO" section so I
did this. It works but it seemed kinda hacky. Is this the best way to
configure a JDO-backed Sequence in AppEngine?

(FWIW I'm using the eclipse plug-in.)

(1) Make the following file: MyProject/META-INF/package.jdo
    * This directory should already contain a "jdoconfig.xml".
    * Both files will be autoplaced into MyProject/war/WEB-INF/classes/
META-INF on deploy.

(2) Paste the following XML into package.jdo:

<?xml version="1.0" encoding="UTF-8" ?>
<jdo xmlns="http://java.sun.com/xml/ns/jdo/jdo";
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdo
http://java.sun.com/xml/ns/jdo/jdo_2_2.xsd";>
        <!-- name the package whatever you want -->
        <package name="MyProject">
                <sequence name="VisitorSequence"
                                   datastore-
sequence="VISITOR_SEQUENCE"
                                   strategy="contiguous"
                 />
        </package>
</jdo>

(3) When you need the next value in your application, call:

PersistenceManager pm = //...allocate pm

//the sequence name is <package name>.<sequence name>
//from the corresponding tags in package.jdo
Sequence seq = pm.getSequence("MyProject.VisitorSequence");

//If "strategy" from package.jdo was "contiguous", this has to happen
//in a transaction.
long nextValue = seq.nextValue();
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to