FileDocCategorySizeDatePackage
SequenceBlock.javaAPI DocExample1645Mon Nov 24 09:16:40 GMT 2003com.oreilly.patterns.chapter8

SequenceBlock

public class SequenceBlock extends Object

Fields Summary
private static int
BLOCK_SIZE
private static long
current
private static long
getNextAt
Constructors Summary
Methods Summary
private static java.sql.ConnectiongetConnection()

    try {
      Context jndiContext = new InitialContext();
      DataSource ds =
 (DataSource)jndiContext.lookup("java:comp/env/jdbc/DataChapterDS");
      return ds.getConnection();
    } catch (NamingException ne) {
        throw new SQLException (ne.getMessage());
    }
  
public static synchronized longgetNextId()

  
  
       
    if((current > -1) && (current < getNextAt))
      return current++;

    // We need to retrieve another block from the database
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      con = getConnection();
      stmt = con.createStatement();
      // Oracle specific
      rs = stmt.executeQuery("SELECT SEQ_PK.NEXTVAL FROM DUAL"); 
      rs.next(); // Exception handler will kick in on failure
      long seqVal = rs.getLong(1);
      current = seqVal * BLOCK_SIZE;
      getNextAt = current + BLOCK_SIZE;
      return current++;
    } catch (SQLException e) {
      throw new IllegalStateException("Unable to access key store");
    } finally {
      if(rs != null) try { rs.close(); } catch (SQLException e) {}
      if(stmt != null) try { stmt.close(); } catch (SQLException e) {}
      if(con != null) try { con.close(); } catch (SQLException e) {}
    }