Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRILL-2127: make JDBC tests re-use bit instances; reset cached bits o…
…n failure
- Loading branch information
1 parent
e46d68b
commit 4ed0a8d
Showing
15 changed files
with
687 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
exec/jdbc/src/test/java/org/apache/drill/jdbc/CachingConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
/** | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you under the Apache License, Version 2.0 (the | |||
* "License"); you may not use this file except in compliance | |||
* with the License. You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package org.apache.drill.jdbc; | |||
|
|||
import java.sql.SQLException; | |||
|
|||
/** | |||
* A connection factory that caches connections. | |||
*/ | |||
public interface CachingConnectionFactory extends ConnectionFactory { | |||
|
|||
/** | |||
* Closes all active connections in the cache. | |||
*/ | |||
void close() throws SQLException; | |||
} |
33 changes: 33 additions & 0 deletions
33
exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
/** | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you under the Apache License, Version 2.0 (the | |||
* "License"); you may not use this file except in compliance | |||
* with the License. You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package org.apache.drill.jdbc; | |||
|
|||
import java.sql.Connection; | |||
|
|||
/** | |||
* A factory used to create {@link java.sql.Connection} instances. | |||
*/ | |||
public interface ConnectionFactory { | |||
/** | |||
* Creates a new {@link java.sql.Connection} based on the given {@link org.apache.drill.jdbc.ConnectionInfo info} | |||
* | |||
* @param info connection parameters | |||
* @throws Exception if factory fails to create a connection. | |||
*/ | |||
Connection createConnection(ConnectionInfo info) throws Exception; | |||
} |
81 changes: 81 additions & 0 deletions
81
exec/jdbc/src/test/java/org/apache/drill/jdbc/ConnectionInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,81 @@ | |||
/** | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you under the Apache License, Version 2.0 (the | |||
* "License"); you may not use this file except in compliance | |||
* with the License. You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package org.apache.drill.jdbc; | |||
|
|||
import java.util.Map; | |||
import java.util.Properties; | |||
|
|||
import com.google.common.base.Objects; | |||
import com.google.common.base.Preconditions; | |||
import com.google.common.collect.ImmutableMap; | |||
|
|||
/** | |||
* An immutable bag of parameters that describes a {@link java.sql.Connection}. | |||
*/ | |||
public class ConnectionInfo { | |||
private final String url; | |||
private final Map<Object, Object> params; | |||
|
|||
public ConnectionInfo(String url, Properties params) { | |||
this(url, ImmutableMap.copyOf(params)); | |||
} | |||
|
|||
public ConnectionInfo(String url, ImmutableMap<Object, Object> params) { | |||
this.url = Preconditions.checkNotNull(url, "URL cannot be null"); | |||
this.params = params; | |||
} | |||
|
|||
/** | |||
* Returns connection url. | |||
*/ | |||
public String getUrl() { | |||
return url; | |||
} | |||
|
|||
/** | |||
* Returns connection parameters. | |||
*/ | |||
public Map<Object, Object> getParameters() { | |||
return params; | |||
} | |||
|
|||
/** | |||
* Creates a new {@link java.util.Properties} instance from underlying parameters. | |||
*/ | |||
public Properties getParamsAsProperties() { | |||
final Properties props = new Properties(); | |||
for (Object key:params.keySet()) { | |||
props.put(key, params.get(key)); | |||
} | |||
return props; | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
return Objects.hashCode(url, params); | |||
} | |||
|
|||
@Override | |||
public boolean equals(Object obj) { | |||
if (obj instanceof ConnectionInfo) { | |||
final ConnectionInfo info = ConnectionInfo.class.cast(obj); | |||
return Objects.equal(url, info.getUrl()) && Objects.equal(params, info.getParameters()); | |||
} | |||
return false; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
exec/jdbc/src/test/java/org/apache/drill/jdbc/MultiConnectionCachingFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,65 @@ | |||
/** | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you under the Apache License, Version 2.0 (the | |||
* "License"); you may not use this file except in compliance | |||
* with the License. You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package org.apache.drill.jdbc; | |||
|
|||
import java.sql.Connection; | |||
import java.sql.SQLException; | |||
import java.util.Map; | |||
|
|||
import com.google.common.collect.Maps; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
/** | |||
* A connection factory that caches connections based on given {@link org.apache.drill.jdbc.ConnectionInfo}. | |||
*/ | |||
public class MultiConnectionCachingFactory implements CachingConnectionFactory { | |||
private static final Logger logger = LoggerFactory.getLogger(MultiConnectionCachingFactory.class); | |||
|
|||
private final ConnectionFactory delegate; | |||
private final Map<ConnectionInfo, Connection> cache = Maps.newHashMap(); | |||
|
|||
public MultiConnectionCachingFactory(ConnectionFactory delegate) { | |||
this.delegate = delegate; | |||
} | |||
|
|||
/** | |||
* Creates a {@link org.apache.drill.jdbc.NonClosableConnection connection} and caches it. | |||
* | |||
* The returned {@link org.apache.drill.jdbc.NonClosableConnection connection} does not support | |||
* {@link java.sql.Connection#close()}. Consumer must call {#close} to close the cached connections. | |||
*/ | |||
@Override | |||
public Connection createConnection(ConnectionInfo info) throws Exception { | |||
Connection conn = cache.get(info); | |||
if (conn == null) { | |||
conn = delegate.createConnection(info); | |||
cache.put(info, conn); | |||
} | |||
return new NonClosableConnection(conn); | |||
} | |||
|
|||
/** | |||
* Closes all active connections in the cache. | |||
*/ | |||
public void close() throws SQLException { | |||
for (Connection conn:cache.values()) { | |||
conn.close(); | |||
} | |||
} | |||
} |
Oops, something went wrong.