private void reseed ( Connection conn ) throws SQLException { logger . debug ( "enter - reseed()" ) ; try { PreparedStatement stmt = null ; ResultSet rs = null ; try { // Keep in this loop as long as we encounter concurrency errors do { stmt = conn . prepareStatement ( FIND_SEQ ) ; stmt . setString ( SEL_NAME , getName ( ) ) ; rs = stmt . executeQuery ( ) ; if ( ! rs . next ( ) ) { logger . info ( "No sequence in DB for " + getName ( ) + "." ) ; // no such sequence, create it { // close resources try { rs . close ( ) ; } catch ( SQLException ignore ) { /* ignore */ } rs = null ; try { stmt . close ( ) ; } catch ( SQLException ignore ) { /* ignore */ } stmt = null ; } sequence = 100L ; nextKey = sequence + interval ; create ( conn ) ; } else { long ts ; sequence = rs . getLong ( SEL_NEXT_KEY ) ; interval = rs . getLong ( SEL_INTERVAL ) ; if ( interval < 1 ) { interval = defaultInterval ; } nextKey = sequence + interval ; ts = rs . getLong ( SEL_UPDATE ) ; { // close resources try { rs . close ( ) ; } catch ( SQLException ignore ) { /* ignore */ } rs = null ; try { stmt . close ( ) ; } catch ( SQLException ignore ) { /* ignore */ } stmt = null ; } // increment the seed in the database stmt = conn . prepareStatement ( UPDATE_SEQ ) ; stmt . setLong ( UPD_NEXT_KEY , nextKey ) ; stmt . setLong ( UPD_SET_UPDATE , System . currentTimeMillis ( ) ) ; stmt . setString ( UPD_NAME , getName ( ) ) ; stmt . setLong ( UPD_WHERE_KEY , sequence ) ; stmt . setLong ( UPD_WHERE_UPDATE , ts ) ; if ( stmt . executeUpdate ( ) != 1 ) { // someone changed the database! try again! sequence = - 1L ; logger . warn ( "Concurrency error, requerying DB." ) ; } else { if ( ! conn . getAutoCommit ( ) ) { conn . commit ( ) ; } } } } while ( sequence == - 1L ) ; logger . info ( "Sequence set to " + sequence + ", next_key is " + nextKey + "." ) ; } finally { if ( rs != null ) { try { rs . close ( ) ; } catch ( SQLException ignore ) { /* ignore */ } } if ( stmt != null ) { try { stmt . close ( ) ; } catch ( SQLException ignore ) { /* ignore */ } } } } finally { logger . debug ( "exit - reseed()" ) ; } }
Gets the next seed from the database for this sequence .