/
ConnectionPool.scala
355 lines (315 loc) · 10.5 KB
/
ConnectionPool.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package scalikejdbc
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory
import javax.sql.DataSource
import java.sql.Connection
import scala.concurrent.{ ExecutionContextExecutor, ExecutionContext }
/**
* Connection Pool
*
* The default implementation uses Commons DBCP 2 internally.
*
* @see [[https://commons.apache.org/proper/commons-dbcp/]]
*/
object ConnectionPool extends LogSupport {
/**
* The default execution context used by async workers for connection pools management.
*/
private lazy val DEFAULT_EXECUTION_CONTEXT: ExecutionContextExecutor = {
ExecutionContext.fromExecutor(Executors.newFixedThreadPool(3, new ThreadFactory {
private val i = new AtomicInteger(0)
override def newThread(r: Runnable): Thread = {
val thread = new Thread(r, s"scalikejdbc-connection-pool-default-ec-${i.incrementAndGet()}")
thread.setDaemon(true)
thread
}
}))
}
type MutableMap[A, B] = scala.collection.mutable.HashMap[A, B]
type CPSettings = ConnectionPoolSettings
type CPFactory = ConnectionPoolFactory
val DEFAULT_NAME: Symbol = 'default
val DEFAULT_CONNECTION_POOL_FACTORY = Commons2ConnectionPoolFactory
private[this] val pools = new MutableMap[Any, ConnectionPool]()
/**
* Returns true when the specified Connection pool is already initialized.
*
* @param name pool name
* @return is initialized
*/
def isInitialized(name: Any = DEFAULT_NAME): Boolean = pools.synchronized {
pools.get(name).isDefined
}
private def ensureInitialized(name: Any): Unit = {
if (!isInitialized(name)) {
val message = ErrorMessage.CONNECTION_POOL_IS_NOT_YET_INITIALIZED + "(name:" + name + ")"
throw new IllegalStateException(message)
}
}
/**
* Returns Connection pool. If the specified Connection pool does not exist, returns null.
*
* @param name pool name
* @return connection pool
*/
def apply(name: Any = DEFAULT_NAME): ConnectionPool = get(name)
/**
* Returns Connection pool. If the specified Connection pool does not exist, returns null.
*
* @param name pool name
* @return connection pool
*/
def get(name: Any = DEFAULT_NAME): ConnectionPool = pools.synchronized {
pools.getOrElse(name, {
val message = ErrorMessage.CONNECTION_POOL_IS_NOT_YET_INITIALIZED + "(name:" + name + ")"
throw new IllegalStateException(message)
})
}
/**
* Registers new named Connection pool.
*
* @param name pool name
* @param url JDBC URL
* @param user JDBC username
* @param password JDBC password
* @param settings Settings
*/
def add(name: Any, url: String, user: String, password: String, settings: CPSettings = ConnectionPoolSettings())(
implicit
factory: CPFactory = DEFAULT_CONNECTION_POOL_FACTORY,
ec: ExecutionContext = DEFAULT_EXECUTION_CONTEXT): Unit = {
import scalikejdbc.JDBCUrl._
val (_factory, factoryName) = Option(settings.connectionPoolFactoryName).map { name =>
ConnectionPoolFactoryRepository.get(name).map(f => (f, name)).getOrElse {
val message = ErrorMessage.INVALID_CONNECTION_POOL_FACTORY_NAME + "(name:" + name + ")"
throw new IllegalArgumentException(message)
}
}.getOrElse((factory, "<default>"))
// register new pool or replace existing pool
pools.synchronized {
val oldPoolOpt: Option[ConnectionPool] = pools.get(name)
// Heroku support
val pool = url match {
case HerokuPostgresRegexp(_user, _password, _host, _dbname) =>
val _url = "jdbc:postgresql://%s/%s".format(_host, _dbname)
_factory.apply(_url, _user, _password, settings)
case url @ HerokuMySQLRegexp(_user, _password, _host, _dbname) =>
val defaultProperties = """?useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci"""
val addDefaultPropertiesIfNeeded = MysqlCustomProperties.findFirstMatchIn(url).map(_ => "").getOrElse(defaultProperties)
val _url = "jdbc:mysql://%s/%s".format(_host, _dbname + addDefaultPropertiesIfNeeded)
_factory.apply(_url, _user, _password, settings)
case _ =>
_factory.apply(url, user, password, settings)
}
pools.update(name, pool)
// wait a little because rarely NPE occurs when immediately accessed.
Thread.sleep(settings.warmUpTime)
// asynchronously close the old pool if exists
oldPoolOpt.foreach(pool => abandonOldPool(name, pool))
}
if (GlobalSettings.loggingConnections) {
log.debug(s"Registered connection pool : ${get(name)} using factory : $factoryName")
}
}
/**
* Registers new named Connection pool.
*
* @param name pool name
* @param dataSource DataSource based ConnectionPool
*/
def add(name: Any, dataSource: DataSourceConnectionPool): Unit = {
// NOTE: cannot pass ExecutionContext from outside due to overload issue
// (multiple overloaded alternatives of method add define default arguments.)
val oldPoolOpt: Option[ConnectionPool] = pools.get(name)
// register new pool or replace existing pool
pools.synchronized {
pools.update(name, dataSource)
// wait a little because rarely NPE occurs when immediately accessed.
Thread.sleep(100L)
}
// asynchronously close the old pool if exists
oldPoolOpt.foreach(pool => abandonOldPool(name, pool))
}
/**
* Registers new named Connection pool.
*
* @param name pool name
* @param dataSource DataSource based ConnectionPool
*/
def add(name: Any, dataSource: AuthenticatedDataSourceConnectionPool): Unit = {
// NOTE: cannot pass ExecutionContext from outside due to overload issue
// (multiple overloaded alternatives of method add define default arguments.)
val oldPoolOpt: Option[ConnectionPool] = pools.get(name)
// register new pool or replace existing pool
pools.synchronized {
pools.update(name, dataSource)
// wait a little because rarely NPE occurs when immediately accessed.
Thread.sleep(100L)
}
// asynchronously close the old pool if exists
oldPoolOpt.foreach(pool => abandonOldPool(name, pool))
}
private[this] def abandonOldPool(name: Any, oldPool: ConnectionPool)(
implicit
ec: ExecutionContext = DEFAULT_EXECUTION_CONTEXT) = {
scala.concurrent.Future {
if (GlobalSettings.loggingConnections) {
log.debug("The old pool destruction started. connection pool : " + get(name).toString())
}
var millis = 0L
while (millis < 60000L && oldPool.numActive > 0) {
Thread.sleep(100L)
millis += 100L
}
oldPool.close()
if (GlobalSettings.loggingConnections) {
log.debug("The old pool is successfully closed. connection pool : " + get(name).toString())
}
}
}
/**
* Registers the default Connection pool.
*
* @param url JDBC URL
* @param user JDBC username
* @param password JDBC password
* @param settings Settings
*/
def singleton(url: String, user: String, password: String,
settings: CPSettings = ConnectionPoolSettings())(implicit factory: CPFactory = DEFAULT_CONNECTION_POOL_FACTORY): Unit = {
add(DEFAULT_NAME, url, user, password, settings)(factory)
if (GlobalSettings.loggingConnections) {
log.debug("Registered singleton connection pool : " + get().toString())
}
}
/**
* Registers the default Connection pool.
*
* @param dataSource DataSource
*/
def singleton(dataSource: DataSourceConnectionPool): Unit = {
add(DEFAULT_NAME, dataSource)
if (GlobalSettings.loggingConnections) {
log.debug("Registered singleton connection pool : " + get().toString())
}
}
/**
* Registers the default Connection pool.
*
* @param dataSource DataSource
*/
def singleton(dataSource: AuthenticatedDataSourceConnectionPool): Unit = {
add(DEFAULT_NAME, dataSource)
if (GlobalSettings.loggingConnections) {
log.debug("Registered singleton connection pool : " + get().toString())
}
}
/**
* Returns javax.sql.DataSource.
*
* @param name pool name
* @return datasource
*/
def dataSource(name: Any = DEFAULT_NAME): DataSource = {
ensureInitialized(name)
get(name).dataSource
}
/**
* Borrows a java.sql.Connection from the specified connection pool.
*
* @param name pool name
* @return connection
*/
def borrow(name: Any = DEFAULT_NAME): Connection = {
ensureInitialized(name)
val pool = get(name)
if (GlobalSettings.loggingConnections) {
log.debug("Borrowed a new connection from " + pool.toString())
}
pool.borrow()
}
/**
* Close a pool by name
*
* @param name pool name
*/
def close(name: Any = DEFAULT_NAME): Unit = {
pools.synchronized {
val removed = pools.remove(name)
removed.foreach { pool => pool.close() }
}
}
/**
* Close all connection pools
*/
def closeAll(): Unit = {
pools.synchronized {
pools.foreach {
case (name, pool) =>
close(name)
}
}
}
}
/**
* Connection Pool
*/
abstract class ConnectionPool(
val url: String,
val user: String,
password: String,
val settings: ConnectionPoolSettings = ConnectionPoolSettings()) extends AutoCloseable {
/**
* Borrows java.sql.Connection from pool.
*
* @return connection
*/
def borrow(): Connection
/**
* Returns javax.sql.DataSource object.
*
* @return datasource
*/
def dataSource: DataSource
/**
* Returns num of active connections.
*
* @return num
*/
def numActive: Int = throw new UnsupportedOperationException
/**
* Returns num of idle connections.
*
* @return num
*/
def numIdle: Int = throw new UnsupportedOperationException
/**
* Returns max limit of active connections.
*
* @return num
*/
def maxActive: Int = throw new UnsupportedOperationException
/**
* Returns max limit of idle connections.
*
* @return num
*/
def maxIdle: Int = throw new UnsupportedOperationException
/**
* Returns self as a String value.
*
* @return printable String value
*/
override def toString(): String = "ConnectionPool(url:" + url + ", user:" + user + ")"
/**
* Close this connection pool.
*/
def close(): Unit = throw new UnsupportedOperationException
def connectionAttributes: DBConnectionAttributes = {
val timeZoneSettings = Option(settings.timeZone).fold(TimeZoneSettings()) { timeZone =>
TimeZoneSettings(true, java.util.TimeZone.getTimeZone(timeZone))
}
DBConnectionAttributes(driverName = Option(settings.driverName), timeZoneSettings = timeZoneSettings)
}
}