Skip to content

Commit 5bd4520

Browse files
authored
fix(bigquery-jdbc): abort session when connection is closed (#14273)
Fixes #13922
1 parent 4b6a671 commit 5bd4520

9 files changed

Lines changed: 458 additions & 151 deletions

File tree

‎java-bigquery-jdbc/docs/USER_GUIDE.md‎

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ This guide provides comprehensive instructions for configuring, developing with,
1414
4. [Connection Properties Reference](#4-connection-properties-reference)
1515
5. [Data Type Mapping Reference](#5-data-type-mapping-reference)
1616
6. [JDBC Driver Architecture & Core Features](#6-jdbc-driver-architecture--core-features)
17-
- [Transaction Management & Multi-Statement Sessions](#transaction-management--multi-statement-sessions)
17+
- [Multi-Statement Sessions & Transaction Management](#multi-statement-sessions--transaction-management)
1818
- [High-Throughput Storage Read & Write APIs](#high-throughput-storage-read--write-apis)
1919
7. [Feature Examples & Code Snippets](#7-feature-examples--code-snippets)
2020
- [Transactions (Manual Commit & Rollback)](#transactions-manual-commit--rollback)
21+
- [Connecting to a Pre-Existing Session](#connecting-to-a-pre-existing-session)
2122
- [Prepared Statements & Parameter Binding](#prepared-statements--parameter-binding)
2223
- [Callable Statements & Stored Procedures](#callable-statements--stored-procedures)
2324
- [Batch Ingestion with Storage Write API](#batch-ingestion-with-storage-write-api)
@@ -205,7 +206,8 @@ String url = "jdbc:bigquery://https://bigquery.googleapis.com:443"
205206

206207
| Property Name | Default Value | Description |
207208
| :--- | :---: | :--- |
208-
| `EnableSession` | `false` | Enables multi-statement session creation and transaction support (`BEGIN`, `COMMIT`, `ROLLBACK`). |
209+
| `EnableSession` | `false` | Enables BigQuery multi-statement session creation and transaction support (`BEGIN`, `COMMIT`, `ROLLBACK`). |
210+
| `QueryProperties` | `null` | Comma- or semicolon-separated key-value pairs passed as connection-level job properties (e.g., `QueryProperties=session_id=<session_id>` to connect to a pre-existing session). |
209211

210212
### Data Types & Extended Precision Properties
211213

@@ -313,58 +315,26 @@ When running queries through the JDBC driver for BigQuery, data types map as spe
313315
314316
## 6. JDBC Driver Architecture & Core Features
315317
316-
### Transaction Management & Multi-Statement Sessions
318+
### Multi-Statement Sessions & Transaction Management
317319
318-
BigQuery supports **Multi-Statement Transactions** across tables using standard SQL primitives (`BEGIN TRANSACTION`, `COMMIT TRANSACTION`, `ROLLBACK TRANSACTION`). The driver bridges standard JDBC methods (`setAutoCommit`, `commit`, `rollback`) directly to BigQuery's underlying session engine.
320+
BigQuery supports **Multi-Statement Sessions**, which preserve state across multiple SQL statements executed on the same connection.
319321
320-
#### Session Lifecycle Flow:
322+
1. **Enabling Sessions (`EnableSession=true`)**:
323+
- Add `;EnableSession=true` (or `EnableSession=1`) to the JDBC connection URL or DataSource properties.
324+
- Under default auto-commit mode (`autoCommit=true`), statements execute and commit individually while sharing session state.
321325
322-
```
323-
[DriverManager.getConnection()]
324-
│
325-
(EnableSession=true)
326-
│
327-
┌──────────▼──────────┐
328-
│ setAutoCommit(false)│ ──────► Begins transaction block in session
329-
└──────────┬──────────┘
330-
│
331-
┌──────────▼──────────┐
332-
│ Execute DML & SQL │ ──────► Runs queries within active session
333-
│ Statements │
334-
└──────────┬──────────┘
335-
│
336-
┌───────┴───────┐
337-
│ │
338-
▼ ▼
339-
┌─────────┐ ┌──────────┐
340-
│commit() │ │rollback()│
341-
└────┬────┘ └────┬─────┘
342-
│ │
343-
▼ ▼
344-
Executes: Executes:
345-
COMMIT ROLLBACK
346-
TRANSACTION; TRANSACTION;
347-
│ │
348-
└───────┬───────┘
349-
│
350-
▼
351-
(Auto-re-executes BEGIN TRANSACTION; if setAutoCommit remains false)
352-
```
326+
2. **Multi-Statement Transactions (`setAutoCommit(false)`)**:
327+
- Multi-statement transactions require `;EnableSession=true` (calling `setAutoCommit(false)`, `commit()`, or `rollback()` with sessions disabled throws an exception).
328+
- Calling `conn.setAutoCommit(false)` begins a multi-statement transaction in BigQuery. Statements executed within the transaction block remain uncommitted until `conn.commit()` is explicitly called (or discarded via `conn.rollback()`).
329+
- If `autoCommit` remains `false`, the driver automatically starts the next transaction block for subsequent statements.
330+
- Isolation level: `Connection.TRANSACTION_SERIALIZABLE` (BigQuery snapshot isolation).
353331
354-
1. **Pre-requisite Check**: Calling `setAutoCommit(false)`, `commit()`, or `rollback()` requires `;EnableSession=true` in the connection URL. If disabled or invoked without an active transaction, an exception is thrown by the driver.
355-
2. **Session & Transaction Start**:
356-
- `setAutoCommit(false)` initiates a multi-statement transaction session in BigQuery.
357-
3. **Statement Propagation**:
358-
- All `Statement` or `PreparedStatement` instances created on the connection execute within the scope of the active session.
359-
4. **Commit & Rollback**:
360-
- `commit()` executes `COMMIT TRANSACTION;` to commit changes.
361-
- `rollback()` executes `ROLLBACK TRANSACTION;` to discard changes.
362-
- If `autoCommit` remains `false`, the driver automatically starts the next transaction block.
363-
5. **Connection Close Safety**:
364-
- If an uncommitted transaction is pending when `conn.close()` is invoked, the driver automatically rolls back the transaction to prevent uncommitted changes from persisting.
365-
6. **Isolation Level & Holdability**:
366-
- Isolation level: `Connection.TRANSACTION_SERIALIZABLE` (BigQuery multi-statement snapshot isolation).
367-
- Holdability: `ResultSet.CLOSE_CURSORS_AT_COMMIT`.
332+
3. **Using an Existing Session (`QueryProperties=session_id=...`)**:
333+
- You can attach to a pre-existing BigQuery session by specifying `;QueryProperties=session_id=<existing_session_id>` in the connection URL.
334+
335+
4. **Connection Closure & Lifecycle**:
336+
- When `conn.close()` is called, sessions created by the driver are automatically terminated to release BigQuery server resources.
337+
- If a pre-existing session ID was supplied by the user (`QueryProperties=session_id=...`), the session is preserved when the connection is closed.
368338
369339
---
370340
@@ -382,7 +352,7 @@ For enterprise data ingestion and analytics extraction, the driver integrates wi
382352
## 7. Feature Examples & Code Snippets
383353
384354
### Transactions (Manual Commit & Rollback)
385-
Transactions require `;EnableSession=true` in the connection URL to enable multi-statement sessions in BigQuery.
355+
Transactions require `;EnableSession=true` in the connection URL to enable multi-statement transactions in BigQuery:
386356
387357
```java
388358
String url = "jdbc:bigquery://https://bigquery.googleapis.com:443;ProjectId=my-project;EnableSession=true;OAuthType=3";
@@ -411,6 +381,28 @@ try (Connection conn = DriverManager.getConnection(url)) {
411381
412382
---
413383
384+
### Connecting to a Pre-Existing Session
385+
To attach to an existing BigQuery session created outside the driver:
386+
387+
```java
388+
String existingSessionId = "your_existing_session_id_here";
389+
String url = "jdbc:bigquery://https://bigquery.googleapis.com:443;ProjectId=my-project;EnableSession=true;"
390+
+ "QueryProperties=session_id=" + existingSessionId + ";OAuthType=3";
391+
392+
try (Connection conn = DriverManager.getConnection(url);
393+
Statement stmt = conn.createStatement()) {
394+
395+
// Query tables or temporary objects in the pre-existing session
396+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM ExistingTempTable")) {
397+
while (rs.next()) {
398+
// Process rows...
399+
}
400+
}
401+
}
402+
```
403+
404+
---
405+
414406
### Prepared Statements & Parameter Binding
415407
Use `PreparedStatement` to safely bind parameters including primitive types, decimals (`BigDecimal`), temporal values (`Date`, `Timestamp`), and byte arrays (`byte[]`).
416408

0 commit comments

Comments
 (0)