From 5c8f4fe25c5da44a0ec5b98b9dab1415b650be5c Mon Sep 17 00:00:00 2001 From: kolzeq Date: Thu, 16 Jul 2015 15:54:28 +0200 Subject: [PATCH] Connections exception now contain SQLState and errorCode [CONJ-166] --- pom.xml | 4 ++-- src/main/java/org/mariadb/jdbc/Version.java | 4 ++-- .../jdbc/internal/common/DefaultOptions.java | 3 ++- .../jdbc/internal/mysql/MySQLProtocol.java | 2 +- .../java/org/mariadb/jdbc/ConnectionTest.java | 18 ++++++++++++++++++ .../jdbc/failover/AuroraFailoverTest.java | 17 +++++++++++++++++ 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index ac6430ef9..752ce4501 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ mariadb-java-client jar mariadb-java-client - 1.2.0-SNAPSHOT + 1.2.0 JDBC driver for MariaDB and MySQL https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/ @@ -88,7 +88,7 @@ org.codehaus.mojo exec-maven-plugin - 1.2.1 + 1.4.0 git diff --git a/src/main/java/org/mariadb/jdbc/Version.java b/src/main/java/org/mariadb/jdbc/Version.java index 1b4106f1e..7b983c1af 100644 --- a/src/main/java/org/mariadb/jdbc/Version.java +++ b/src/main/java/org/mariadb/jdbc/Version.java @@ -1,10 +1,10 @@ package org.mariadb.jdbc; public final class Version { - public static final String version = "1.2.0-SNAPSHOT"; + public static final String version = "1.2.0"; public static final int majorVersion = 1; public static final int minorVersion = 2; public static final int patchVersion = 0; - public static final String qualifier = "SNAPSHOT"; + public static final String qualifier = ""; } \ No newline at end of file diff --git a/src/main/java/org/mariadb/jdbc/internal/common/DefaultOptions.java b/src/main/java/org/mariadb/jdbc/internal/common/DefaultOptions.java index 739318692..0fca4d674 100644 --- a/src/main/java/org/mariadb/jdbc/internal/common/DefaultOptions.java +++ b/src/main/java/org/mariadb/jdbc/internal/common/DefaultOptions.java @@ -364,7 +364,8 @@ private static Options parse(UrlHAMode haMode, String urlParameters, Properties if (pos == -1) { throw new IllegalArgumentException("Invalid connection URL, expected key=value pairs, found " + parameter); } - properties.setProperty(parameter.substring(0, pos), parameter.substring(pos + 1)); + if (!properties.containsKey(parameter.substring(0, pos))) + properties.setProperty(parameter.substring(0, pos), parameter.substring(pos + 1)); } } return parse(haMode, properties, options); diff --git a/src/main/java/org/mariadb/jdbc/internal/mysql/MySQLProtocol.java b/src/main/java/org/mariadb/jdbc/internal/mysql/MySQLProtocol.java index 4119bd754..44500278c 100644 --- a/src/main/java/org/mariadb/jdbc/internal/mysql/MySQLProtocol.java +++ b/src/main/java/org/mariadb/jdbc/internal/mysql/MySQLProtocol.java @@ -450,7 +450,7 @@ void checkErrorPacket(RawPacket rp) throws QueryException{ if (rp.getByteBuffer().get(0) == -1) { ErrorPacket ep = new ErrorPacket(rp); String message = ep.getMessage(); - throw new QueryException("Could not connect: " + message); + throw new QueryException("Could not connect: " + message, ep.getErrorNumber(), ep.getSqlState()); } } diff --git a/src/test/java/org/mariadb/jdbc/ConnectionTest.java b/src/test/java/org/mariadb/jdbc/ConnectionTest.java index 72cce69e6..01128c4a9 100644 --- a/src/test/java/org/mariadb/jdbc/ConnectionTest.java +++ b/src/test/java/org/mariadb/jdbc/ConnectionTest.java @@ -12,6 +12,7 @@ import java.util.Properties; import java.util.concurrent.Executor; +import org.junit.Assert; import org.junit.Assume; import org.junit.Test; import org.mariadb.jdbc.internal.common.query.MySQLQuery; @@ -21,6 +22,23 @@ public class ConnectionTest extends BaseTest { + + /** + * CONJ-166 + * Connection error code must be thrown + * @throws SQLException + */ + @Test + public void testAccessDeniedErrorCode() throws SQLException { + try { + DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database+"?user=foo"); + Assert.fail(); + } catch (SQLException e) { + Assert.assertTrue("28000".equals(e.getSQLState())); + Assert.assertTrue(1045 == e.getErrorCode()); + } + } + /** * CONJ-89 * diff --git a/src/test/java/org/mariadb/jdbc/failover/AuroraFailoverTest.java b/src/test/java/org/mariadb/jdbc/failover/AuroraFailoverTest.java index ee6656df0..dcb3570d1 100644 --- a/src/test/java/org/mariadb/jdbc/failover/AuroraFailoverTest.java +++ b/src/test/java/org/mariadb/jdbc/failover/AuroraFailoverTest.java @@ -531,6 +531,23 @@ public void socketTimeoutTest() throws SQLException { // the connection should not be closed assertTrue(!connection.isClosed()); } + + /** + * CONJ-166 + * Connection error code must be thrown + * @throws SQLException + */ + @Test + public void testAccessDeniedErrorCode() throws SQLException { + try { + DriverManager.getConnection(initialUrl+"&retriesAllDown=1", "foouser", "foopwd"); + Assert.fail(); + } catch (SQLException e) { + Assert.assertTrue("28000".equals(e.getSQLState())); + Assert.assertTrue(1045 == e.getErrorCode()); + } + } + // @Test // public void testFailoverMaster() throws Throwable { // connection = getNewConnection("&validConnectionTimeout=1&secondsBeforeRetryMaster=1&autoReconnect=true", false);