Buggy code:
@ Log class ReplicateSweep implements Runnable { private final DataSource datasource ; private static ReplicationPolicy replicatePolicy = ReplicationPolicy . firstSite ; public ReplicateSweep ( DataSource datasource ) { } private Collection < StorageSite > availableStorage = null ; private Iterator < StorageSite > it = null ; private StorageSite findBestSite ( ) { } private Collection < StorageSite > findBestSites ( ) { } private Collection < StorageSite > getStorageSites ( Connection connection ) throws SQLException { } class CacheDescr { Long pdriId ; String name ; Long pdriGroupRef ; } private void onCacheReplicate ( ReplicateSweep . CacheDescr cd , CachePDRI cpdri , Connection connection ) throws IOException , SQLException { } private void onCacheReplicate ( PDRIDescr cd , PDRI cpdri , Connection connection ) throws IOException , SQLException { } @ Override public void run ( ) { String name ; long ssID ; String resourceURL ; String username ; String password ; boolean encrypt ; BigInteger key ; long keyLong ; PDRI source ; long pdriGroupRef ; long pdriId ; try ( Connection connection = datasource . getConnection ( ) ) { try { connection . setAutoCommit ( false ) ; availableStorage = getStorageSites ( connection ) ; ArrayList < PDRIDescr > toReplicate = new ArrayList < > ( ) ; try ( Statement statement = connection . createStatement ( ) ) { <START_BUG> String sql = "SELECT<seq2seq4repair_space>fileName,<seq2seq4repair_space>storageSiteId,<seq2seq4repair_space>storage_site_table.resourceUri,<seq2seq4repair_space>username,<seq2seq4repair_space>password,<seq2seq4repair_space>encrypt,<seq2seq4repair_space>encryptionKey,<seq2seq4repair_space>pdri_table.pdriGroupRef,<seq2seq4repair_space>" + ( ( ( ( ( ( "pdri_table.pdriId<seq2seq4repair_space>" + "FROM<seq2seq4repair_space>pdri_table<seq2seq4repair_space>JOIN<seq2seq4repair_space>(SELECT<seq2seq4repair_space>pdriGroupRef,<seq2seq4repair_space>count(pdri_table.storageSiteRef)<seq2seq4repair_space>AS<seq2seq4repair_space>refcnt<seq2seq4repair_space>" ) + "FROM<seq2seq4repair_space>pdri_table<seq2seq4repair_space>GROUP<seq2seq4repair_space>BY<seq2seq4repair_space>pdriGroupRef)<seq2seq4repair_space>AS<seq2seq4repair_space>t<seq2seq4repair_space>ON<seq2seq4repair_space>pdri_table.pdriGroupRef<seq2seq4repair_space>=<seq2seq4repair_space>t.pdriGroupRef<seq2seq4repair_space>" ) + "JOIN<seq2seq4repair_space>storage_site_table<seq2seq4repair_space>ON<seq2seq4repair_space>pdri_table.storageSiteRef<seq2seq4repair_space>=<seq2seq4repair_space>storage_site_table.storageSiteId<seq2seq4repair_space>" ) + "JOIN<seq2seq4repair_space>credential_table<seq2seq4repair_space>on<seq2seq4repair_space>credential_table.credintialId<seq2seq4repair_space>=<seq2seq4repair_space>storage_site_table.credentialRef<seq2seq4repair_space>" ) + "<seq2seq4repair_space>JOIN<seq2seq4repair_space>ldata_table<seq2seq4repair_space>on<seq2seq4repair_space>(ldata_table.pdriGroupRef<seq2seq4repair_space>=<seq2seq4repair_space>pdri_table.pdriGroupRef<seq2seq4repair_space>AND<seq2seq4repair_space>ldata_table.lockTokenId<seq2seq4repair_space>is<seq2seq4repair_space>NULL)" ) + "WHERE<seq2seq4repair_space>refcnt<seq2seq4repair_space>><seq2seq4repair_space>1<seq2seq4repair_space>AND<seq2seq4repair_space>isCache<seq2seq4repair_space>LIMIT<seq2seq4repair_space>100" ) ; <END_BUG> ResultSet rs = statement . executeQuery ( sql ) ; while ( rs . next ( ) ) { name = rs . getString ( 1 ) ; ssID = rs . getLong ( 2 ) ; resourceURL = rs . getString ( 3 ) ; username = rs . getString ( 4 ) ; password = rs . getString ( 5 ) ; encrypt = rs . getBoolean ( 6 ) ; keyLong = rs . getLong ( 7 ) ; key = BigInteger . valueOf ( keyLong ) ; pdriGroupRef = rs . getLong ( 8 ) ; pdriId = rs . getLong ( 9 ) ; PDRIDescr cd = new PDRIDescr ( name , ssID , resourceURL , username , password , encrypt , key , pdriGroupRef , pdriId ) ; toReplicate . add ( cd ) ; } } connection . commit ( ) ; for ( PDRIDescr cd : toReplicate ) { log . log ( Level . FINE , "to<seq2seq4repair_space>replicate:<seq2seq4repair_space>{0}" , cd . getResourceUrl ( ) ) ; try ( PreparedStatement preparedStatement = connection . prepareStatement ( ( "INSERT<seq2seq4repair_space>INTO<seq2seq4repair_space>pdri_table<seq2seq4repair_space>" + "(fileName,<seq2seq4repair_space>storageSiteRef,<seq2seq4repair_space>pdriGroupRef,isEncrypted,<seq2seq4repair_space>encryptionKey)<seq2seq4repair_space>VALUES(?,<seq2seq4repair_space>?,<seq2seq4repair_space>?,<seq2seq4repair_space>?,<seq2seq4repair_space>?)" ) ) ) { source = new PDRIFactory ( ) . createInstance ( cd , false ) ; Collection < StorageSite > ss = findBestSites ( ) ; boolean failed = false ; for ( StorageSite s : ss ) { BigInteger pdriKey = DesEncrypter . generateKey ( ) ; PDRIDescr pdriDescr = new PDRIDescr ( cd . getName ( ) , s . getStorageSiteId ( ) , s . getResourceURI ( ) , s . getCredential ( ) . getStorageSiteUsername ( ) , s . getCredential ( ) . getStorageSitePassword ( ) , s . isEncrypt ( ) , pdriKey , cd . getPdriGroupRef ( ) , null ) ; PDRI replica = PDRIFactory . getFactory ( ) . createInstance ( pdriDescr , false ) ; try { if ( ! ( replica . exists ( source . getFileName ( ) ) ) ) { replica . replicate ( source ) ; } try ( Statement s2 = connection . createStatement ( ) ) { ResultSet rs = s2 . executeQuery ( ( ( ( ( ( ( ( ( ( ( "select<seq2seq4repair_space>*<seq2seq4repair_space>from<seq2seq4repair_space>pdri_table<seq2seq4repair_space>WHERE<seq2seq4repair_space>" + "(fileName<seq2seq4repair_space>LIKE<seq2seq4repair_space>'" ) + ( cd . getName ( ) ) ) + "'" ) + "<seq2seq4repair_space>AND<seq2seq4repair_space>storageSiteRef<seq2seq4repair_space>=<seq2seq4repair_space>" ) + ( s . getStorageSiteId ( ) ) ) + "<seq2seq4repair_space>AND<seq2seq4repair_space>pdriGroupRef<seq2seq4repair_space>=<seq2seq4repair_space>" ) + ( cd . getPdriGroupRef ( ) ) ) + "<seq2seq4repair_space>AND<seq2seq4repair_space>isEncrypted<seq2seq4repair_space>=<seq2seq4repair_space>" ) + ( replica . getEncrypted ( ) ) ) + ")" ) ) ; if ( ! ( rs . next ( ) ) ) { preparedStatement . setString ( 1 , cd . getName ( ) ) ; preparedStatement . setLong ( 2 , s . getStorageSiteId ( ) ) ; preparedStatement . setLong ( 3 , cd . getPdriGroupRef ( ) ) ; preparedStatement . setBoolean ( 4 , replica . getEncrypted ( ) ) ; preparedStatement . setLong ( 5 , pdriKey . longValue ( ) ) ; preparedStatement . executeUpdate ( ) ; } } } catch ( IOException ex ) { failed = true ; continue ; } } if ( ! failed ) { onCacheReplicate ( cd , source , connection ) ; } connection . commit ( ) ; } catch ( NoSuchAlgorithmException ex ) { Logger . getLogger ( ReplicateSweep . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; } } } catch ( SQLException | IOException e ) { log . log ( Level . SEVERE , null , e ) ; connection . rollback ( ) ; } } catch ( SQLException e ) { log . log ( Level . SEVERE , null , e ) ; } } }

Fix:
String sql = "SELECT<seq2seq4repair_space>fileName,<seq2seq4repair_space>storageSiteId,<seq2seq4repair_space>storage_site_table.resourceUri,<seq2seq4repair_space>username,<seq2seq4repair_space>password,<seq2seq4repair_space>encrypt,<seq2seq4repair_space>encryptionKey,<seq2seq4repair_space>pdri_table.pdriGroupRef,<seq2seq4repair_space>" + ( ( ( ( ( ( "pdri_table.pdriId<seq2seq4repair_space>" + "FROM<seq2seq4repair_space>pdri_table<seq2seq4repair_space>JOIN<seq2seq4repair_space>(SELECT<seq2seq4repair_space>pdriGroupRef,<seq2seq4repair_space>count(pdri_table.storageSiteRef)<seq2seq4repair_space>AS<seq2seq4repair_space>refcnt<seq2seq4repair_space>" ) + "FROM<seq2seq4repair_space>pdri_table<seq2seq4repair_space>GROUP<seq2seq4repair_space>BY<seq2seq4repair_space>pdriGroupRef)<seq2seq4repair_space>AS<seq2seq4repair_space>t<seq2seq4repair_space>ON<seq2seq4repair_space>pdri_table.pdriGroupRef<seq2seq4repair_space>=<seq2seq4repair_space>t.pdriGroupRef<seq2seq4repair_space>" ) + "JOIN<seq2seq4repair_space>storage_site_table<seq2seq4repair_space>ON<seq2seq4repair_space>pdri_table.storageSiteRef<seq2seq4repair_space>=<seq2seq4repair_space>storage_site_table.storageSiteId<seq2seq4repair_space>" ) + "JOIN<seq2seq4repair_space>credential_table<seq2seq4repair_space>on<seq2seq4repair_space>credential_table.credintialId<seq2seq4repair_space>=<seq2seq4repair_space>storage_site_table.credentialRef<seq2seq4repair_space>" ) + "<seq2seq4repair_space>JOIN<seq2seq4repair_space>ldata_table<seq2seq4repair_space>on<seq2seq4repair_space>(ldata_table.pdriGroupRef<seq2seq4repair_space>=<seq2seq4repair_space>pdri_table.pdriGroupRef<seq2seq4repair_space>AND<seq2seq4repair_space>ldata_table.lockTokenId<seq2seq4repair_space>is<seq2seq4repair_space>NULL)" ) + "WHERE<seq2seq4repair_space>refcnt<seq2seq4repair_space>>=<seq2seq4repair_space>1<seq2seq4repair_space>AND<seq2seq4repair_space>isCache<seq2seq4repair_space>LIMIT<seq2seq4repair_space>100" ) ;