Skip to content

Commit 61c2596

Browse files
mbutrovichapavlo
andauthored
Add newConnectionPerTxn config option and implementation (#208)
Co-authored-by: Andy Pavlo <[email protected]>
1 parent 3e559a6 commit 61c2596

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/main/java/com/oltpbenchmark/DBWorkload.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public static void main(String[] args) throws Exception {
125125
wrkld.setRandomSeed(xmlConfig.getInt("randomSeed", -1));
126126
wrkld.setBatchSize(xmlConfig.getInt("batchsize", 128));
127127
wrkld.setMaxRetries(xmlConfig.getInt("retries", 3));
128+
wrkld.setNewConnectionPerTxn(xmlConfig.getBoolean("newConnectionPerTxn", false));
128129

129130
int terminals = xmlConfig.getInt("terminals[not(@bench)]", 0);
130131
terminals = xmlConfig.getInt("terminals" + pluginTest, terminals);
@@ -170,6 +171,7 @@ public static void main(String[] args) throws Exception {
170171
initDebug.put("Batch Size", wrkld.getBatchSize());
171172
initDebug.put("Scale Factor", wrkld.getScaleFactor());
172173
initDebug.put("Terminals", wrkld.getTerminals());
174+
initDebug.put("New Connection Per Txn", wrkld.getNewConnectionPerTxn());
173175

174176
if (selectivity != -1) {
175177
initDebug.put("Selectivity", selectivity);

src/main/java/com/oltpbenchmark/WorkloadConfiguration.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public class WorkloadConfiguration {
5050
private String dataDir = null;
5151
private String ddlPath = null;
5252

53+
/**
54+
* If true, establish a new connection for each transaction, otherwise use one persistent connection per client
55+
* session. This is useful to measure the connection overhead.
56+
*/
57+
private boolean newConnectionPerTxn = false;
58+
5359
public String getBenchmarkName() {
5460
return benchmarkName;
5561
}
@@ -118,6 +124,23 @@ public void setMaxRetries(int maxRetries) {
118124
this.maxRetries = maxRetries;
119125
}
120126

127+
/**
128+
* @return @see newConnectionPerTxn member docs for behavior.
129+
*/
130+
public boolean getNewConnectionPerTxn() {
131+
return newConnectionPerTxn;
132+
}
133+
134+
/**
135+
* Used by the configuration loader at startup. Changing it any other time is probably dangeroues. @see
136+
* newConnectionPerTxn member docs for behavior.
137+
*
138+
* @param newConnectionPerTxn
139+
*/
140+
public void setNewConnectionPerTxn(boolean newConnectionPerTxn) {
141+
this.newConnectionPerTxn = newConnectionPerTxn;
142+
}
143+
121144
/**
122145
* Initiate a new benchmark and workload state
123146
*/

src/main/java/com/oltpbenchmark/api/Worker.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public abstract class Worker<T extends BenchmarkModule> implements Runnable {
5050

5151
private final int id;
5252
private final T benchmark;
53-
protected final Connection conn;
53+
protected Connection conn = null;
5454
protected final WorkloadConfiguration configuration;
5555
protected final TransactionTypes transactionTypes;
5656
protected final Map<TransactionType, Procedure> procedures = new HashMap<>();
@@ -74,12 +74,14 @@ public Worker(T benchmark, int id) {
7474
this.currStatement = null;
7575
this.transactionTypes = this.configuration.getTransTypes();
7676

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+
}
8385
}
8486

8587
// Generate all the Procedures that we're going to need
@@ -391,6 +393,20 @@ protected final void doWork(DatabaseType databaseType, TransactionType transacti
391393

392394
TransactionStatus status = TransactionStatus.UNKNOWN;
393395

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+
394410
try {
395411

396412
if (LOG.isDebugEnabled()) {
@@ -438,6 +454,14 @@ protected final void doWork(DatabaseType databaseType, TransactionType transacti
438454
}
439455

440456
} 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+
}
441465

442466
switch (status) {
443467
case UNKNOWN -> this.txnUnknown.put(transactionType);
@@ -511,10 +535,12 @@ protected void initialize() {
511535
* Called at the end of the test to do any clean up that may be required.
512536
*/
513537
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+
}
518544
}
519545
}
520546

0 commit comments

Comments
 (0)