@@ -50,7 +50,7 @@ public abstract class Worker<T extends BenchmarkModule> implements Runnable {
50
50
51
51
private final int id ;
52
52
private final T benchmark ;
53
- protected final Connection conn ;
53
+ protected Connection conn = null ;
54
54
protected final WorkloadConfiguration configuration ;
55
55
protected final TransactionTypes transactionTypes ;
56
56
protected final Map <TransactionType , Procedure > procedures = new HashMap <>();
@@ -74,12 +74,14 @@ public Worker(T benchmark, int id) {
74
74
this .currStatement = null ;
75
75
this .transactionTypes = this .configuration .getTransTypes ();
76
76
77
- try {
78
- this .conn = this .benchmark .makeConnection ();
79
- this .conn .setAutoCommit (false );
80
- this .conn .setTransactionIsolation (this .configuration .getIsolationMode ());
81
- } catch (SQLException ex ) {
82
- throw new RuntimeException ("Failed to connect to database" , ex );
77
+ if (!this .configuration .getNewConnectionPerTxn ()) {
78
+ try {
79
+ this .conn = this .benchmark .makeConnection ();
80
+ this .conn .setAutoCommit (false );
81
+ this .conn .setTransactionIsolation (this .configuration .getIsolationMode ());
82
+ } catch (SQLException ex ) {
83
+ throw new RuntimeException ("Failed to connect to database" , ex );
84
+ }
83
85
}
84
86
85
87
// Generate all the Procedures that we're going to need
@@ -391,6 +393,20 @@ protected final void doWork(DatabaseType databaseType, TransactionType transacti
391
393
392
394
TransactionStatus status = TransactionStatus .UNKNOWN ;
393
395
396
+ if (this .conn == null ) {
397
+ try {
398
+ this .conn = this .benchmark .makeConnection ();
399
+ this .conn .setAutoCommit (false );
400
+ this .conn .setTransactionIsolation (this .configuration .getIsolationMode ());
401
+ } catch (SQLException ex ) {
402
+ if (LOG .isDebugEnabled ()) {
403
+ LOG .debug (String .format ("%s failed to open a connection..." , this ));
404
+ }
405
+ retryCount ++;
406
+ continue ;
407
+ }
408
+ }
409
+
394
410
try {
395
411
396
412
if (LOG .isDebugEnabled ()) {
@@ -438,6 +454,14 @@ protected final void doWork(DatabaseType databaseType, TransactionType transacti
438
454
}
439
455
440
456
} finally {
457
+ if (this .configuration .getNewConnectionPerTxn () && this .conn != null ) {
458
+ try {
459
+ this .conn .close ();
460
+ this .conn = null ;
461
+ } catch (SQLException e ) {
462
+ LOG .error ("Connection couldn't be closed." , e );
463
+ }
464
+ }
441
465
442
466
switch (status ) {
443
467
case UNKNOWN -> this .txnUnknown .put (transactionType );
@@ -511,10 +535,12 @@ protected void initialize() {
511
535
* Called at the end of the test to do any clean up that may be required.
512
536
*/
513
537
public void tearDown () {
514
- try {
515
- conn .close ();
516
- } catch (SQLException e ) {
517
- LOG .error ("Connection couldn't be closed." , e );
538
+ if (!this .configuration .getNewConnectionPerTxn () && this .conn != null ) {
539
+ try {
540
+ conn .close ();
541
+ } catch (SQLException e ) {
542
+ LOG .error ("Connection couldn't be closed." , e );
543
+ }
518
544
}
519
545
}
520
546
0 commit comments