Commit 40b638d5 authored by Andre Freyssinet's avatar Andre Freyssinet

Fix JDBCTransaction parameters for reconnect mechanism.

Adds a metod allowing to dump configuration properties.
parent b7ae54aa
...@@ -91,9 +91,13 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe ...@@ -91,9 +91,13 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
*/ */
public final static String JDBC_CONNECT_RETRY_COUNT_PROP = JDBC_TRANSACTION_PREFIX + ".connect_retry_count"; public final static String JDBC_CONNECT_RETRY_COUNT_PROP = JDBC_TRANSACTION_PREFIX + ".connect_retry_count";
/** /**
* This property is used to set the maximum time to attempt to reconnect after a failure, by default 60.000 (60 seconds). * This property is used to set the minimum time between two attempts to reconnect after a failure, by default 1.000 (1 seconds).
*/ */
public final static String JDBC_CONNECT_RETRY_DELAY_PROP = JDBC_TRANSACTION_PREFIX + ".connect_retry_delay"; public final static String JDBC_CONNECT_RETRY_MIN_DELAY_PROP = JDBC_TRANSACTION_PREFIX + ".connect_retry_min_delay";
/**
* This property is used to set the maximum time trying to reconnect after a failure, by default 60.000 (60 seconds).
*/
public final static String JDBC_CONNECT_RETRY_MAX_PERIOD_PROP = JDBC_TRANSACTION_PREFIX + ".connect_retry_max_period";
/** /**
* This property allows to define the SQL statement allowing to create the table used by the module, for example: * This property allows to define the SQL statement allowing to create the table used by the module, for example:
...@@ -253,7 +257,11 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe ...@@ -253,7 +257,11 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
} }
connectRetryCount = AgentServer.getInteger(JDBC_CONNECT_RETRY_COUNT_PROP, JDBC_CONNECT_RETRY_COUNT_DFLT); connectRetryCount = AgentServer.getInteger(JDBC_CONNECT_RETRY_COUNT_PROP, JDBC_CONNECT_RETRY_COUNT_DFLT);
connectRetryDelay = AgentServer.getLong(JDBC_CONNECT_RETRY_DELAY_PROP, JDBC_CONNECT_RETRY_DELAY_DFLT); if (connectRetryCount < 0) connectRetryCount = 0;
connectRetryMinDelay = AgentServer.getLong(JDBC_CONNECT_RETRY_MIN_DELAY_PROP, JDBC_CONNECT_RETRY_MIN_DELAY_DFLT);
if (connectRetryMinDelay < 0) connectRetryMinDelay = 0;
connectRetryMaxPeriod = AgentServer.getLong(JDBC_CONNECT_RETRY_MAX_PERIOD_PROP, JDBC_CONNECT_RETRY_MAX_PERIOD_DFLT);
if (connectRetryMaxPeriod < 0) connectRetryMaxPeriod = 0;
try { try {
Class.forName(driver).newInstance(); Class.forName(driver).newInstance();
...@@ -286,6 +294,7 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe ...@@ -286,6 +294,7 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
logmon.log(BasicLevel.WARN, "DBTransaction, init()", sqle); logmon.log(BasicLevel.WARN, "DBTransaction, init()", sqle);
} }
} }
logmon.log(BasicLevel.INFO, "DBTransaction, init(): " + dumpProperties());
} }
@Override @Override
...@@ -329,4 +338,18 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe ...@@ -329,4 +338,18 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
public String getPropertiesPath() { public String getPropertiesPath() {
return path; return path;
} }
protected void dumpProperties(StringBuilder strbuf) {
super.dumpProperties(strbuf);
strbuf.append('(').append("driver=").append(driver).append(')');
strbuf.append('(').append("url=").append(connurl).append(')');
strbuf.append('(').append("dbtable=").append(dbtable).append(')');
strbuf.append('(').append("dbname=").append(dbname).append(')');
strbuf.append('(').append("user=").append(user).append(')');
strbuf.append('(').append("password=").append("***").append(')');
strbuf.append('(').append("connect_retry_count=").append(connectRetryCount).append(')');
strbuf.append('(').append("connect_retry_min_delay=").append(connectRetryMinDelay).append(')');
strbuf.append('(').append("connect_retry_max_period=").append(connectRetryMaxPeriod).append(')');
}
} }
...@@ -457,8 +457,10 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra ...@@ -457,8 +457,10 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
protected final int JDBC_CONNECT_RETRY_COUNT_DFLT = 5; protected final int JDBC_CONNECT_RETRY_COUNT_DFLT = 5;
protected int connectRetryCount = JDBC_CONNECT_RETRY_COUNT_DFLT; protected int connectRetryCount = JDBC_CONNECT_RETRY_COUNT_DFLT;
protected final long JDBC_CONNECT_RETRY_DELAY_DFLT = 60000L; protected final long JDBC_CONNECT_RETRY_MAX_PERIOD_DFLT = 60000L;
protected long connectRetryDelay = JDBC_CONNECT_RETRY_DELAY_DFLT; protected long connectRetryMaxPeriod = JDBC_CONNECT_RETRY_MAX_PERIOD_DFLT;
protected final long JDBC_CONNECT_RETRY_MIN_DELAY_DFLT = 1000L;
protected long connectRetryMinDelay = JDBC_CONNECT_RETRY_MIN_DELAY_DFLT;
public final synchronized void commit(boolean release) throws IOException { public final synchronized void commit(boolean release) throws IOException {
if (phase != RUN) if (phase != RUN)
...@@ -472,6 +474,7 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra ...@@ -472,6 +474,7 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
boolean completed = false; boolean completed = false;
int retry = 0; int retry = 0;
long startRetry = 0L; long startRetry = 0L;
long lastTry = 0L;
SQLException lastexc = null; SQLException lastexc = null;
do { do {
try { try {
...@@ -486,12 +489,25 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra ...@@ -486,12 +489,25 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
lastexc = sqle; lastexc = sqle;
retry += 1; retry += 1;
if ((connectRetryCount > 0) && (retry > connectRetryCount)) break; long time = System.currentTimeMillis();
if (startRetry == 0L) startRetry = System.currentTimeMillis(); if (startRetry == 0L) startRetry = time;
if ((connectRetryDelay > 0L) && ((System.currentTimeMillis() - startRetry) > connectRetryDelay)) break;
if ((connectRetryCount > 0) && (retry > connectRetryCount)) break;
if ((connectRetryMaxPeriod > 0L) && ((time - startRetry) > connectRetryMaxPeriod)) break;
try { try {
if (lastTry == 0L) {
// It's the first retry do not wait.
lastTry = time;
} else {
// Wait for at least configured delay
long delay = connectRetryMinDelay - (time - lastTry);
lastTry = time;
try {
if (delay > 0) Thread.sleep(delay);
} catch (InterruptedException exc) {}
}
connectDB(); connectDB();
createPreparedStatement(); createPreparedStatement();
} catch (IOException exc) {} } catch (IOException exc) {}
...@@ -808,6 +824,21 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra ...@@ -808,6 +824,21 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
logmon.log(BasicLevel.INFO, "DBTransaction, closed"); logmon.log(BasicLevel.INFO, "DBTransaction, closed");
} }
} }
@Override
public String dumpProperties() {
StringBuilder strbuf = new StringBuilder();
strbuf.append('[');
dumpProperties(strbuf);
strbuf.append(']');
return strbuf.toString();
}
protected void dumpProperties(StringBuilder strbuf) {
strbuf.append('(').append("dbtable=").append(dbtable);
}
} }
final class DBOperation implements Serializable { final class DBOperation implements Serializable {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment