Skip to content

Commit 85f1a29

Browse files
committed
only connection extracts connectionID
1 parent 8299c29 commit 85f1a29

4 files changed

Lines changed: 34 additions & 43 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,12 @@ public void close() {
109109
}
110110

111111
protected SQLException logAndCreateException(SQLException ex) {
112-
if (this.statement != null) {
113-
try (BigQueryJdbcMdc.MdcCloseable mdc =
114-
BigQueryJdbcMdc.registerInstance(this.statement.connectionId)) {
115-
LOG.severe(ex.getMessage(), ex);
116-
}
112+
if (this.statement == null) {
113+
return ex;
114+
}
115+
try (BigQueryJdbcMdc.MdcCloseable mdc =
116+
BigQueryJdbcMdc.registerInstance(this.statement.connectionId)) {
117+
LOG.severe(ex.getMessage(), ex);
117118
}
118119
return ex;
119120
}
@@ -209,7 +210,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
209210
metaData = BigQueryResultSetMetadata.of(this.schema.getFields(), this.statement);
210211
}
211212
}
212-
return BigQueryJdbcContextProxy.wrap(metaData, ResultSetMetaData.class);
213+
return BigQueryJdbcContextProxy.wrap(metaData, ResultSetMetaData.class, connectionId);
213214
}
214215

215216
@Override

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

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private BigQueryJdbcContextProxy(Object target, String connectionId, Class<?> in
4141
this.interfaceType = interfaceType;
4242
}
4343

44-
/** Wraps a target JDBC object with a dynamic proxy carrying the connection context. */
44+
/** Wraps a target Connection JDBC object, auto-extracting its connection ID. */
4545
@SuppressWarnings("unchecked")
4646
static <T> T wrap(Object target, Class<T> interfaceType) {
4747
if (target == null) {
@@ -55,38 +55,26 @@ static <T> T wrap(Object target, Class<T> interfaceType) {
5555
new BigQueryJdbcContextProxy(target, connectionId, interfaceType));
5656
}
5757

58+
/** Wraps a target child JDBC object, propagating the connection ID parameter directly. */
59+
@SuppressWarnings("unchecked")
60+
static <T> T wrap(Object target, Class<T> interfaceType, String connectionId) {
61+
if (target == null) {
62+
return null;
63+
}
64+
return (T)
65+
Proxy.newProxyInstance(
66+
interfaceType.getClassLoader(),
67+
new Class<?>[] {interfaceType},
68+
new BigQueryJdbcContextProxy(target, connectionId, interfaceType));
69+
}
70+
5871
private static String extractConnectionId(Object target) {
5972
if (target == null) {
6073
return null;
6174
}
6275
if (target instanceof BigQueryConnection) {
6376
return ((BigQueryConnection) target).getConnectionId();
6477
}
65-
if (target instanceof BigQueryStatement) {
66-
return ((BigQueryStatement) target).connectionId;
67-
}
68-
if (target instanceof BigQueryDatabaseMetaData) {
69-
return ((BigQueryDatabaseMetaData) target).connection.getConnectionId();
70-
}
71-
if (target instanceof BigQueryResultSetMetadata) {
72-
java.sql.Statement stmt = ((BigQueryResultSetMetadata) target).getStatement();
73-
if (stmt != null) {
74-
if (Proxy.isProxyClass(stmt.getClass())) {
75-
InvocationHandler handler = Proxy.getInvocationHandler(stmt);
76-
if (handler instanceof BigQueryJdbcContextProxy) {
77-
return ((BigQueryJdbcContextProxy) handler).connectionId;
78-
}
79-
}
80-
if (stmt instanceof BigQueryStatement) {
81-
return ((BigQueryStatement) stmt).connectionId;
82-
}
83-
}
84-
}
85-
// Fallback to thread active context
86-
String activeId = BigQueryJdbcMdc.getConnectionId();
87-
if (activeId != null) {
88-
return activeId;
89-
}
9078
return null;
9179
}
9280

@@ -146,17 +134,17 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
146134

147135
// Automatically cascade proxy wrappers to child JDBC objects returned from calls
148136
if (result instanceof java.sql.CallableStatement) {
149-
return wrap(result, java.sql.CallableStatement.class);
137+
return wrap(result, java.sql.CallableStatement.class, connectionId);
150138
} else if (result instanceof java.sql.PreparedStatement) {
151-
return wrap(result, java.sql.PreparedStatement.class);
139+
return wrap(result, java.sql.PreparedStatement.class, connectionId);
152140
} else if (result instanceof java.sql.Statement) {
153-
return wrap(result, java.sql.Statement.class);
141+
return wrap(result, java.sql.Statement.class, connectionId);
154142
} else if (result instanceof java.sql.DatabaseMetaData) {
155-
return wrap(result, java.sql.DatabaseMetaData.class);
143+
return wrap(result, java.sql.DatabaseMetaData.class, connectionId);
156144
} else if (result instanceof java.sql.ParameterMetaData) {
157-
return wrap(result, java.sql.ParameterMetaData.class);
145+
return wrap(result, java.sql.ParameterMetaData.class, connectionId);
158146
} else if (result instanceof java.sql.ResultSetMetaData) {
159-
return wrap(result, java.sql.ResultSetMetaData.class);
147+
return wrap(result, java.sql.ResultSetMetaData.class, connectionId);
160148
}
161149

162150
return result;

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcContextProxyTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void testExtractConnectionIdFromStatement() throws SQLException {
6565
BigQueryStatement stmt = new BigQueryStatement(mockConn);
6666
stmt.connectionId = "conn-uuid-456";
6767

68-
Statement proxy = BigQueryJdbcContextProxy.wrap(stmt, Statement.class);
68+
Statement proxy = BigQueryJdbcContextProxy.wrap(stmt, Statement.class, "conn-uuid-456");
6969
assertNotNull(proxy);
7070

7171
// We can call any statement method (like getUpdateCount) and verify context routing
@@ -83,7 +83,8 @@ public void testExtractConnectionIdFromDatabaseMetaData() throws SQLException {
8383

8484
BigQueryDatabaseMetaData meta = new BigQueryDatabaseMetaData(mockConn);
8585

86-
DatabaseMetaData proxy = BigQueryJdbcContextProxy.wrap(meta, DatabaseMetaData.class);
86+
DatabaseMetaData proxy =
87+
BigQueryJdbcContextProxy.wrap(meta, DatabaseMetaData.class, "conn-uuid-789");
8788
assertNotNull(proxy);
8889

8990
// Assert read-only capability does not leak thread context
@@ -100,7 +101,8 @@ public void testExtractConnectionIdFromResultSetMetaData() throws SQLException {
100101
FieldList fields = FieldList.of(Field.of("col", StandardSQLTypeName.STRING));
101102
BigQueryResultSetMetadata meta = BigQueryResultSetMetadata.of(fields, stmt);
102103

103-
ResultSetMetaData proxy = BigQueryJdbcContextProxy.wrap(meta, ResultSetMetaData.class);
104+
ResultSetMetaData proxy =
105+
BigQueryJdbcContextProxy.wrap(meta, ResultSetMetaData.class, "conn-uuid-999");
104106
assertNotNull(proxy);
105107

106108
assertEquals(1, proxy.getColumnCount());
@@ -113,7 +115,7 @@ public void testWrapWithNullContextAndExceptionThrown() throws SQLException {
113115
mockStmt.connectionId = null;
114116
when(mockStmt.executeQuery("SELECT *")).thenThrow(new SQLException("Database error"));
115117

116-
Statement proxy = BigQueryJdbcContextProxy.wrap(mockStmt, Statement.class);
118+
Statement proxy = BigQueryJdbcContextProxy.wrap(mockStmt, Statement.class, null);
117119
assertNotNull(proxy);
118120

119121
SQLException ex = assertThrows(SQLException.class, () -> proxy.executeQuery("SELECT *"));

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void testProxyExceptionLogRouting() throws Exception {
143143
.thenThrow(new SQLException("Database error"));
144144

145145
// Wrap it using our proxy (which dynamically extracts "c456" as its connection ID!)
146-
Statement proxy = BigQueryJdbcContextProxy.wrap(mockStmt, Statement.class);
146+
Statement proxy = BigQueryJdbcContextProxy.wrap(mockStmt, Statement.class, "c456");
147147
assertNotNull(proxy);
148148

149149
// Call the proxy method. It will throw SQLException

0 commit comments

Comments
 (0)