Commit f3959655 authored by Andre Freyssinet's avatar Andre Freyssinet

Fix JDBCTransaction parameters for reconnect mechanism.

Adds a method allowing to dump configuration properties.
parent 82eee39a
......@@ -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";
/**
* 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:
......@@ -253,7 +257,11 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
}
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 {
Class.forName(driver).newInstance();
......@@ -286,6 +294,7 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
logmon.log(BasicLevel.WARN, "DBTransaction, init()", sqle);
}
}
logmon.log(BasicLevel.INFO, "DBTransaction, init(): " + dumpProperties());
}
@Override
......@@ -329,4 +338,18 @@ public class JDBCTransaction extends DBTransaction implements JDBCTransactionMBe
public String getPropertiesPath() {
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(')');
}
}
......@@ -459,8 +459,10 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
protected final int JDBC_CONNECT_RETRY_COUNT_DFLT = 5;
protected int connectRetryCount = JDBC_CONNECT_RETRY_COUNT_DFLT;
protected final long JDBC_CONNECT_RETRY_DELAY_DFLT = 60000L;
protected long connectRetryDelay = JDBC_CONNECT_RETRY_DELAY_DFLT;
protected final long JDBC_CONNECT_RETRY_MAX_PERIOD_DFLT = 60000L;
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 {
if (phase != RUN)
......@@ -474,29 +476,52 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
boolean completed = false;
int retry = 0;
long startRetry = 0L;
long lastTry = 0L;
SQLException lastexc = null;
do {
try {
// TODO (AF): The dbLogCommit is currently done after each connection attempt, even if the
// connection fails. We should set a boolean in the catch clause of connectDB and execute the
// dbLogCommit only if the connection is successful.
dbLogCommit(log);
lastexc = null;
completed = true;
} catch (SQLException sqle) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.WARN, "DBTransaction, commit: try to reconnect", sqle);
else
logmon.log(BasicLevel.WARN, "DBTransaction, commit: try to reconnect - " + sqle.getMessage());
lastexc = sqle;
retry += 1;
if ((connectRetryCount > 0) && (retry > connectRetryCount)) break;
if (startRetry == 0L) startRetry = System.currentTimeMillis();
if ((connectRetryDelay > 0L) && ((System.currentTimeMillis() - startRetry) > connectRetryDelay)) break;
long time = System.currentTimeMillis();
if (startRetry == 0L) startRetry = time;
if ((connectRetryCount > 0) && (retry > connectRetryCount)) {
logmon.log(BasicLevel.WARN, "DBTransaction, commit: maximum number of reconnection attempts reached: " + connectRetryCount);
break;
}
if ((connectRetryMaxPeriod > 0L) && ((time - startRetry) > connectRetryMaxPeriod)) {
logmon.log(BasicLevel.WARN, "DBTransaction, commit: reconnection period exceeded: " + connectRetryMaxPeriod);
break;
}
try {
// Do not wait the 1st time.
if (lastTry != 0L) {
// Wait for at least configured delay since last attempt to connect
long delay = connectRetryMinDelay - (time - lastTry);
try {
if (delay > 0) Thread.sleep(delay);
} catch (InterruptedException exc) {}
}
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.WARN, "DBTransaction, commit: try to reconnect", sqle);
else
logmon.log(BasicLevel.WARN, "DBTransaction, commit: try to reconnect - " + sqle.getMessage());
lastTry = System.currentTimeMillis();
connectDB();
createPreparedStatement();
} catch (IOException exc) {}
} catch (IOException exc) {
logmon.log(BasicLevel.INFO, "DBTransaction, commit: cannot reconnect" + exc.getMessage());
}
}
} while (!completed);
......@@ -810,6 +835,21 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
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 {
......
......@@ -88,4 +88,11 @@ public interface DBTransactionMBean extends TransactionMBean {
String getObject(String dirname, String name);
String getObjectList(String prefix);
/**
* Returns the configuration properties of the component.
*
* @return the configuration properties of the component.
*/
String dumpProperties();
}
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