diff --git a/lib/guava-31.1-jre.jar b/lib/guava-31.1-jre.jar new file mode 100644 index 0000000..1681922 Binary files /dev/null and b/lib/guava-31.1-jre.jar differ diff --git a/lib/guava-retrying-2.0.0.jar b/lib/guava-retrying-2.0.0.jar new file mode 100644 index 0000000..d147eba Binary files /dev/null and b/lib/guava-retrying-2.0.0.jar differ diff --git a/src/client/jTPCC.java b/src/client/jTPCC.java index 5e7dc9c..2f811ec 100644 --- a/src/client/jTPCC.java +++ b/src/client/jTPCC.java @@ -484,7 +484,7 @@ else if(newOrderWeightValue == 0 && paymentWeightValue == 0 && orderStatusWeight conn, dbType, transactionsPerTerminal, terminalWarehouseFixed, paymentWeightValue, orderStatusWeightValue, - deliveryWeightValue, stockLevelWeightValue, numWarehouses, limPerMin_Terminal, this); + deliveryWeightValue, stockLevelWeightValue, numWarehouses, limPerMin_Terminal, this, database, dbProps); terminals[i] = terminal; terminalNames[i] = terminalName; diff --git a/src/client/jTPCCConnection.java b/src/client/jTPCCConnection.java index bb36623..7c53660 100644 --- a/src/client/jTPCCConnection.java +++ b/src/client/jTPCCConnection.java @@ -297,6 +297,11 @@ public jTPCCConnection(Connection dbConn, int dbType) " WHERE c_w_id = ? AND c_d_id = ? AND c_id = ?"); } + public Connection getDbConn() + { + return this.dbConn; + } + public jTPCCConnection(String connURL, Properties connProps, int dbType) throws SQLException { @@ -316,6 +321,6 @@ public void commit() public void rollback() throws SQLException { - dbConn.rollback(); + dbConn.rollback(); } } diff --git a/src/client/jTPCCTData.java b/src/client/jTPCCTData.java index 74872cb..f85d653 100644 --- a/src/client/jTPCCTData.java +++ b/src/client/jTPCCTData.java @@ -568,10 +568,6 @@ else if (newOrder.ol_supply_w_id[ol_seq[y]] == newOrder.ol_supply_w_id[ol_seq[x] } catch (SQLException se) { - log.error("Unexpected SQLException in NEW_ORDER"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); try { @@ -935,10 +931,6 @@ private void executePayment(Logger log, jTPCCConnection db) } catch (SQLException se) { - log.error("Unexpected SQLException in PAYMENT"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); try { @@ -1232,10 +1224,6 @@ private void executeOrderStatus(Logger log, jTPCCConnection db) } catch (SQLException se) { - log.error("Unexpected SQLException in ORDER_STATUS"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); try { @@ -1388,10 +1376,6 @@ private void executeStockLevel(Logger log, jTPCCConnection db) } catch (SQLException se) { - log.error("Unexpected SQLException in STOCK_LEVEL"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); try { @@ -1731,10 +1715,6 @@ private void executeDeliveryBG(Logger log, jTPCCConnection db) } catch (SQLException se) { - log.error("Unexpected SQLException in DELIVERY_BG"); - for (SQLException x = se; x != null; x = x.getNextException()) - log.error(x.getMessage()); - se.printStackTrace(); try { diff --git a/src/client/jTPCCTerminal.java b/src/client/jTPCCTerminal.java index e75e4c7..a4015c2 100644 --- a/src/client/jTPCCTerminal.java +++ b/src/client/jTPCCTerminal.java @@ -6,19 +6,26 @@ * Copyright (C) 2016, Jan Wieck * */ -import org.apache.log4j.*; -import java.io.*; +import com.github.rholder.retry.RetryException; +import com.github.rholder.retry.Retryer; +import com.github.rholder.retry.RetryerBuilder; +import com.github.rholder.retry.StopStrategies; +import org.apache.log4j.Logger; + +import java.io.PrintWriter; +import java.io.StringWriter; import java.sql.*; -import java.sql.Date; -import java.util.*; -import javax.swing.*; +import java.util.Properties; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; -public class jTPCCTerminal implements jTPCCConfig, Runnable -{ +public class jTPCCTerminal implements jTPCCConfig, Runnable { private static org.apache.log4j.Logger log = Logger.getLogger(jTPCCTerminal.class); + static Retryer connectRetryer = connectRetryer(); + private String terminalName; private Connection conn = null; private Statement stmt = null; @@ -45,12 +52,16 @@ public class jTPCCTerminal implements jTPCCConfig, Runnable jTPCCConnection db = null; int dbType = 0; + private String database = ""; + private Properties dbProps = null; + public jTPCCTerminal (String terminalName, int terminalWarehouseID, int terminalDistrictID, Connection conn, int dbType, int numTransactions, boolean terminalWarehouseFixed, int paymentWeight, int orderStatusWeight, - int deliveryWeight, int stockLevelWeight, int numWarehouses, int limPerMin_Terminal, jTPCC parent) throws SQLException + int deliveryWeight, int stockLevelWeight, int numWarehouses, int limPerMin_Terminal, jTPCC parent, + String database, Properties dbProp) throws SQLException { this.terminalName = terminalName; this.conn = conn; @@ -76,6 +87,9 @@ public class jTPCCTerminal implements jTPCCConfig, Runnable this.newOrderCounter = 0; this.limPerMin_Terminal = limPerMin_Terminal; + this.database = database; + this.dbProps = dbProp; + this.db = new jTPCCConnection(conn, dbType); terminalMessage(""); @@ -167,9 +181,12 @@ private void executeTransactions(int numTransactions) } catch (Exception e) { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); + try { + Callable retryConnect = () -> retryConnect(); + connectRetryer.call(retryConnect); + } catch (ExecutionException | RetryException ex) { + throw new RuntimeException(ex); + } } transactionTypeName = "Payment"; } @@ -193,9 +210,12 @@ else if(transactionType <= paymentWeight + stockLevelWeight) } catch (Exception e) { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); + try { + Callable retryConnect = () -> retryConnect(); + connectRetryer.call(retryConnect); + } catch (ExecutionException | RetryException ex) { + throw new RuntimeException(ex); + } } transactionTypeName = "Stock-Level"; } @@ -219,9 +239,12 @@ else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight) } catch (Exception e) { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); + try { + Callable retryConnect = () -> retryConnect(); + connectRetryer.call(retryConnect); + } catch (ExecutionException | RetryException ex) { + throw new RuntimeException(ex); + } } transactionTypeName = "Order-Status"; } @@ -258,9 +281,12 @@ else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight } catch (Exception e) { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); + try { + Callable retryConnect = () -> retryConnect(); + connectRetryer.call(retryConnect); + } catch (ExecutionException | RetryException ex) { + throw new RuntimeException(ex); + } } transactionTypeName = "Delivery"; } @@ -284,9 +310,12 @@ else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight } catch (Exception e) { - log.fatal(e.getMessage()); - e.printStackTrace(); - System.exit(4); + try { + Callable retryConnect = () -> retryConnect(); + connectRetryer.call(retryConnect); + } catch (ExecutionException | RetryException ex) { + throw new RuntimeException(ex); + } } transactionTypeName = "New-Order"; newOrderCounter++; @@ -367,5 +396,32 @@ void transCommit() { } // end transCommit() + boolean retryConnect() throws SQLException { + if (!db.getDbConn().isClosed()){ + return true; + } + long startTime = System.currentTimeMillis(); + Connection c = DriverManager.getConnection(this.database, this.dbProps); + c.setAutoCommit(false); + this.conn = c; + this.stmt = c.createStatement(); + this.stmt.setMaxRows(200); + this.stmt.setFetchSize(100); + + this.stmt1 = c.createStatement(); + this.stmt1.setMaxRows(1); + + this.db = new jTPCCConnection(c, dbType); + log.info("Reconnected to " + this.database + " in " + (System.currentTimeMillis() - startTime) + "ms"); + return true; + } + + public static Retryer connectRetryer() { + return RetryerBuilder.newBuilder() + .retryIfExceptionOfType(Exception.class) + .withStopStrategy(StopStrategies.stopAfterDelay(1000 * 120, java.util.concurrent.TimeUnit.MILLISECONDS)) + .build(); + } + }