Skip to content

Commit

Permalink
[CONJ-197] log and error message improved
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Jun 28, 2016
1 parent 02b0468 commit c4189cb
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class MariaDbServerPreparedStatement extends AbstractMariaDbPrepareStatem
public MariaDbServerPreparedStatement(MariaDbConnection connection, String sql, int resultSetScrollType, boolean forcePrepare)
throws SQLException {
super(connection, resultSetScrollType);
this.sql = Utils.nativeSql(sql, connection.noBackslashEscapes);;
this.sql = Utils.nativeSql(sql, connection.noBackslashEscapes);

useFractionalSeconds = options.useFractionalSeconds;
returnTableAlias = options.useOldAliasMetadataBehavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ public Set<HostAddress> getBlacklistKeys() {
* @return a HandleErrorResult object to indicate if query has been relaunched, and the exception if not
* @throws Throwable when method and parameters does not exist.
*/
public HandleErrorResult handleFailover(Method method, Object[] args, Protocol protocol) throws Throwable {
public HandleErrorResult handleFailover(QueryException qe, Method method, Object[] args, Protocol protocol) throws Throwable {
if (isExplicitClosed()) {
throw new QueryException("Connection has been closed !");
}
if (setMasterHostFail()) {
logger.warn("SQL Primary node [" + this.currentProtocol.getHostAddress().toString()
+ ", conn " + this.currentProtocol.getServerThreadId()
+ " ] connection fail ");
+ " ] connection fail. Reason : " + qe.getMessage());
addToBlacklist(currentProtocol.getHostAddress());
}
return primaryFail(method, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,24 @@ protected AbstractMastersSlavesListener(UrlParser urlParser) {
* @return HandleErrorResult object to indicate if query has finally been relaunched or exception if not.
* @throws Throwable if method with parameters doesn't exist
*/
public HandleErrorResult handleFailover(Method method, Object[] args, Protocol protocol) throws Throwable {
public HandleErrorResult handleFailover(QueryException qe, Method method, Object[] args, Protocol protocol) throws Throwable {
if (isExplicitClosed()) {
throw new QueryException("Connection has been closed !");
}
if (protocol.mustBeMasterConnection()) {
if (setMasterHostFail()) {
logger.warn("SQL Primary node [" + this.currentProtocol.getHostAddress().toString()
+ ", conn " + this.currentProtocol.getServerThreadId()
+ " ] connection fail ");
+ " ] connection fail. Reason : " + qe.getMessage());

addToBlacklist(protocol.getHostAddress());
}
return primaryFail(method, args);
} else {
if (setSecondaryHostFail()) {
logger.warn("SQL secondary node [" + this.currentProtocol.getHostAddress().toString()
+ ", conn " + this.currentProtocol.getServerThreadId()
+ " ] connection fail ");
+ " ] connection fail. Reason : " + qe.getMessage());
addToBlacklist(protocol.getHostAddress());
}
return secondaryFail(method, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private Object handleFailOver(QueryException qe, Method method, Object[] args, P
failHostAddress = protocol.getHostAddress();
failIsMaster = protocol.isMasterConnection();
}
HandleErrorResult handleErrorResult = listener.handleFailover(method, args, protocol);
HandleErrorResult handleErrorResult = listener.handleFailover(qe, method, args, protocol);
if (handleErrorResult.mustThrowError) {
listener.throwFailoverMessage(failHostAddress, failIsMaster, qe, handleErrorResult.isReconnected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public interface Listener {

Object invoke(Method method, Object[] args) throws Throwable;

HandleErrorResult handleFailover(Method method, Object[] args, Protocol protocol) throws Throwable;
HandleErrorResult handleFailover(QueryException qe, Method method, Object[] args, Protocol protocol) throws Throwable;

void foundActiveMaster(Protocol protocol) throws QueryException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ private String logQuery(String methodName, Object[] args, Object returnObj) {
for (int i = 0; i < params.length; i++) {
sql += params[i].toString() + ",";
if (maxQuerySizeToLog > 0 && sql.length() > maxQuerySizeToLog) break;
sql = sql.substring(0, sql.length() - 1);
}
sql += "]";
sql = sql.substring(0, sql.length() - 1) + "]";
}
break;
case "prepareAndExecutesComMulti":
Expand All @@ -200,8 +199,8 @@ private String logQuery(String methodName, Object[] args, Object returnObj) {
sql += "[";
for (int i = 0; i < serverPrepareResult.getParameters().length; i++) {
sql += parameters[i].toString() + ",";
sql = sql.substring(0, sql.length() - 1);
}
sql = sql.substring(0, sql.length() - 1);

if (maxQuerySizeToLog > 0 && sql.length() > maxQuerySizeToLog) {
break;
Expand All @@ -217,10 +216,10 @@ private String logQuery(String methodName, Object[] args, Object returnObj) {
if (paramHolder.length > 0) {
sql += ", parameters [";
for (int i = 0; i < ((ServerPrepareResult) args[1]).getParameters().length; i++) {
sql += paramHolder[i].toString();
sql += paramHolder[i].toString() + ",";
if (maxQuerySizeToLog > 0 && sql.length() > maxQuerySizeToLog) break;
}
sql += "]";
sql = sql.substring(0, sql.length() - 1) + "]";
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS

public class StringParameter implements ParameterHolder, Cloneable {
public static Field charsFieldValue;

static {
try {
charsFieldValue = String.class.getDeclaredField("value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,9 @@ public void executePreparedQuery(boolean mustExecuteOnMaster, ServerPrepareResul
if (serverPrepareResult.getParameters().length > 0) {
sql += ", parameters [";
for (int i = 0; i < serverPrepareResult.getParameters().length; i++) {
if (!parameters[i].isLongData()) sql += parameters[i].toString();
if (!parameters[i].isLongData()) sql += parameters[i].toString() + ",";
}
sql += "]";
sql = sql.substring(0, sql.length() - 1) + "]";
}
if (options.maxQuerySizeToLog != 0 && sql.length() > options.maxQuerySizeToLog - 3) {
qex.setMessage(qex.getMessage() + "\nQuery is: " + sql.substring(0, options.maxQuerySizeToLog - 3) + "...");
Expand Down
67 changes: 36 additions & 31 deletions src/test/java/org/mariadb/jdbc/ErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ErrorMessageTest extends BaseTest {
*/
@BeforeClass()
public static void initClass() throws SQLException {
createTable("testErrorMessage", "id int not null primary key auto_increment, test varchar(10)");
createTable("testErrorMessage", "id int not null primary key auto_increment, test varchar(10), test2 int");
}

@Test
Expand All @@ -28,9 +28,9 @@ public void testSmallRewriteErrorMessage() throws SQLException {
executeBatchWithException(connection);
fail("Must Have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values ('whoua0'), ('whoua1'), "
+ "('whoua2'), ('whoua3'), ('whoua4'), ('whoua5'), ('whoua6'), ('whoua7'), ('whoua8'), ('whoua9'), "
+ "('more than 10 characters to provoc error')"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values ('whoua0', 0), ('whoua1', 1), "
+ "('whoua2', 2), ('whoua3', 3), ('whoua4', 4), ('whoua5', 5), ('whoua6', 6), ('whoua7', 7), "
+ "('whoua8', 8), ('whoua9', 9), ('more than 10 characters to provoc error', 10)"));
}
}

Expand All @@ -41,17 +41,17 @@ public void testSmallMultiErrorMessage() throws SQLException {
executeBatchWithException(connection);
fail("Must Have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values ('whoua0');"
+ "INSERT INTO testErrorMessage(test) values ('whoua1');"
+ "INSERT INTO testErrorMessage(test) values ('whoua2');"
+ "INSERT INTO testErrorMessage(test) values ('whoua3');"
+ "INSERT INTO testErrorMessage(test) values ('whoua4');"
+ "INSERT INTO testErrorMessage(test) values ('whoua5');"
+ "INSERT INTO testErrorMessage(test) values ('whoua6');"
+ "INSERT INTO testErrorMessage(test) values ('whoua7');"
+ "INSERT INTO testErrorMessage(test) values ('whoua8');"
+ "INSERT INTO testErrorMessage(test) values ('whoua9');"
+ "INSERT INTO testErrorMessage(test) values ('more than 10 characters to provoc error')"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values ('whoua0', 0);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua1', 1);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua2', 2);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua3', 3);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua4', 4);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua5', 5);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua6', 6);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua7', 7);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua8', 8);"
+ "INSERT INTO testErrorMessage(test, test2) values ('whoua9', 9);"
+ "INSERT INTO testErrorMessage(test, test2) values ('more than 10 characters to provoc error', 10)"));
}
}

Expand All @@ -62,8 +62,8 @@ public void testSmallPrepareErrorMessage() throws SQLException {
executeBatchWithException(connection);
fail("Must Have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values (?), parameters "
+ "['more than 10 characters to provoc error']"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values (?, ?), "
+ "parameters ['more than 10 characters to provoc error',10]"));
}
}

Expand All @@ -75,12 +75,12 @@ public void testSmallComMultiErrorMessage() throws SQLException {
fail("Must Have thrown error");
} catch (SQLException sqle) {
if (minVersion(10, 2)) {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values (?), parameters "
+ "['whoua0'],['whoua1'],['whoua2'],['whoua3'],['whoua4'],['whoua5'],['whoua6'],['whoua7'],['whoua8'],"
+ "['whoua9'],['more than 10 characters to provoc error']"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values (?, ?), parameters "
+ "['whoua0',0],['whoua1',1],['whoua2',2],['whoua3',3],['whoua4',4],['whoua5',5],['whoua6',6],"
+ "['whoua7',7],['whoua8',8],['whoua9',9],['more than 10 characters to provoc error',10]"));
} else {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values (?), parameters "
+ "['more than 10 characters to provoc error']"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values (?, ?), "
+ "parameters ['more than 10 characters to provoc error',10]"));
}
}
}
Expand All @@ -91,7 +91,7 @@ public void testBigRewriteErrorMessage() throws SQLException {
executeBigBatchWithException(connection);
fail("Must Have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage().contains("('whoua72'), ('whoua73'), ('whoua74'), ('whoua75'), (..."));
assertTrue(sqle.getMessage().contains("('whoua56', 56), ('whoua57', 57), ('whou..."));
}
}

Expand All @@ -102,7 +102,7 @@ public void testBigMultiErrorMessage() throws SQLException {
executeBigBatchWithException(connection);
fail("Must Have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage().contains(";INSERT INTO testErrorMessage(test) values ('whoua18');INSER..."));
assertTrue(sqle.getMessage().contains(";INSERT INTO testErrorMessage(test, test2) values ('whoua15', 15);I..."));
}
}

Expand All @@ -113,8 +113,8 @@ public void testBigPrepareErrorMessage() throws SQLException {
executeBigBatchWithException(connection);
fail("Must Have thrown error");
} catch (SQLException sqle) {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values (?), parameters "
+ "['more than 10 characters to provoc error']"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values (?, ?), parameters "
+ "['more than 10 characters to provoc error',200]"));
}
}

Expand All @@ -126,23 +126,26 @@ public void testBigComMultiErrorMessage() throws SQLException {
fail("Must Have thrown error");
} catch (SQLException sqle) {
if (minVersion(10, 2)) {
assertTrue(sqle.getMessage().contains(",['whoua78'],['whoua79'],['whoua80'],[..."));
assertTrue(sqle.getMessage().contains(",['whoua60',60],['whoua61',61],['whoua62',62],['whoua63',63],['whoua64',64..."));
} else {
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test) values (?), parameters "
+ "['more than 10 characters to provoc error']"));
assertTrue(sqle.getMessage().contains("INSERT INTO testErrorMessage(test, test2) values (?, ?), parameters "
+ "['more than 10 characters to provoc error',200]"));
}
}
}


private void executeBatchWithException(Connection connection) throws SQLException {
connection.createStatement().execute("TRUNCATE testErrorMessage");
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO testErrorMessage(test) values (?)")) {
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO testErrorMessage(test, test2) values (?, ?)")) {
for (int i = 0; i < 10; i++) {
preparedStatement.setString(1, "whoua" + i);
preparedStatement.setInt(2, i);
preparedStatement.addBatch();
}
preparedStatement.setString(1, "more than 10 characters to provoc error");
preparedStatement.setInt(2, 10);

preparedStatement.addBatch();
preparedStatement.executeBatch();
} catch (SQLException e) {
Expand All @@ -154,12 +157,14 @@ private void executeBatchWithException(Connection connection) throws SQLExceptio

private void executeBigBatchWithException(Connection connection) throws SQLException {
connection.createStatement().execute("TRUNCATE testErrorMessage");
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO testErrorMessage(test) values (?)")) {
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO testErrorMessage(test, test2) values (?, ?)")) {
for (int i = 0; i < 200; i++) {
preparedStatement.setString(1, "whoua" + i);
preparedStatement.setInt(2, i);
preparedStatement.addBatch();
}
preparedStatement.setString(1, "more than 10 characters to provoc error");
preparedStatement.setInt(2, 200);
preparedStatement.addBatch();
preparedStatement.executeBatch();
} catch (SQLException e) {
Expand Down

0 comments on commit c4189cb

Please sign in to comment.