Skip to content
Permalink
Browse files

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

  • Loading branch information...
guilgaly authored and cchantep committed Dec 6, 2018
1 parent 231155b commit a383dff20fadc08ec9306f2f1f24b2a7e0047449
@@ -1,5 +1,7 @@
package acolyte.jdbc;

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

@@ -31,8 +33,8 @@
/**
* 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 ---

@@ -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

@@ -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)
}
}
}

0 comments on commit a383dff

Please sign in to comment.
You can’t perform that action at this time.