2929import java .sql .ResultSet ;
3030import java .sql .ResultSetMetaData ;
3131import java .sql .SQLException ;
32- import java .sql .Statement ;
3332import java .sql .Types ;
3433import java .util .Arrays ;
3534import java .util .HashSet ;
36- import java .util .Properties ;
3735import java .util .Random ;
3836import java .util .Set ;
3937import java .util .regex .Pattern ;
@@ -56,10 +54,6 @@ public class ITDatabaseMetadataTest extends ITBase {
5654 private static final String CONSTRAINTS_TABLE_NAME = "JDBC_CONSTRAINTS_TEST_TABLE" ;
5755 private static final String CONSTRAINTS_TABLE_NAME2 = "JDBC_CONSTRAINTS_TEST_TABLE2" ;
5856 private static final String CONSTRAINTS_TABLE_NAME3 = "JDBC_CONSTRAINTS_TEST_TABLE3" ;
59-
60- static Connection bigQueryConnection ;
61- static Statement bigQueryStatement ;
62-
6357 private static final Pattern VERSION_PATTERN =
6458 Pattern .compile ("^(\\ d+)\\ .(\\ d+)(?:\\ .\\ d+)+\\ s*.*" );
6559 private static final String DEFAULT_CATALOG = ServiceOptions .getDefaultProjectId ();
@@ -71,15 +65,10 @@ public class ITDatabaseMetadataTest extends ITBase {
7165 public static void beforeClass () throws InterruptedException , SQLException {
7266 // Set up Dataset
7367 ITBase .setUpTable (DATASET , TABLE_NAME );
74- bigQueryConnection = DriverManager .getConnection (connection_uri , new Properties ());
75- bigQueryStatement = bigQueryConnection .createStatement ();
7668 }
7769
7870 @ AfterClass
79- public static void afterClass () throws SQLException {
80- bigQueryStatement .close ();
81- bigQueryConnection .close ();
82- }
71+ public static void afterClass () throws SQLException {}
8372
8473 @ Test
8574 public void testGetCatalogs () throws SQLException {
@@ -284,18 +273,20 @@ public void testGetPrimaryKeys() throws SQLException {
284273 connection .close ();
285274 }
286275
287- @ org . junit . jupiter . api . Test
276+ @ Test
288277 public void testTableConstraints () throws SQLException {
278+ Connection connection =
279+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
289280 ResultSet primaryKey1 =
290- bigQueryConnection
281+ connection
291282 .getMetaData ()
292283 .getPrimaryKeys (PROJECT_ID , CONSTRAINTS_DATASET , CONSTRAINTS_TABLE_NAME );
293284 primaryKey1 .next ();
294285 Assertions .assertEquals ("id" , primaryKey1 .getString (4 ));
295286 Assertions .assertFalse (primaryKey1 .next ());
296287
297288 ResultSet primaryKey2 =
298- bigQueryConnection
289+ connection
299290 .getMetaData ()
300291 .getPrimaryKeys (PROJECT_ID , CONSTRAINTS_DATASET , CONSTRAINTS_TABLE_NAME2 );
301292 primaryKey2 .next ();
@@ -305,7 +296,7 @@ public void testTableConstraints() throws SQLException {
305296 Assertions .assertFalse (primaryKey2 .next ());
306297
307298 ResultSet foreignKeys =
308- bigQueryConnection
299+ connection
309300 .getMetaData ()
310301 .getImportedKeys (PROJECT_ID , CONSTRAINTS_DATASET , CONSTRAINTS_TABLE_NAME );
311302 foreignKeys .next ();
@@ -323,7 +314,7 @@ public void testTableConstraints() throws SQLException {
323314 Assertions .assertFalse (foreignKeys .next ());
324315
325316 ResultSet crossReference =
326- bigQueryConnection
317+ connection
327318 .getMetaData ()
328319 .getCrossReference (
329320 PROJECT_ID ,
@@ -340,11 +331,15 @@ public void testTableConstraints() throws SQLException {
340331 Assertions .assertEquals ("last_name" , crossReference .getString (4 ));
341332 Assertions .assertEquals ("second_name" , crossReference .getString (8 ));
342333 Assertions .assertFalse (crossReference .next ());
334+ connection .close ();
343335 }
344336
345- @ org . junit . jupiter . api . Test
337+ @ Test
346338 public void testDatabaseMetadataGetCatalogs () throws SQLException {
347- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
339+
340+ Connection connection =
341+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
342+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
348343 try (ResultSet rs = databaseMetaData .getCatalogs ()) {
349344 assertNotNull (rs , "ResultSet from getCatalogs() should not be null" );
350345
@@ -359,13 +354,17 @@ public void testDatabaseMetadataGetCatalogs() throws SQLException {
359354 PROJECT_ID , rs .getString ("TABLE_CAT" ), "Catalog name should match Project ID" );
360355 Assertions .assertFalse (rs .next (), "ResultSet should have no more rows" );
361356 }
357+ connection .close ();
362358 }
363359
364- @ org . junit . jupiter . api . Test
360+ @ Test
365361 public void testDatabaseMetadataGetProcedures () throws SQLException {
362+
363+ Connection connection =
364+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
366365 String DATASET = "JDBC_INTEGRATION_DATASET" ;
367366 String procedureName = "create_customer" ;
368- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
367+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
369368 ResultSet resultSet = databaseMetaData .getProcedures (PROJECT_ID , DATASET , procedureName );
370369 while (resultSet .next ()) {
371370 Assertions .assertEquals (PROJECT_ID , resultSet .getString ("PROCEDURE_CAT" ));
@@ -375,11 +374,16 @@ public void testDatabaseMetadataGetProcedures() throws SQLException {
375374 Assertions .assertEquals (
376375 DatabaseMetaData .procedureResultUnknown , resultSet .getInt ("PROCEDURE_TYPE" ));
377376 }
377+ resultSet .close ();
378+ connection .close ();
378379 }
379380
380- @ org . junit . jupiter . api . Test
381+ @ Test
381382 public void testDatabaseMetadataGetProcedureColumns () throws SQLException {
382- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
383+
384+ Connection connection =
385+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
386+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
383387
384388 // --- Test Case 1: Specific schema and procedure, null column name pattern ---
385389 String specificSchema = "JDBC_INTEGRATION_DATASET" ;
@@ -435,13 +439,17 @@ public void testDatabaseMetadataGetProcedureColumns() throws SQLException {
435439 Assertions .assertFalse (
436440 resultSet .next (), "Should not find columns for a non-existent procedure" );
437441 resultSet .close ();
442+ connection .close ();
438443 }
439444
440- @ org . junit . jupiter . api . Test
445+ @ Test
441446 public void testDatabaseMetadataGetColumns () throws SQLException {
447+
448+ Connection connection =
449+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
442450 String DATASET = "JDBC_INTEGRATION_DATASET" ;
443451 String TABLE_NAME = "JDBC_DATATYPES_INTEGRATION_TEST_TABLE" ;
444- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
452+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
445453
446454 // --- Test Case 1: Specific Column (StringField) ---
447455 ResultSet resultSet =
@@ -623,11 +631,15 @@ public void testDatabaseMetadataGetColumns() throws SQLException {
623631 Assertions .assertEquals (1 , resultSet .getInt ("NULLABLE" ));
624632 Assertions .assertEquals (14 , resultSet .getInt ("ORDINAL_POSITION" ));
625633 Assertions .assertFalse (resultSet .next ());
634+
635+ connection .close ();
626636 }
627637
628- @ org . junit . jupiter . api . Test
638+ @ Test
629639 public void testDatabaseMetadataGetTables () throws SQLException {
630- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
640+ Connection connection =
641+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
642+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
631643 String DATASET = "JDBC_TABLE_TYPES_TEST" ;
632644
633645 // --- Test Case 1: Get all tables (types = null) ---
@@ -721,11 +733,14 @@ public void testDatabaseMetadataGetTables() throws SQLException {
721733 Assertions .assertEquals ("VIEW" , rsNullType .getString ("TABLE_TYPE" ));
722734 Assertions .assertEquals ("my_view" , rsNullType .getString ("TABLE_NAME" ));
723735 Assertions .assertFalse (rsNullType .next ());
736+ connection .close ();
724737 }
725738
726- @ org . junit . jupiter . api . Test
739+ @ Test
727740 public void testDatabaseMetadataGetSchemas () throws SQLException {
728- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
741+ Connection connection =
742+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
743+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
729744
730745 // Test case 1: Get all schemas with catalog and check for the presence of specific schemas
731746 ResultSet rsAll = databaseMetaData .getSchemas (PROJECT_ID , null );
@@ -755,12 +770,15 @@ public void testDatabaseMetadataGetSchemas() throws SQLException {
755770 // Test case 4: Get schemas with non-existent catalog
756771 rsNoMatch = databaseMetaData .getSchemas ("invalid-catalog" , null );
757772 Assertions .assertFalse (rsNoMatch .next ());
773+ connection .close ();
758774 }
759775
760- @ org . junit . jupiter . api . Test
776+ @ Test
761777 public void testDatabaseMetadataGetSchemasNoArgs () throws SQLException {
762- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
763- String expectedCatalog = bigQueryConnection .getCatalog ();
778+ Connection connection =
779+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
780+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
781+ String expectedCatalog = connection .getCatalog ();
764782 assertNotNull (expectedCatalog , "Project ID (catalog) from connection should not be null" );
765783
766784 // Test case: Get all schemas (datasets) for the current project
@@ -788,11 +806,14 @@ public void testDatabaseMetadataGetSchemasNoArgs() throws SQLException {
788806 foundTestDataset , "At least one of the known test datasets should be found" );
789807 Assertions .assertTrue (rowCount > 0 , "Should retrieve at least one schema/dataset" );
790808 }
809+ connection .close ();
791810 }
792811
793- @ org . junit . jupiter . api . Test
812+ @ Test
794813 public void testDatabaseMetaDataGetFunctions () throws SQLException {
795- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
814+ Connection connection =
815+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
816+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
796817 String testSchema = "JDBC_TABLE_TYPES_TEST" ;
797818 String testCatalog = PROJECT_ID ;
798819
@@ -896,11 +917,14 @@ public void testDatabaseMetaDataGetFunctions() throws SQLException {
896917 ResultSet rsNullCatalog = databaseMetaData .getFunctions (null , testSchema , null );
897918 Assertions .assertFalse (rsNullCatalog .next (), "Null catalog should return no results" );
898919 rsNullCatalog .close ();
920+ connection .close ();
899921 }
900922
901- @ org . junit . jupiter . api . Test
923+ @ Test
902924 public void testDatabaseMetadataGetFunctionColumns () throws SQLException {
903- DatabaseMetaData databaseMetaData = bigQueryConnection .getMetaData ();
925+ Connection connection =
926+ DriverManager .getConnection (String .format (connectionUrl , DEFAULT_CATALOG ));
927+ DatabaseMetaData databaseMetaData = connection .getMetaData ();
904928 String testCatalog = PROJECT_ID ;
905929 String testSchema = "JDBC_TABLE_TYPES_TEST" ;
906930
@@ -1022,9 +1046,10 @@ public void testDatabaseMetadataGetFunctionColumns() throws SQLException {
10221046 testCatalog , testSchema , "non_existent_function_xyz" , null );
10231047 Assertions .assertFalse (rs .next (), "Should not find columns for a non-existent function" );
10241048 rs .close ();
1049+ connection .close ();
10251050 }
10261051
1027- @ org . junit . jupiter . api . Test
1052+ @ Test
10281053 public void testAdditionalProjectsInMetadata () throws SQLException {
10291054 String additionalProjectsValue = "bigquery-public-data" ;
10301055 String datasetInAdditionalProject = "baseball" ;
@@ -1083,8 +1108,9 @@ public void testAdditionalProjectsInMetadata() throws SQLException {
10831108 }
10841109 }
10851110
1086- @ org . junit . jupiter . api . Test
1111+ @ Test
10871112 public void testFilterTablesOnDefaultDataset_getTables () throws SQLException {
1113+
10881114 String defaultDatasetValue = CONSTRAINTS_DATASET ;
10891115 String table1InDefaultDataset = CONSTRAINTS_TABLE_NAME ;
10901116 String table2InDefaultDataset = CONSTRAINTS_TABLE_NAME2 ;
@@ -1154,7 +1180,7 @@ public void testFilterTablesOnDefaultDataset_getTables() throws SQLException {
11541180 }
11551181 }
11561182
1157- @ org . junit . jupiter . api . Test
1183+ @ Test
11581184 public void testFilterTablesOnDefaultDataset_getColumns () throws SQLException {
11591185 String defaultDatasetValue = CONSTRAINTS_DATASET ;
11601186 String tableInDefaultDataset = CONSTRAINTS_TABLE_NAME ;
0 commit comments