@@ -201,76 +201,72 @@ private void fillBank(long stagger) {
201201 try {
202202 beganTransaction = TransactionUtil .begin ();
203203
204- Connection connection = null ;
205- Statement stmt = null ;
206- ResultSet rs = null ;
207-
208- try {
209- connection = TransactionFactoryLoader .getInstance ().getConnection (SequenceUtil .this .helperInfo );
210- } catch (SQLException sqle ) {
211- Debug .logWarning ("Unable to establish a connection with the database. Error was:" + sqle .toString (), MODULE );
212- throw sqle ;
213- } catch (GenericEntityException e ) {
214- Debug .logWarning ("Unable to establish a connection with the database. Error was: " + e .toString (), MODULE );
215- throw e ;
216- }
217- if (connection == null ) {
218- throw new GenericEntityException ("Unable to establish a connection with the database, connection was null..." );
219- }
204+ boolean committed = false ;
205+ boolean connectionAvailable = false ;
206+ try (Connection connection = TransactionFactoryLoader .getInstance ().getConnection (SequenceUtil .this .helperInfo )) {
207+ if (connection == null ) {
208+ throw new GenericEntityException ("Unable to establish a connection with the database, connection was null..." );
209+ }
210+ connectionAvailable = true ;
211+
212+ try (Statement stmt = connection .createStatement ()) {
213+ String sql = null ;
214+ // 1 - run an update with no changes to get a lock on the record
215+ if (stmt .executeUpdate (updateForLockStatement ) <= 0 ) {
216+ Debug .logWarning ("Lock failed; no sequence row was found, will try to add a new one for sequence: " + seqName ,
217+ MODULE );
218+ sql = "INSERT INTO " + SequenceUtil .this .tableName + " (" + SequenceUtil .this .nameColName + ", "
219+ + SequenceUtil .this .idColName + ") VALUES ('" + this .seqName + "', " + START_SEQ_ID + ")" ;
220+ try {
221+ stmt .executeUpdate (sql );
222+ } catch (SQLException sqle ) {
223+ // insert failed: this means that another thread inserted the record;
224+ // then retry to run an update with no changes to
225+ // get a lock on the record
226+ if (stmt .executeUpdate (updateForLockStatement ) <= 0 ) {
227+ // This should never happen
228+ throw new GenericEntityException ("No rows changed when trying insert new sequence: " + seqName );
229+ }
220230
221- try {
222- stmt = connection .createStatement ();
223- String sql = null ;
224- // 1 - run an update with no changes to get a lock on the record
225- if (stmt .executeUpdate (updateForLockStatement ) <= 0 ) {
226- Debug .logWarning ("Lock failed; no sequence row was found, will try to add a new one for sequence: " + seqName , MODULE );
227- sql = "INSERT INTO " + SequenceUtil .this .tableName + " (" + SequenceUtil .this .nameColName + ", "
228- + SequenceUtil .this .idColName + ") VALUES ('" + this .seqName + "', " + START_SEQ_ID + ")" ;
229- try {
230- stmt .executeUpdate (sql );
231- } catch (SQLException sqle ) {
232- // insert failed: this means that another thread inserted the record; then retry to run an update with no changes to
233- // get a lock on the record
234- if (stmt .executeUpdate (updateForLockStatement ) <= 0 ) {
235- // This should never happen
236- throw new GenericEntityException ("No rows changed when trying insert new sequence: " + seqName );
237231 }
238-
239232 }
240- }
241- // 2 - select the record (now locked) to get the curSeqId
242- rs = stmt .executeQuery (selectSequenceStatement );
243- boolean sequenceFound = rs .next ();
244- if (sequenceFound ) {
245- curSeqId = rs .getLong (SequenceUtil .this .idColName );
246- }
247- rs . close ();
248- if (!sequenceFound ) {
249- throw new GenericEntityException ("Failed to find the sequence record for sequence: " + seqName );
250- }
251- // 3 - increment the sequence
252- sql = "UPDATE " + SequenceUtil .this .tableName + " SET " + SequenceUtil .this .idColName + "=" + SequenceUtil .this .idColName
253- + "+" + bankSize + " WHERE " + SequenceUtil .this .nameColName + "='" + this .seqName + "'" ;
254- if (stmt .executeUpdate (sql ) <= 0 ) {
255- throw new GenericEntityException ("Update failed, no rows changes for seqName: " + seqName );
256- }
233+ // 2 - select the record (now locked) to get the curSeqId
234+ boolean sequenceFound ;
235+ try ( ResultSet rs = stmt .executeQuery (selectSequenceStatement )) {
236+ sequenceFound = rs .next ();
237+ if (sequenceFound ) {
238+ curSeqId = rs .getLong (SequenceUtil .this .idColName );
239+ }
240+ }
241+ if (!sequenceFound ) {
242+ throw new GenericEntityException ("Failed to find the sequence record for sequence: " + seqName );
243+ }
244+ // 3 - increment the sequence
245+ sql = "UPDATE " + SequenceUtil .this .tableName + " SET " + SequenceUtil .this .idColName + "=" + SequenceUtil .this .idColName
246+ + "+" + bankSize + " WHERE " + SequenceUtil .this .nameColName + "='" + this .seqName + "'" ;
247+ if (stmt .executeUpdate (sql ) <= 0 ) {
248+ throw new GenericEntityException ("Update failed, no rows changes for seqName: " + seqName );
249+ }
257250
258- TransactionUtil .commit (beganTransaction );
251+ TransactionUtil .commit (beganTransaction );
252+ committed = true ;
253+ }
259254
260255 } catch (SQLException sqle ) {
261- Debug .logWarning (sqle , "SQL Exception:" + sqle .getMessage (), MODULE );
262- throw sqle ;
263- } finally {
264- try {
265- if (stmt != null ) stmt .close ();
266- } catch (SQLException sqle ) {
267- Debug .logWarning (sqle , "Error closing statement in sequence util" , MODULE );
256+ if (committed ) {
257+ Debug .logWarning (sqle , "Error closing database resources in sequence util" , MODULE );
258+ } else if (!connectionAvailable ) {
259+ Debug .logWarning ("Unable to establish a connection with the database. Error was:" + sqle .toString (), MODULE );
260+ throw sqle ;
261+ } else {
262+ Debug .logWarning (sqle , "SQL Exception:" + sqle .getMessage (), MODULE );
263+ throw sqle ;
268264 }
269- try {
270- connection .close ();
271- } catch (SQLException sqle ) {
272- Debug .logWarning (sqle , "Error closing connection in sequence util" , MODULE );
265+ } catch (GenericEntityException e ) {
266+ if (!connectionAvailable ) {
267+ Debug .logWarning ("Unable to establish a connection with the database. Error was: " + e .toString (), MODULE );
273268 }
269+ throw e ;
274270 }
275271 } catch (SQLException | GenericEntityException e ) {
276272 // reset the sequence fields and return (note: it would be better to throw an exception)
@@ -313,4 +309,3 @@ private void fillBank(long stagger) {
313309 }
314310 }
315311}
316-
0 commit comments