Skip to content

Commit 1625f85

Browse files
committed
propagating max connections when using DataSourceJdbcDataSource
1 parent 19dc04a commit 1625f85

3 files changed

Lines changed: 48 additions & 27 deletions

File tree

slick/src/main/scala/slick/jdbc/JdbcBackend.scala

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ trait JdbcBackend extends RelationalBackend {
8585
/** Create a Database based on a DataSource.
8686
*
8787
* @param ds The DataSource to use.
88-
* @param maxConnection The maximum number of connections that the DataSource can provide. This is necessary to
88+
* @param maxConnections The maximum number of connections that the DataSource can provide. This is necessary to
8989
* prevent deadlocks when scheduling database actions. Use `None` if there is no hard limit.
9090
* @param executor The AsyncExecutor for scheduling database actions.
9191
* @param keepAliveConnection If this is set to true, one extra connection will be opened as soon as the database
@@ -97,18 +97,25 @@ trait JdbcBackend extends RelationalBackend {
9797

9898
/** Create a Database based on the JNDI name of a DataSource.
9999
*
100-
* @param ds The name of the DataSource to use.
101-
* @param maxConnection The maximum number of connections that the DataSource can provide. This is necessary to
100+
* @param name The name of the DataSource to use.
101+
* @param maxConnections The maximum number of connections that the DataSource can provide. This is necessary to
102102
* prevent deadlocks when scheduling database actions. Use `None` if there is no hard limit.
103103
* @param executor The AsyncExecutor for scheduling database actions.
104104
*/
105-
def forName(name: String, maxConnections: Option[Int], executor: AsyncExecutor = null) = new InitialContext().lookup(name) match {
106-
case ds: DataSource => forDataSource(ds, maxConnections, executor match {
107-
case null => AsyncExecutor.default(name)
108-
case e => e
109-
})
110-
case x => throw new SlickException("Expected a DataSource for JNDI name "+name+", but got "+x)
111-
}
105+
def forName(name: String, maxConnections: Option[Int], executor: AsyncExecutor = null) =
106+
new InitialContext().lookup(name) match {
107+
108+
case ds: DataSource =>
109+
val configuredExecutor =
110+
(executor, maxConnections) match {
111+
case (null, Some(maxConnec)) => AsyncExecutor.default(name, maxConnec)
112+
case (null, None) => AsyncExecutor.default(name)
113+
case (e, _) => e
114+
}
115+
forDataSource(ds, maxConnections, configuredExecutor)
116+
117+
case x => throw new SlickException("Expected a DataSource for JNDI name "+name+", but got "+x)
118+
}
112119

113120
/** Create a Database that uses the DriverManager to open new connections. */
114121
def forURL(url: String, user: String = null, password: String = null, prop: Properties = null, driver: String = null,
@@ -287,7 +294,7 @@ trait JdbcBackend extends RelationalBackend {
287294
val source = JdbcDataSource.forConfig(usedConfig, driver, path, classLoader)
288295
val poolName = usedConfig.getStringOr("poolName", path)
289296
val numThreads = usedConfig.getIntOr("numThreads", 20)
290-
val maxConnections = source.maxConnections.fold(numThreads)(identity)
297+
val maxConnections = source.maxConnections.getOrElse(numThreads)
291298
val registerMbeans = usedConfig.getBooleanOr("registerMbeans", false)
292299
val executor = AsyncExecutor(poolName, numThreads, numThreads, usedConfig.getIntOr("queueSize", 1000),
293300
maxConnections, registerMbeans = registerMbeans)

slick/src/main/scala/slick/jdbc/JdbcDataSource.scala

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,26 @@ class DataSourceJdbcDataSource(val ds: DataSource, val keepAliveConnection: Bool
8484

8585
object DataSourceJdbcDataSource extends JdbcDataSourceFactory {
8686
def forConfig(c: Config, driver: Driver, name: String, classLoader: ClassLoader): DataSourceJdbcDataSource = {
87-
val ds = c.getStringOpt("dataSourceClass") match {
88-
case Some(dsClass) =>
89-
val propsO = c.getPropertiesOpt("properties")
90-
try {
91-
val ds = Class.forName(dsClass).newInstance.asInstanceOf[DataSource]
92-
propsO.foreach(BeanConfigurator.configure(ds, _))
93-
ds
94-
} catch { case ex: Exception => throw new SlickException("Error configuring DataSource "+dsClass, ex) }
95-
case None =>
96-
val ds = new DriverDataSource
97-
ds.classLoader = classLoader
98-
ds.driverObject = driver
99-
BeanConfigurator.configure(ds, c.toProperties, Set("url", "user", "password", "properties", "driver", "driverClassName"))
100-
ds
101-
}
102-
new DataSourceJdbcDataSource(ds, c.getBooleanOr("keepAliveConnection"), None, new ConnectionPreparer(c))
87+
val (ds, maxConnections) =
88+
c.getStringOpt("dataSourceClass") match {
89+
90+
case Some(dsClass) =>
91+
val propsO = c.getPropertiesOpt("properties")
92+
try {
93+
val ds = Class.forName(dsClass).newInstance.asInstanceOf[DataSource]
94+
propsO.foreach(BeanConfigurator.configure(ds, _))
95+
val maxConnections = c.getIntOpt("maxConnections")
96+
(ds, maxConnections)
97+
} catch { case ex: Exception => throw new SlickException("Error configuring DataSource " + dsClass, ex) }
98+
99+
case None =>
100+
val ds = new DriverDataSource
101+
ds.classLoader = classLoader
102+
ds.driverObject = driver
103+
BeanConfigurator.configure(ds, c.toProperties, Set("url", "user", "password", "properties", "driver", "driverClassName"))
104+
(ds, None)
105+
}
106+
new DataSourceJdbcDataSource(ds, c.getBooleanOr("keepAliveConnection"), maxConnections, new ConnectionPreparer(c))
103107
}
104108
}
105109

slick/src/main/scala/slick/util/AsyncExecutor.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ object AsyncExecutor extends Logging {
4949
* queue and thread pool workload. */
5050
def apply(name: String, minThreads: Int, maxThreads: Int, queueSize: Int, maxConnections: Int = Integer.MAX_VALUE, keepAliveTime: Duration = 1.minute,
5151
registerMbeans: Boolean = false): AsyncExecutor = new AsyncExecutor {
52+
5253
@volatile private[this] lazy val mbeanName = new ObjectName(s"slick:type=AsyncExecutor,name=$name");
5354

5455
// Before init: 0, during init: 1, after init: 2, during/after shutdown: 3
@@ -184,6 +185,15 @@ object AsyncExecutor extends Logging {
184185
}
185186
}
186187

188+
def default(name: String, maxConnections: Int): AsyncExecutor =
189+
apply(
190+
name,
191+
minThreads = 20,
192+
maxThreads = 20,
193+
queueSize = 1000,
194+
maxConnections = maxConnections
195+
)
196+
187197
def default(name: String = "AsyncExecutor.default"): AsyncExecutor =
188198
apply(name, 20, 1000)
189199

0 commit comments

Comments
 (0)