@@ -149,9 +149,9 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
149149 }
150150
151151 BigQueryConnection (String url , DataSource ds ) throws IOException {
152- try {
153- this . connectionId = String . valueOf ( connectionIdCounter . getAndIncrement ());
154- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
152+ this . connectionId = String . valueOf ( connectionIdCounter . getAndIncrement ());
153+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
154+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
155155 LOG .finest ("++enter++" );
156156
157157 this .connectionUrl = url ;
@@ -265,9 +265,6 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
265265
266266 this .headerProvider = createHeaderProvider ();
267267 this .bigQuery = getBigQueryConnection ();
268- } finally {
269- BigQueryJdbcMdc .clear ();
270- ;
271268 }
272269 }
273270
@@ -351,16 +348,14 @@ String getConnectionId() {
351348 */
352349 @ Override
353350 public Statement createStatement () throws SQLException {
354- try {
355- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
351+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
352+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
356353 LOG .finest ("++enter++" );
357354 checkClosed ();
358355 BigQueryStatement currentStatement = new BigQueryStatement (this );
359356 LOG .fine ("Statement %s created." , currentStatement );
360357 addOpenStatements (currentStatement );
361358 return currentStatement ;
362- } finally {
363- BigQueryJdbcMdc .clear ();
364359 }
365360 }
366361
@@ -380,8 +375,8 @@ public Statement createStatement() throws SQLException {
380375 @ Override
381376 public Statement createStatement (int resultSetType , int resultSetConcurrency )
382377 throws SQLException {
383- try {
384- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
378+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
379+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
385380 LOG .finest ("++enter++" );
386381 checkClosed ();
387382 if (resultSetType != ResultSet .TYPE_FORWARD_ONLY
@@ -390,8 +385,6 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency)
390385 "Unsupported createStatement feature." );
391386 }
392387 return createStatement ();
393- } finally {
394- BigQueryJdbcMdc .clear ();
395388 }
396389 }
397390
@@ -411,8 +404,8 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency)
411404 @ Override
412405 public Statement createStatement (
413406 int resultSetType , int resultSetConcurrency , int resultSetHoldability ) throws SQLException {
414- try {
415- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
407+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
408+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
416409 LOG .finest ("++enter++" );
417410 checkClosed ();
418411 if (resultSetType != ResultSet .TYPE_FORWARD_ONLY
@@ -422,37 +415,31 @@ public Statement createStatement(
422415 "Unsupported createStatement feature" );
423416 }
424417 return createStatement ();
425- } finally {
426- BigQueryJdbcMdc .clear ();
427418 }
428419 }
429420
430421 @ Override
431422 public PreparedStatement prepareStatement (String sql ) throws SQLException {
432- try {
433- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
423+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
424+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
434425 LOG .finest ("++enter++" );
435426 checkClosed ();
436427 PreparedStatement currentStatement = new BigQueryPreparedStatement (this , sql );
437428 LOG .fine ("Prepared Statement %s created." , currentStatement );
438429 addOpenStatements (currentStatement );
439430 return currentStatement ;
440- } finally {
441- BigQueryJdbcMdc .clear ();
442431 }
443432 }
444433
445434 @ Override
446435 public PreparedStatement prepareStatement (String sql , int autoGeneratedKeys ) throws SQLException {
447- try {
448- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
436+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
437+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
449438 LOG .finest ("++enter++" );
450439 if (autoGeneratedKeys != Statement .NO_GENERATED_KEYS ) {
451440 throw new BigQueryJdbcSqlFeatureNotSupportedException ("autoGeneratedKeys is not supported" );
452441 }
453442 return prepareStatement (sql );
454- } finally {
455- BigQueryJdbcMdc .clear ();
456443 }
457444 }
458445
@@ -465,8 +452,8 @@ public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throw
465452 public PreparedStatement prepareStatement (
466453 String sql , int resultSetType , int resultSetConcurrency , int resultSetHoldability )
467454 throws SQLException {
468- try {
469- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
455+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
456+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
470457 LOG .finest ("++enter++" );
471458 if (resultSetType != ResultSet .TYPE_FORWARD_ONLY
472459 || resultSetConcurrency != ResultSet .CONCUR_READ_ONLY
@@ -475,25 +462,21 @@ public PreparedStatement prepareStatement(
475462 "Unsupported prepareStatement feature" );
476463 }
477464 return prepareStatement (sql );
478- } finally {
479- BigQueryJdbcMdc .clear ();
480465 }
481466 }
482467
483468 @ Override
484469 public PreparedStatement prepareStatement (String sql , int resultSetType , int resultSetConcurrency )
485470 throws SQLException {
486- try {
487- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
471+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
472+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
488473 LOG .finest ("++enter++" );
489474 if (resultSetType != ResultSet .TYPE_FORWARD_ONLY
490475 || resultSetConcurrency != ResultSet .CONCUR_READ_ONLY ) {
491476 throw new BigQueryJdbcSqlFeatureNotSupportedException (
492477 "Unsupported prepareStatement feature" );
493478 }
494479 return prepareStatement (sql );
495- } finally {
496- BigQueryJdbcMdc .clear ();
497480 }
498481 }
499482
@@ -709,8 +692,8 @@ Long getListenerPoolSize() {
709692
710693 @ Override
711694 public boolean isValid (int timeout ) throws SQLException {
712- try {
713- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
695+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
696+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
714697 LOG .finest ("++enter++" );
715698 if (timeout < 0 ) {
716699 throw new BigQueryJdbcException ("timeout must be >= 0" );
@@ -729,19 +712,15 @@ public boolean isValid(int timeout) throws SQLException {
729712 }
730713 }
731714 return false ;
732- } finally {
733- BigQueryJdbcMdc .clear ();
734715 }
735716 }
736717
737718 @ Override
738719 public void abort (Executor executor ) throws SQLException {
739- try {
740- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
720+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
721+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
741722 LOG .finest ("++enter++" );
742723 close ();
743- } finally {
744- BigQueryJdbcMdc .clear ();
745724 }
746725 }
747726
@@ -782,20 +761,18 @@ public void clearWarnings() {
782761
783762 @ Override
784763 public boolean getAutoCommit () {
785- try {
786- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
764+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
765+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
787766 LOG .finest ("++enter++" );
788767 checkClosed ();
789768 return this .autoCommit ;
790- } finally {
791- BigQueryJdbcMdc .clear ();
792769 }
793770 }
794771
795772 @ Override
796773 public void setAutoCommit (boolean autoCommit ) throws SQLException {
797- try {
798- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
774+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
775+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
799776 LOG .finest ("++enter++" );
800777 checkClosed ();
801778 checkIfEnabledSession ("setAutoCommit" );
@@ -811,15 +788,13 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
811788 if (!this .autoCommit ) {
812789 beginTransaction ();
813790 }
814- } finally {
815- BigQueryJdbcMdc .clear ();
816791 }
817792 }
818793
819794 @ Override
820795 public void commit () {
821- try {
822- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
796+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
797+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
823798 LOG .finest ("++enter++" );
824799 checkClosed ();
825800 checkIfEnabledSession ("commit" );
@@ -832,15 +807,13 @@ public void commit() {
832807 if (!getAutoCommit ()) {
833808 beginTransaction ();
834809 }
835- } finally {
836- BigQueryJdbcMdc .clear ();
837810 }
838811 }
839812
840813 @ Override
841814 public void rollback () throws SQLException {
842- try {
843- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
815+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
816+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
844817 LOG .finest ("++enter++" );
845818 checkClosed ();
846819 checkIfEnabledSession ("rollback" );
@@ -864,22 +837,18 @@ public void rollback() throws SQLException {
864837 LOG .severe (ex , "Failed to rollback transaction" );
865838 throw new BigQueryJdbcException (ex );
866839 }
867- } finally {
868- BigQueryJdbcMdc .clear ();
869840 }
870841 }
871842
872843 @ Override
873844 public DatabaseMetaData getMetaData () throws SQLException {
874- try {
875- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
845+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
846+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
876847 LOG .finest ("++enter++" );
877848 if (databaseMetaData == null ) {
878849 databaseMetaData = new BigQueryDatabaseMetaData (this );
879850 }
880851 return databaseMetaData ;
881- } finally {
882- BigQueryJdbcMdc .clear ();
883852 }
884853 }
885854
@@ -890,16 +859,14 @@ public int getTransactionIsolation() {
890859
891860 @ Override
892861 public void setTransactionIsolation (int level ) throws SQLException {
893- try {
894- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
862+ try ( BigQueryJdbcMdc . MdcCloseable mdc =
863+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
895864 LOG .finest ("++enter++" );
896865 if (level != Connection .TRANSACTION_SERIALIZABLE ) {
897866 throw new BigQueryJdbcSqlFeatureNotSupportedException (
898867 "Unsupported transaction isolation level" );
899868 }
900869 this .transactionIsolation = level ;
901- } finally {
902- BigQueryJdbcMdc .clear ();
903870 }
904871 }
905872
@@ -926,12 +893,12 @@ public void setHoldability(int holdability) throws SQLException {
926893 */
927894 @ Override
928895 public void close () throws SQLException {
929- try {
930- if (isClosed ()) {
931- return ;
932- }
896+ if (isClosed ()) {
897+ return ;
898+ }
933899
934- BigQueryJdbcMdc .registerInstance (this , this .connectionId );
900+ try (BigQueryJdbcMdc .MdcCloseable mdc =
901+ BigQueryJdbcMdc .registerInstance (this , this .connectionId )) {
935902 LOG .finest ("++enter++" );
936903 LOG .fine ("Closing Connection " + this );
937904
@@ -963,8 +930,6 @@ public void close() throws SQLException {
963930 BigQueryJdbcRootLogger .closeConnectionHandler (this .connectionId );
964931 }
965932 this .isClosed = true ;
966- } finally {
967- BigQueryJdbcMdc .clear ();
968933 }
969934 }
970935
0 commit comments