Skip to content

Commit

Permalink
[CONJ-419] checking if master connection is still in write mode when …
Browse files Browse the repository at this point in the history
…having exception with 1290 error code (ER_OPTION_PREVENTS_STATEMENT)
  • Loading branch information
rusher committed Feb 9, 2017
1 parent 31a264d commit 7e4650d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/HostAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public String toString() {
return "HostAddress{"
+ "host='" + host + '\''
+ ", port=" + port
+ ", type=" + ((type == null) ? null : "'" + type + "'")
+ ((type != null) ? (", type='" + type + "'") : "")
+ "}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ protected void resetMasterFailoverData() {

protected void setSessionReadOnly(boolean readOnly, Protocol protocol) throws QueryException {
if (protocol.versionGreaterOrEqual(5, 6, 5)) {
logger.info("SQL node [" + protocol.getHostAddress().toString()
+ ", conn " + protocol.getServerThreadId()
+ " ] is now in " + (readOnly ? "read-only" : "write") + " mode.");
protocol.executeQuery("SET SESSION TRANSACTION " + (readOnly ? "READ ONLY" : "READ WRITE"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public HandleErrorResult handleFailover(QueryException qe, Method method, Object
throw new QueryException("Connection has been closed !");
}
if (protocol.mustBeMasterConnection()) {
if (setMasterHostFail()) {
if (!protocol.isMasterConnection()) {
logger.warn("SQL Primary node [" + this.currentProtocol.getHostAddress().toString()
+ ", conn " + this.currentProtocol.getServerThreadId()
+ " ] is now in read-only mode. Exception : " + qe.getMessage());
} else if (setMasterHostFail()) {
logger.warn("SQL Primary node [" + this.currentProtocol.getHostAddress().toString()
+ ", conn " + this.currentProtocol.getServerThreadId()
+ " ] connection fail. Reason : " + qe.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,20 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
} catch (InvocationTargetException e) {
if (e.getTargetException() != null) {
if (e.getTargetException() instanceof QueryException) {
if (hasToHandleFailover((QueryException) e.getTargetException())) {
return handleFailOver((QueryException) e.getTargetException(), method, args, listener.getCurrentProtocol());
QueryException queryException = (QueryException) e.getTargetException();
addExceptionHost(queryException, listener.getCurrentProtocol());
if (hasToHandleFailover(queryException)) {
return handleFailOver(queryException, method, args, listener.getCurrentProtocol());
}

//error is "The MySQL server is running with the %s option so it cannot execute this statement"
//checking that server was master has not been demote to slave without resetting connections
if (queryException.getErrorCode() == 1290
&& listener.getCurrentProtocol() != null
&& listener.getCurrentProtocol().isMasterConnection()
&& !listener.getCurrentProtocol().checkIfMaster()) {
//connection state has changed !
return handleFailOver(queryException, method, args, listener.getCurrentProtocol());
}
}
throw e.getTargetException();
Expand Down Expand Up @@ -262,4 +274,8 @@ public void reconnect() throws SQLException {
public Listener getListener() {
return listener;
}

private static void addExceptionHost(QueryException exception, Protocol protocol) {
exception.setMessage(exception.getMessage() + "\non " + protocol.getHostAddress().toString() + ",master=" + protocol.isMasterConnection() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ public void connect() throws QueryException {
} else {
connect(null, 3306);
}
return;
} catch (IOException e) {
throw new QueryException("Could not connect to " + currentHost + "." + e.getMessage(), -1, CONNECTION_EXCEPTION, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public enum DefaultOptions {
PREPSTMTCACHESQLLIMIT("prepStmtCacheSqlLimit", new Integer(2048), new Integer(0), Integer.MAX_VALUE, "1.3.0"),

/**
* when in high availalability, and switching to a read-only host, assure that this host is in read-only mode by
* when in high availability, and switching to a read-only host, assure that this host is in read-only mode by
* setting session read-only.
* default to false
*/
Expand Down

0 comments on commit 7e4650d

Please sign in to comment.