Skip to content

Commit 3a0f2ff

Browse files
committed
Refactor BigQueryConnection to generate a unique ID for each connection
1 parent e156cb4 commit 3a0f2ff

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.util.concurrent.ConcurrentHashMap;
6262
import java.util.concurrent.Executor;
6363
import java.util.concurrent.TimeUnit;
64+
import java.util.concurrent.atomic.AtomicLong;
6465

6566
/**
6667
* An implementation of {@link java.sql.Connection} for establishing a connection with BigQuery and
@@ -72,6 +73,8 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
7273

7374
private final BigQueryJdbcCustomLogger LOG = new BigQueryJdbcCustomLogger(this.toString());
7475
String connectionClassName = this.toString();
76+
private final String connectionId;
77+
private static final AtomicLong connectionIdCounter = new AtomicLong(1);
7578
private static final String DEFAULT_JDBC_TOKEN_VALUE = "Google-BigQuery-JDBC-Driver";
7679
private static final String DEFAULT_VERSION = "0.0.0";
7780
private HeaderProvider headerProvider;
@@ -146,6 +149,8 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
146149
}
147150

148151
BigQueryConnection(String url, DataSource ds) throws IOException {
152+
this.connectionId = String.valueOf(connectionIdCounter.getAndIncrement());
153+
BigQueryJdbcMdc.registerInstance(this, this.connectionId);
149154
this.connectionUrl = url;
150155
this.openStatements = ConcurrentHashMap.newKeySet();
151156
this.autoCommit = true;
@@ -323,6 +328,10 @@ String getConnectionUrl() {
323328
return connectionUrl;
324329
}
325330

331+
String getConnectionId() {
332+
return this.connectionId;
333+
}
334+
326335
/**
327336
* Creates and returns a new {@code Statement} object for executing BigQuery SQL queries
328337
*
@@ -834,6 +843,7 @@ public void close() throws SQLException {
834843
if (isClosed()) {
835844
return;
836845
}
846+
837847
try {
838848
if (this.bigQueryReadClient != null) {
839849
this.bigQueryReadClient.shutdown();
@@ -857,6 +867,9 @@ public void close() throws SQLException {
857867
} catch (InterruptedException e) {
858868
LOG.severe(e, "Interrupted during close");
859869
throw new BigQueryJdbcRuntimeException(e);
870+
} finally{
871+
BigQueryJdbcMdc.removeInstance(this);
872+
BigQueryJdbcRootLogger.closeConnectionHandler(this.connectionId);
860873
}
861874
this.isClosed = true;
862875
}

0 commit comments

Comments
 (0)