Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-293] permit pipe without hostname (url string like "jdbc:mariad…
…b:///db?pipe=namepipe") as documented
  • Loading branch information
rusher committed Aug 24, 2016
1 parent 18fac06 commit d370cac
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 114 deletions.
27 changes: 15 additions & 12 deletions src/main/java/org/mariadb/jdbc/MariaDbConnection.java
Expand Up @@ -405,18 +405,21 @@ public PreparedStatement internalPrepareStatement(final String sql, final int re
checkConnection();

boolean canUsePrepareStatement = false;
String cleanSql = sql.toUpperCase().trim();
if (options.rewriteBatchedStatements || !options.useServerPrepStmts) {
//in case of CALL statement, handling INOUT parameter is better with Prepare protocol
canUsePrepareStatement = cleanSql.contains("CALL");
} else if (options.useServerPrepStmts && sql != null) {
canUsePrepareStatement = (cleanSql.contains("SELECT")
|| cleanSql.contains("CALL")
|| cleanSql.contains("UPDATE")
|| cleanSql.contains("INSERT")
|| cleanSql.contains("DELETE")
|| cleanSql.contains("REPLACE")
|| cleanSql.contains("DO"));

if (sql != null) {
String cleanSql = sql.toUpperCase().trim();
if (!options.useServerPrepStmts) {
//in case of CALL statement, handling INOUT parameter is better with Prepare protocol
canUsePrepareStatement = cleanSql.contains("CALL");
} else if (options.useServerPrepStmts) {
canUsePrepareStatement = (cleanSql.contains("SELECT")
|| cleanSql.contains("CALL")
|| cleanSql.contains("UPDATE")
|| cleanSql.contains("INSERT")
|| cleanSql.contains("DELETE")
|| cleanSql.contains("REPLACE")
|| cleanSql.contains("DO"));
}
}

if (canUsePrepareStatement) {
Expand Down
Expand Up @@ -322,7 +322,11 @@ public void connect() throws QueryException {
close();
}
try {
connect(currentHost.host, currentHost.port);
if (currentHost != null) {
connect(currentHost.host, currentHost.port);
} else {
connect(null, 3306);
}
return;
} catch (IOException e) {
throw new QueryException("Could not connect to " + currentHost + "." + e.getMessage(), -1,
Expand All @@ -349,7 +353,7 @@ private void connect(String host, int port) throws QueryException, IOException {
}

if (!socket.isConnected()) {
InetSocketAddress sockAddr = new InetSocketAddress(host, port);
InetSocketAddress sockAddr = urlParser.getOptions().pipe == null ? new InetSocketAddress(host, port) : null;
if (options.connectTimeout != null) {
socket.connect(sockAddr, options.connectTimeout);
} else {
Expand Down Expand Up @@ -701,7 +705,7 @@ public UrlParser getUrlParser() {
* @return is master flag
*/
public boolean isMasterConnection() {
return ParameterConstant.TYPE_MASTER.equals(currentHost.type);
return currentHost == null ? true : ParameterConstant.TYPE_MASTER.equals(currentHost.type);
}

public boolean mustBeMasterConnection() {
Expand Down Expand Up @@ -785,7 +789,7 @@ public void setHostAddress(HostAddress host) {
}

public String getHost() {
return currentHost.host;
return (currentHost == null) ? null : currentHost.host;
}

public FailoverProxy getProxy() {
Expand All @@ -797,7 +801,7 @@ public void setProxy(FailoverProxy proxy) {
}

public int getPort() {
return currentHost.port;
return (currentHost == null) ? 3306 : currentHost.port;
}

public String getDatabase() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/mariadb/jdbc/internal/util/DefaultOptions.java
Expand Up @@ -588,6 +588,18 @@ private static Options parse(HaMode haMode, Properties properties, Options optio
//only for jws, so never thrown
throw new IllegalArgumentException("Security too restrictive : " + s.getMessage());
}

//not compatible options

//disable use server prepare id using client rewrite
if (options.rewriteBatchedStatements) {
options.useServerPrepStmts = false;
options.cachePrepStmts = false;
}
//pipe cannot use read and write socket simultaneously
if (options.pipe != null) options.useBatchMultiSend = false;


return options;
}

Expand Down
14 changes: 9 additions & 5 deletions src/test/java/org/mariadb/jdbc/BaseTest.java
Expand Up @@ -149,9 +149,13 @@ public static void beforeClassBaseTest() throws SQLException {
testSingleHost = Boolean.parseBoolean(System.getProperty("testSingleHost", "true"));
if (testSingleHost) {
urlParser = UrlParser.parse(url);

hostname = urlParser.getHostAddresses().get(0).host;
port = urlParser.getHostAddresses().get(0).port;
if (urlParser.getHostAddresses().size() > 0) {
hostname = urlParser.getHostAddresses().get(0).host;
port = urlParser.getHostAddresses().get(0).port;
} else {
hostname = null;
port = 3306;
}
database = urlParser.getDatabase();
username = urlParser.getUsername();
password = urlParser.getPassword();
Expand All @@ -164,7 +168,7 @@ public static void beforeClassBaseTest() throws SQLException {


private static void setUri() {
connU = "jdbc:mysql://" + hostname + ":" + port + "/" + database;
connU = "jdbc:mysql://" + ((hostname == null) ? "localhost" : hostname) + ":" + port + "/" + database;
connUri = connU + "?user=" + username
+ (password != null && !"".equals(password) ? "&password=" + password : "")
+ (parameters != null ? parameters : "");
Expand Down Expand Up @@ -354,7 +358,7 @@ protected Connection setConnection(String parameters) throws SQLException {
}

protected Connection setConnection(String additionnallParameters, String database) throws SQLException {
String connU = "jdbc:mysql://" + hostname + ":" + port + "/" + database;
String connU = "jdbc:mysql://" + ((hostname == null) ? "localhost" : hostname) + ":" + port + "/" + database;
String connUri = connU + "?user=" + username
+ (password != null && !"".equals(password) ? "&password=" + password : "")
+ (parameters != null ? parameters : "");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/mariadb/jdbc/ConnectionTest.java
Expand Up @@ -33,7 +33,7 @@ public static void initClass() throws SQLException {
@Test
public void testAccessDeniedErrorCode() throws SQLException {
try {
DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database + "?user=foo");
DriverManager.getConnection("jdbc:mysql://" + ((hostname != null) ? hostname : "localhost") + ":" + port + "/" + database + "?user=foo");
Assert.fail();
} catch (SQLException e) {
if (1524 == e.getErrorCode()) {
Expand Down
30 changes: 13 additions & 17 deletions src/test/java/org/mariadb/jdbc/DataSourceTest.java
Expand Up @@ -27,7 +27,7 @@ public static void beforeClassDataSourceTest() {

@Test
public void testDataSource() throws SQLException {
MariaDbDataSource ds = new MariaDbDataSource(hostname, port, database);
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port, database);
Connection connection = ds.getConnection(username, password);
try {
assertEquals(connection.isValid(0), true);
Expand All @@ -38,7 +38,7 @@ public void testDataSource() throws SQLException {

@Test
public void testDataSource2() throws SQLException {
MariaDbDataSource ds = new MariaDbDataSource(hostname, port, database);
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port, database);
Connection connection = ds.getConnection(username, password);
try {
assertEquals(connection.isValid(0), true);
Expand All @@ -52,7 +52,7 @@ public void testDataSourceEmpty() throws SQLException {
MariaDbDataSource ds = new MariaDbDataSource();
ds.setDatabaseName(database);
ds.setPort(port);
ds.setServerName(hostname);
ds.setServerName(hostname == null ? "localhost" : hostname);
Connection connection = ds.getConnection(username, password);
try {
assertEquals(connection.isValid(0), true);
Expand All @@ -68,7 +68,7 @@ public void testDataSourceEmpty() throws SQLException {
*/
@Test
public void setDatabaseNameTest() throws SQLException {
MariaDbDataSource ds = new MariaDbDataSource(hostname, port, database);
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port, database);
Connection connection = ds.getConnection(username, password);
connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test2");
ds.setDatabaseName("test2");
Expand All @@ -87,7 +87,7 @@ public void setDatabaseNameTest() throws SQLException {
@Test
public void setServerNameTest() throws SQLException {
Assume.assumeTrue(connectToIP != null);
MariaDbDataSource ds = new MariaDbDataSource(hostname, port, database);
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port, database);
Connection connection = ds.getConnection(username, password);
ds.setServerName(connectToIP);
connection = ds.getConnection(username, password);
Expand All @@ -103,7 +103,7 @@ public void setServerNameTest() throws SQLException {
public void setPortTest() throws SQLException {


MariaDbDataSource ds = new MariaDbDataSource(hostname, port, database);
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port, database);
Connection connection2 = ds.getConnection(username, password);
//delete blacklist, because can failover on 3306 is filled
assureBlackList(connection2);
Expand All @@ -127,25 +127,21 @@ public void setPortTest() throws SQLException {
*/
@Test
public void setPropertiesTest() throws SQLException {
MariaDbDataSource ds = new MariaDbDataSource(hostname, port, database);
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port, database);
ds.setProperties("sessionVariables=sql_mode='PIPES_AS_CONCAT'");
Connection connection = null;
try {
connection = ds.getConnection(username, password);
try (Connection connection = ds.getConnection(username, password)) {
ResultSet rs = connection.createStatement().executeQuery("SELECT @@sql_mode");
if (rs.next()) {
assertEquals("PIPES_AS_CONCAT", rs.getString(1));
ds.setUrl(connUri + "&sessionVariables=sql_mode='ALLOW_INVALID_DATES'");
connection = ds.getConnection();
rs = connection.createStatement().executeQuery("SELECT @@sql_mode");
assertTrue(rs.next());
assertEquals("ALLOW_INVALID_DATES", rs.getString(1));
connection.close();
try (Connection connection2 = ds.getConnection()) {
rs = connection2.createStatement().executeQuery("SELECT @@sql_mode");
assertTrue(rs.next());
assertEquals("ALLOW_INVALID_DATES", rs.getString(1));
}
} else {
fail();
}
} finally {
connection.close();
}

}
Expand Down
9 changes: 3 additions & 6 deletions src/test/java/org/mariadb/jdbc/DriverTest.java
Expand Up @@ -392,12 +392,11 @@ public void testPreparedWithNull() throws SQLException {

@Test
public void connectFailover() throws SQLException {
Assume.assumeTrue(hostname != null);
String hosts = hostname + ":" + port + "," + hostname + ":" + (port + 1);
String url = "jdbc:mysql://" + hosts + "/" + database + "?user=" + username;
url += (password != null && !"".equals(password) ? "&password=" + password : "");
Connection connection = null;
try {
connection = openNewConnection(url);
try (Connection connection = openNewConnection(url)) {
MariaDbConnection my = (MariaDbConnection) connection;
assertTrue(my.getPort() == port);
ResultSet rs = connection.createStatement().executeQuery("select 1");
Expand All @@ -406,8 +405,6 @@ public void connectFailover() throws SQLException {
} else {
fail();
}
} finally {
connection.close();
}
}

Expand Down Expand Up @@ -1098,7 +1095,7 @@ public void namedPipeWithoutHost() throws Exception {
String namedPipeName = rs.getString(2);
//skip test if no namedPipeName was obtained because then we do not use a socket connection
Assume.assumeTrue(namedPipeName != null);
try (Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost/testj?user="
try (Connection connection = DriverManager.getConnection("jdbc:mariadb:///testj?user="
+ username + "&pipe=" + namedPipeName)) {
Statement stmt = connection.createStatement();
rs = stmt.executeQuery("SELECT 1");
Expand Down
64 changes: 0 additions & 64 deletions src/test/java/org/mariadb/jdbc/FetchSizeTest.java
Expand Up @@ -100,70 +100,6 @@ public void fetchSizeErrorWhileFetchTest() throws SQLException {
}
}

@Test
public void fetchSizeSpeedTest() throws SQLException {
prepare100record("fetchSizeTest2");

Statement stmt = sharedConnection.createStatement();

//test limit
final long start = System.nanoTime();
ResultSet resultSet = stmt.executeQuery("SELECT test FROM fetchSizeTest2 LIMIT 2");
assertTrue(resultSet.next());
assertEquals("0", resultSet.getString(1));
assertTrue(resultSet.next());
assertEquals("1", resultSet.getString(1));
long resultTimeLimit2 = System.nanoTime() - start;

stmt.setFetchSize(0);
//execute another query, so skipping result are not on next query
stmt.executeQuery("SELECT 1");
long resultTimeLimit2WithSkip = System.nanoTime() - start;
System.out.println(resultTimeLimit2WithSkip + " / " + resultTimeLimit2);

//test setMaxRows(2)
final long start2 = System.nanoTime();
stmt.setMaxRows(2);
test2firstResult(stmt);
long resultTimeFetchMaxRow = System.nanoTime() - start2;
stmt.setMaxRows(0);
stmt.executeQuery("SELECT 1");
long resultTimeFetchMaxRowWithSkip = System.nanoTime() - start2;
System.out.println(resultTimeFetchMaxRowWithSkip + " / " + resultTimeFetchMaxRow);

//test fetch size 2
stmt.setFetchSize(2);
final long start3 = System.nanoTime();
test2firstResult(stmt);
long resultTimeFetch2 = System.nanoTime() - start3;
stmt.setFetchSize(0);
stmt.executeQuery("SELECT 1");
long resultTimeFetch2WithSkip = System.nanoTime() - start3;
System.out.println(resultTimeFetch2WithSkip + " / " + resultTimeFetch2);

//test fetch all
final long start4 = System.nanoTime();
test2firstResult(stmt);
long resultTimeFetchAll = System.nanoTime() - start4;
stmt.executeQuery("SELECT 1");
long resultTimeFetchAllWithSkip = System.nanoTime() - start4;
System.out.println(resultTimeFetchAllWithSkip + " / " + resultTimeFetchAll);

//normally this is right, but since server is caching rows, that may not be always the case.
// assertTrue(resultTimeFetchMaxRowWithSkip > resultTimeLimit2WithSkip);
// assertTrue(resultTimeFetch2WithSkip > resultTimeFetchMaxRowWithSkip);
// assertTrue(resultTimeFetchAllWithSkip > resultTimeFetch2WithSkip);

}

private void test2firstResult(Statement statement) throws SQLException {
ResultSet resultSet = statement.executeQuery("SELECT test FROM fetchSizeTest2");
assertTrue(resultSet.next());
assertEquals("0", resultSet.getString(1));
assertTrue(resultSet.next());
assertEquals("1", resultSet.getString(1));

}

private void prepare100record(String tableName) throws SQLException {
PreparedStatement pstmt = sharedConnection.prepareStatement("INSERT INTO " + tableName + " (test) values (?)");
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/mariadb/jdbc/PooledConnectionTest.java
Expand Up @@ -12,7 +12,7 @@
public class PooledConnectionTest extends BaseTest {
@Test
public void testPooledConnectionClosed() throws Exception {
ConnectionPoolDataSource ds = new MariaDbDataSource(hostname, port, database);
ConnectionPoolDataSource ds = new MariaDbDataSource(hostname != null ? hostname : "localhost", port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
Connection connection = pc.getConnection();
MyEventListener listener = new MyEventListener();
Expand All @@ -36,7 +36,7 @@ public void testPooledConnectionClosed() throws Exception {

@Test
public void testPooledConnectionException() throws Exception {
ConnectionPoolDataSource ds = new MariaDbDataSource(hostname, port, database);
ConnectionPoolDataSource ds = new MariaDbDataSource(hostname != null ? hostname : "localhost", port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
MyEventListener listener = new MyEventListener();
pc.addConnectionEventListener(listener);
Expand All @@ -63,7 +63,7 @@ public void testPooledConnectionException() throws Exception {

@Test
public void testPooledConnectionStatementError() throws Exception {
ConnectionPoolDataSource ds = new MariaDbDataSource(hostname, port, database);
ConnectionPoolDataSource ds = new MariaDbDataSource(hostname != null ? hostname : "localhost", port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
MyEventListener listener = new MyEventListener();
pc.addStatementEventListener(listener);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/mariadb/jdbc/failover/OldFailoverTest.java
Expand Up @@ -16,7 +16,8 @@ public class OldFailoverTest extends BaseTest {
*/
@Test
public void isOldConfigurationValid() throws Exception {
String falseUrl = "jdbc:mysql://localhost:1111," + hostname + ":" + port + "/" + database + "?user=" + username
String falseUrl = "jdbc:mysql://localhost:1111," + ((hostname == null) ? "localhost" : hostname) + ":"
+ port + "/" + database + "?user=" + username
+ (password != null && !"".equals(password) ? "&password=" + password : "")
+ (parameters != null ? parameters : "");

Expand Down

0 comments on commit d370cac

Please sign in to comment.