6666import java .sql .Statement ;
6767import java .time .Duration ;
6868import java .util .ArrayList ;
69+ import java .util .Collections ;
6970import java .util .ConcurrentModificationException ;
7071import java .util .List ;
7172import java .util .Map ;
7677import java .util .concurrent .Executor ;
7778import java .util .concurrent .ExecutorService ;
7879import java .util .concurrent .TimeUnit ;
79- import java .util .concurrent .locks .ReentrantLock ;
8080
8181/**
8282 * An implementation of {@link java.sql.Connection} for establishing a connection with BigQuery and
8787public class BigQueryConnection extends BigQueryNoOpsConnection {
8888
8989 private final BigQueryJdbcCustomLogger LOG = new BigQueryJdbcCustomLogger (this .toString ());
90- private final ReentrantLock queryPropertiesLock = new ReentrantLock ();
9190 String connectionClassName = this .toString ();
9291 private final String connectionId ;
9392 private static final String DEFAULT_JDBC_TOKEN_VALUE = "Google-BigQuery-JDBC-Driver" ;
@@ -179,7 +178,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
179178 // transactionStarted is false by default.
180179 // when autocommit is false transaction starts and session is initialized.
181180 boolean transactionStarted ;
182- ConnectionProperty sessionInfoConnectionProperty ;
181+ volatile ConnectionProperty sessionInfoConnectionProperty ;
183182 boolean isClosed ;
184183 DatasetId defaultDataset ;
185184 String location ;
@@ -199,7 +198,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
199198 long destinationDatasetExpirationTime ;
200199 String kmsKeyName ;
201200 String universeDomain ;
202- List <ConnectionProperty > queryProperties ;
201+ private volatile List <ConnectionProperty > queryProperties ;
203202 Map <String , String > authProperties ;
204203 Map <String , String > overrideProperties ;
205204 Map <String , String > proxyProperties ;
@@ -615,12 +614,7 @@ String getKmsKeyName() {
615614 }
616615
617616 List <ConnectionProperty > getQueryProperties () {
618- queryPropertiesLock .lock ();
619- try {
620- return this .queryProperties ;
621- } finally {
622- queryPropertiesLock .unlock ();
623- }
617+ return this .queryProperties ;
624618 }
625619
626620 public String getLocation () {
@@ -705,30 +699,29 @@ private void beginTransaction() {
705699 }
706700 }
707701
708- void updateSessionInfo (String sessionId ) {
702+ synchronized void updateSessionInfo (String sessionId ) {
709703 if (sessionId != null && !sessionId .isEmpty ()) {
710- queryPropertiesLock .lock ();
711- try {
712- if (this .sessionInfoConnectionProperty == null
713- || !sessionId .equals (this .sessionInfoConnectionProperty .getValue ())) {
714- this .sessionInfoConnectionProperty =
715- ConnectionProperty .newBuilder ().setKey ("session_id" ).setValue (sessionId ).build ();
716- boolean found = false ;
717- if (this .queryProperties != null ) {
718- for (int i = 0 ; i < this .queryProperties .size (); i ++) {
719- if ("session_id" .equalsIgnoreCase (this .queryProperties .get (i ).getKey ())) {
720- this .queryProperties .set (i , this .sessionInfoConnectionProperty );
721- found = true ;
722- break ;
723- }
724- }
725- if (!found ) {
726- this .queryProperties .add (this .sessionInfoConnectionProperty );
727- }
704+ if (this .sessionInfoConnectionProperty == null
705+ || !sessionId .equals (this .sessionInfoConnectionProperty .getValue ())) {
706+ ConnectionProperty sessionProperty =
707+ ConnectionProperty .newBuilder ().setKey ("session_id" ).setValue (sessionId ).build ();
708+ this .sessionInfoConnectionProperty = sessionProperty ;
709+ List <ConnectionProperty > updated =
710+ this .queryProperties != null
711+ ? new ArrayList <>(this .queryProperties )
712+ : new ArrayList <>();
713+ boolean found = false ;
714+ for (int i = 0 ; i < updated .size (); i ++) {
715+ if ("session_id" .equalsIgnoreCase (updated .get (i ).getKey ())) {
716+ updated .set (i , sessionProperty );
717+ found = true ;
718+ break ;
728719 }
729720 }
730- } finally {
731- queryPropertiesLock .unlock ();
721+ if (!found ) {
722+ updated .add (sessionProperty );
723+ }
724+ this .queryProperties = Collections .unmodifiableList (updated );
732725 }
733726 }
734727 }
@@ -746,12 +739,7 @@ boolean isUnsupportedHTAPIFallback() {
746739 }
747740
748741 public ConnectionProperty getSessionInfoConnectionProperty () {
749- queryPropertiesLock .lock ();
750- try {
751- return this .sessionInfoConnectionProperty ;
752- } finally {
753- queryPropertiesLock .unlock ();
754- }
742+ return this .sessionInfoConnectionProperty ;
755743 }
756744
757745 boolean isEnableHighThroughputAPI () {
@@ -1214,7 +1202,7 @@ private List<ConnectionProperty> convertMapToConnectionPropertiesList(
12141202 .build ());
12151203 }
12161204 }
1217- return connectionProperties ;
1205+ return Collections . unmodifiableList ( connectionProperties ) ;
12181206 }
12191207
12201208 void removeStatement (Statement statement ) {
0 commit comments