Skip to content

Commit

Permalink
[CONJ-366] save state of last successful authentication without auth …
Browse files Browse the repository at this point in the history
…to avoid one server exchange when pipelining
  • Loading branch information
rusher committed Apr 6, 2017
1 parent 01fe81a commit 4b723ea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
15 changes: 5 additions & 10 deletions src/main/java/org/mariadb/jdbc/UrlParser.java
Expand Up @@ -345,22 +345,17 @@ public HaMode getHaMode() {

@Override
public boolean equals(Object parser) {
if (this == parser) {
return true;
}
if (!(parser instanceof UrlParser)) {
return false;
}
if (this == parser) return true;
if (!(parser instanceof UrlParser)) return false;

UrlParser urlParser = (UrlParser) parser;

if (getDatabase() != null ? !getDatabase().equals(urlParser.getDatabase()) : urlParser.getDatabase() != null) {
if (initialUrl != null ? !initialUrl.equals(urlParser.getInitialUrl()) : urlParser.getInitialUrl() != null) {
return false;
}
if (getOptions() != null ? !getOptions().equals(urlParser.getOptions()) : urlParser.getOptions() != null) {
if (getUsername() != null ? !getUsername().equals(urlParser.getUsername()) : urlParser.getUsername() != null) {
return false;
}
return addresses != null ? addresses.equals(urlParser.addresses) : urlParser.addresses == null && getHaMode() == urlParser.getHaMode();
return getPassword() != null ? getPassword().equals(urlParser.getPassword()) : urlParser.getPassword() == null;

}

Expand Down
Expand Up @@ -135,7 +135,7 @@ public abstract class AbstractConnectProtocol implements Protocol {
private int patchVersion;
protected Map<String, String> serverData;
private TimeZone timeZone;
private static boolean expectAuthPlugin = true;
private static UrlParser expectAuthUrlParser = null;

/**
* Get a protocol instance.
Expand Down Expand Up @@ -342,7 +342,7 @@ public void connect() throws SQLException {
try {
connect((currentHost != null) ? currentHost.host : null,
(currentHost != null) ? currentHost.port : 3306,
expectAuthPlugin);
!urlParser.equals(expectAuthUrlParser));
return;
} catch (OptimisticAuthPluginException opt) {
try {
Expand Down Expand Up @@ -718,7 +718,7 @@ private void authentication(byte exchangeCharset, long clientCapabilities, byte[

if ((buffer.getByteAt(0) & 0xFF) == 0xFE) {
if (!expectAuthPlugin && options.usePipelineAuth) {
this.expectAuthPlugin = true;
this.expectAuthUrlParser = null;
throw new OptimisticAuthPluginException("doesn't expect Authentication Plugin");
}

Expand Down Expand Up @@ -762,7 +762,8 @@ private void authentication(byte exchangeCharset, long clientCapabilities, byte[
}
throw new SQLException("Could not connect: " + errorPacket.getMessage(), errorPacket.getSqlState(), errorPacket.getErrorNumber());
}
this.expectAuthPlugin = false;
//if capability CONNECT_WITH_DB is not set, then server send a plugin auth 0xFE packet.
if (database != null && !options.createDatabaseIfNotExist) this.expectAuthUrlParser = urlParser;
serverStatus = new OkPacket(buffer).getServerStatus();
}

Expand Down Expand Up @@ -994,7 +995,7 @@ public void connectWithoutProxy() throws SQLException {
try {
try {

connect(null, 0, expectAuthPlugin);
connect(null, 0, !urlParser.equals(expectAuthUrlParser));
return;

} catch (OptimisticAuthPluginException opt) {
Expand Down Expand Up @@ -1025,7 +1026,7 @@ public void connectWithoutProxy() throws SQLException {
try {
try {

connect(currentHost.host, currentHost.port, expectAuthPlugin);
connect(currentHost.host, currentHost.port, !urlParser.equals(expectAuthUrlParser));
return;

} catch (OptimisticAuthPluginException opt) {
Expand Down
11 changes: 9 additions & 2 deletions src/test/java/org/mariadb/jdbc/DriverTest.java
Expand Up @@ -350,7 +350,11 @@ public void testConnectorJurl() throws SQLException {
public void testAliasReplication() throws SQLException {
UrlParser url = UrlParser.parse("jdbc:mysql:replication://localhost/test");
UrlParser url2 = UrlParser.parse("jdbc:mariadb:replication://localhost/test");
assertEquals(url, url2);
assertEquals(url.getDatabase(), url2.getDatabase());
assertEquals(url.getOptions(), url2.getOptions());
assertEquals(url.getHostAddresses(), url2.getHostAddresses());
assertEquals(url.getHaMode(), url2.getHaMode());

}

@Test
Expand All @@ -362,7 +366,10 @@ public void testAliasDataSource() throws SQLException {

urlParser.parseUrl("jdbc:mysql:replication://localhost/test");
urlParser2.parseUrl("jdbc:mariadb:replication://localhost/test");
assertEquals(urlParser, urlParser2);
assertEquals(urlParser.getDatabase(), urlParser2.getDatabase());
assertEquals(urlParser.getOptions(), urlParser2.getOptions());
assertEquals(urlParser.getHostAddresses(), urlParser2.getHostAddresses());
assertEquals(urlParser.getHaMode(), urlParser2.getHaMode());
}


Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/mariadb/jdbc/JdbcParserTest.java
Expand Up @@ -14,7 +14,10 @@ public class JdbcParserTest {
public void testMariaAlias() throws Throwable {
UrlParser jdbc = UrlParser.parse("jdbc:mariadb://localhost/test");
UrlParser jdbc2 = UrlParser.parse("jdbc:mysql://localhost/test");
assertEquals(jdbc, jdbc2);
assertEquals(jdbc.getDatabase(), jdbc2.getDatabase());
assertEquals(jdbc.getOptions(), jdbc2.getOptions());
assertEquals(jdbc.getHostAddresses(), jdbc2.getHostAddresses());
assertEquals(jdbc.getHaMode(), jdbc2.getHaMode());
}

@Test
Expand Down

0 comments on commit 4b723ea

Please sign in to comment.