@@ -192,6 +192,7 @@ public String[] getValidValues() {
192
192
private static final boolean DEFAULT_LENIENT = false ;
193
193
private static final boolean DEFAULT_ROUTE_TO_LEADER = true ;
194
194
private static final boolean DEFAULT_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE = false ;
195
+ private static final boolean DEFAULT_KEEP_TRANSACTION_ALIVE = false ;
195
196
private static final boolean DEFAULT_TRACK_SESSION_LEAKS = true ;
196
197
private static final boolean DEFAULT_TRACK_CONNECTION_LEAKS = true ;
197
198
private static final boolean DEFAULT_DATA_BOOST_ENABLED = false ;
@@ -269,6 +270,8 @@ public String[] getValidValues() {
269
270
/** Name of the 'delay transaction start until first write' property. */
270
271
public static final String DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE_NAME =
271
272
"delayTransactionStartUntilFirstWrite" ;
273
+ /** Name of the 'keep transaction alive' property. */
274
+ public static final String KEEP_TRANSACTION_ALIVE_PROPERTY_NAME = "keepTransactionAlive" ;
272
275
/** Name of the 'trackStackTraceOfSessionCheckout' connection property. */
273
276
public static final String TRACK_SESSION_LEAKS_PROPERTY_NAME = "trackSessionLeaks" ;
274
277
/** Name of the 'trackStackTraceOfConnectionCreation' connection property. */
@@ -405,6 +408,12 @@ private static String generateGuardedConnectionPropertyError(
405
408
+ "the first write operation in a read/write transaction will be executed using the read/write transaction. Enabling this mode can reduce locking "
406
409
+ "and improve performance for applications that can handle the lower transaction isolation semantics." ,
407
410
DEFAULT_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE ),
411
+ ConnectionProperty .createBooleanProperty (
412
+ KEEP_TRANSACTION_ALIVE_PROPERTY_NAME ,
413
+ "Enabling this option will trigger the connection to keep read/write transactions alive by executing a SELECT 1 query once every 10 seconds "
414
+ + "if no other statements are being executed. This option should be used with caution, as it can keep transactions alive and hold on to locks "
415
+ + "longer than intended. This option should typically be used for CLI-type application that might wait for user input for a longer period of time." ,
416
+ DEFAULT_KEEP_TRANSACTION_ALIVE ),
408
417
ConnectionProperty .createBooleanProperty (
409
418
TRACK_SESSION_LEAKS_PROPERTY_NAME ,
410
419
"Capture the call stack of the thread that checked out a session of the session pool. This will "
@@ -735,6 +744,7 @@ public static Builder newBuilder() {
735
744
private final RpcPriority rpcPriority ;
736
745
private final DdlInTransactionMode ddlInTransactionMode ;
737
746
private final boolean delayTransactionStartUntilFirstWrite ;
747
+ private final boolean keepTransactionAlive ;
738
748
private final boolean trackSessionLeaks ;
739
749
private final boolean trackConnectionLeaks ;
740
750
@@ -799,6 +809,7 @@ private ConnectionOptions(Builder builder) {
799
809
this .rpcPriority = parseRPCPriority (this .uri );
800
810
this .ddlInTransactionMode = parseDdlInTransactionMode (this .uri );
801
811
this .delayTransactionStartUntilFirstWrite = parseDelayTransactionStartUntilFirstWrite (this .uri );
812
+ this .keepTransactionAlive = parseKeepTransactionAlive (this .uri );
802
813
this .trackSessionLeaks = parseTrackSessionLeaks (this .uri );
803
814
this .trackConnectionLeaks = parseTrackConnectionLeaks (this .uri );
804
815
@@ -1179,6 +1190,12 @@ static boolean parseDelayTransactionStartUntilFirstWrite(String uri) {
1179
1190
: DEFAULT_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE ;
1180
1191
}
1181
1192
1193
+ @ VisibleForTesting
1194
+ static boolean parseKeepTransactionAlive (String uri ) {
1195
+ String value = parseUriProperty (uri , KEEP_TRANSACTION_ALIVE_PROPERTY_NAME );
1196
+ return value != null ? Boolean .parseBoolean (value ) : DEFAULT_KEEP_TRANSACTION_ALIVE ;
1197
+ }
1198
+
1182
1199
@ VisibleForTesting
1183
1200
static boolean parseTrackSessionLeaks (String uri ) {
1184
1201
String value = parseUriProperty (uri , TRACK_SESSION_LEAKS_PROPERTY_NAME );
@@ -1315,6 +1332,14 @@ static List<String> parseProperties(String uri) {
1315
1332
return res ;
1316
1333
}
1317
1334
1335
+ static long tryParseLong (String value , long defaultValue ) {
1336
+ try {
1337
+ return Long .parseLong (value );
1338
+ } catch (NumberFormatException ignore ) {
1339
+ return defaultValue ;
1340
+ }
1341
+ }
1342
+
1318
1343
/**
1319
1344
* Create a new {@link Connection} from this {@link ConnectionOptions}. Calling this method
1320
1345
* multiple times for the same {@link ConnectionOptions} will return multiple instances of {@link
@@ -1551,6 +1576,18 @@ boolean isDelayTransactionStartUntilFirstWrite() {
1551
1576
return delayTransactionStartUntilFirstWrite ;
1552
1577
}
1553
1578
1579
+ /**
1580
+ * Whether connections created by this {@link ConnectionOptions} should keep read/write
1581
+ * transactions alive by executing a SELECT 1 once every 10 seconds if no other statements are
1582
+ * executed. This option should be used with caution, as enabling it can keep transactions alive
1583
+ * for a very long time, which will hold on to any locks that have been taken by the transaction.
1584
+ * This option should typically only be enabled for CLI-type applications or other user-input
1585
+ * applications that might wait for a longer period of time on user input.
1586
+ */
1587
+ boolean isKeepTransactionAlive () {
1588
+ return keepTransactionAlive ;
1589
+ }
1590
+
1554
1591
boolean isTrackConnectionLeaks () {
1555
1592
return this .trackConnectionLeaks ;
1556
1593
}
0 commit comments