Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jdbc-driver] Synchronize access to the handlers map #93

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions jdbc-driver/src/main/java/acolyte/jdbc/Driver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package acolyte.jdbc;

import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.HashMap;

Expand Down Expand Up @@ -31,8 +33,8 @@ public final class Driver implements java.sql.Driver {
/**
* Handler registry
*/
public static final HashMap<String,ConnectionHandler> handlers =
new HashMap<String,ConnectionHandler>();
public static final Map<String,ConnectionHandler> handlers =
Collections.synchronizedMap(new HashMap<String,ConnectionHandler>());

// --- Shared ---

Expand Down
24 changes: 23 additions & 1 deletion jdbc-driver/src/test/scala/acolyte/jdbc/DriverSpec.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package acolyte.jdbc

import java.util.ServiceLoader
import java.util.{ ServiceLoader, UUID }
import java.util.concurrent.Executors

import java.sql.{ Driver ⇒ JdbcDriver, DriverManager }

import scala.reflect.ClassTag
import scala.concurrent.{ Await, ExecutionContext, ExecutionContextExecutorService, Future }

import org.specs2.mutable.Specification

Expand Down Expand Up @@ -139,6 +141,26 @@ object DriverSpec extends Specification with DriverUtils with DriverFixtures {
getStatementHandler aka "handler" mustEqual h

}

"handle multi-threaded access" in {
import scala.concurrent.duration._

implicit val ec: ExecutionContextExecutorService =
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(16))

val futures =
(1 to 1000).map { _ =>
Future {
val handlerId = UUID.randomUUID().toString
acolyte.jdbc.Driver.register(handlerId, new CompositeHandler())
handlerId
}.map { handlerId =>
acolyte.jdbc.Driver.handlers.get(handlerId) mustNotEqual null
}
}

Await.result(Future.sequence(futures), 5.seconds)
}
}
}

Expand Down