Skip to content

Commit

Permalink
[jdbc-driver] Synchronize access to the handlers map (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilgaly authored and cchantep committed Dec 6, 2018
1 parent 231155b commit a383dff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
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

0 comments on commit a383dff

Please sign in to comment.