HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"UniqueID Generator: A Pattern for Primary Key Generation"
Vol. 5, Issue 12, p. 58

	


Listing 1


/**
* Singleton exists on a per JVM basis and returns a new, unique ID 
* to the caller.
*/
public class Singleton {
    private long max = 0;
    private long currentID = 0;
    private static Singleton singleton = new Singleton();
    private UniqueID uniqueIDEJB;
    /**
    * Accessor to get handle to singleton object.
    */
    public Singleton instance() {
	return singleton;
    }
    /**
    * Cannot instantiate.  Must use instance() method to get a handle to the object. 
    */
    private Singleton() { 
	// GET HANDLE TO UNIQUEID EJB
    }

    /**
    * Returns a unique ID to the caller. This is synchronized to prevent
    * multiple callers from getting the same ID back if they called getNextID() at the
    * same time. 
    * 
    *
    * @return long 		next ID
    */
    public synchronized long getNextID() throws Exception{
        currentID++;
	if (currentID > max) {
	    try{
		IDSet set = uniqueIdEJB.getNextIDSet();
				
		// reset current id
		currentID = set.getMin();
		max = set.getMax();
	     }
	    catch(Exception ex) {
		ex.printStackTrace();
		throw ex;
	    }
      	}
	return currentID;
    }

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.