src/main/scala/net/tz/lift/util/DB.scala
author Tomas Zeman <tzeman@volny.cz>
Tue, 23 Apr 2013 10:36:04 +0200
changeset 108 ef4e3e0ef83f
parent 5 993582ca8d2e
permissions -rw-r--r--
84a94fa29a67504b Task/Project notifications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
 * Fix for ProtoDBVendor/StandardDBVendor connection leak.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Original code is:
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * Copyright 2006-2011 WorldWide Conferencing, LLC
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * You may obtain a copy of the License at
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
 * limitations under the License.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
 *
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
 * All fixes are under the same license.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
 */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
package net.tz.lift.util
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
import java.sql.{Connection, DriverManager}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
import net.liftweb.common._
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
import net.liftweb.db.{ConnectionIdentifier, ConnectionManager}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
import net.liftweb.util._
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
import net.liftweb.util.Helpers._
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
/**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
 * The standard DB vendor.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
 * @param driverName the name of the database driver
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
 * @param dbUrl the URL for the JDBC data connection
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
 * @param dbUser the optional username
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
 * @param dbPassword the optional db password
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
 */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
class StandardDBVendor(driverName: String,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
                       dbUrl: String,
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
                       dbUser: Box[String],
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
                       dbPassword: Box[String]) extends ProtoDBVendor {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
  protected def createOne: Box[Connection] = try {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
    Class.forName(driverName)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
    val dm = (dbUser, dbPassword) match {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
      case (Full(user), Full(pwd)) =>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
        DriverManager.getConnection(dbUrl, user, pwd)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
      case _ => DriverManager.getConnection(dbUrl)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    49
    }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
    Full(dm)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
  } catch {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
    case e: Exception => e.printStackTrace; Empty
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
  }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    56
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    57
trait ProtoDBVendor extends ConnectionManager {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    58
  private val logger = Logger(classOf[ProtoDBVendor])
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    59
  private var pool: List[Connection] = Nil
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    60
  private var poolSize = 0
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    61
  private var tempMaxSize = maxPoolSize
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    62
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    63
  /**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    64
   * Override and set to false if the maximum pool size can temporarilly be expanded to avoid pool starvation
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    65
   */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    66
  protected def allowTemporaryPoolExpansion = true
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    67
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    68
  /**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    69
   *  Override this method if you want something other than
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    70
   * 4 connections in the pool
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    71
   */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    72
  protected def maxPoolSize = 4
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    73
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    74
  /**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    75
   * The absolute maximum that this pool can extend to
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    76
   * The default is 20.  Override this method to change.
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    77
   */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    78
  protected def doNotExpandBeyond = 20
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    79
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    80
  /**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    81
   * The logic for whether we can expand the pool beyond the current size.  By
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    82
   * default, the logic tests allowTemporaryPoolExpansion &amp;&amp; poolSize &lt;= doNotExpandBeyond
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    83
   */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    84
  protected def canExpand_? : Boolean = allowTemporaryPoolExpansion && poolSize <= doNotExpandBeyond
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    85
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    86
  /**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    87
   *   How is a connection created?
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    88
   */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    89
  protected def createOne: Box[Connection]
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    90
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    91
  /**
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    92
   * Test the connection.  By default, setAutoCommit(false),
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    93
   * but you can do a real query on your RDBMS to see if the connection is alive
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    94
   */
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    95
  protected def testConnection(conn: Connection) {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    96
    conn.setAutoCommit(false)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    97
  }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    98
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    99
  def newConnection(name: ConnectionIdentifier): Box[Connection] =
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   100
    synchronized {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   101
      pool match {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   102
        case Nil if poolSize < tempMaxSize =>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   103
          val ret = createOne
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   104
          ret foreach { c =>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   105
            c.setAutoCommit(false)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   106
            poolSize = poolSize + 1
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   107
            logger.debug("Created new pool entry. name=%s, poolSize=%d".format(name, poolSize))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   108
          }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   109
          ret
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   110
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   111
        case Nil =>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   112
          val curSize = poolSize
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   113
          logger.trace("No connection left in pool, waiting...")
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   114
          wait(50L)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   115
          // if we've waited 50 ms and the pool is still empty, temporarily expand it
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   116
          if (pool.isEmpty && poolSize == curSize && canExpand_?) {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   117
            tempMaxSize += 1
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   118
            logger.debug("Temporarily expanding pool. name=%s, tempMaxSize=%d".format(name, tempMaxSize))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   119
          }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   120
          newConnection(name)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   121
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   122
        case x :: xs =>
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   123
          logger.trace("Found connection in pool, name=%s".format(name))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   124
          pool = xs
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   125
          try {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   126
            this.testConnection(x)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   127
            Full(x)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   128
          } catch {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   129
            case e => try {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   130
              logger.debug("Test connection failed, removing connection from pool, name=%s".format(name))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   131
              poolSize = poolSize - 1
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   132
              tryo(x.close)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   133
              newConnection(name)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   134
            } catch {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   135
              case e => newConnection(name)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   136
            }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   137
          }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   138
      }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   139
    }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   140
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   141
  def releaseConnection(conn: Connection): Unit = synchronized {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   142
    if (tempMaxSize > maxPoolSize) {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   143
      tryo {conn.close()}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   144
      tempMaxSize -= 1
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   145
      poolSize -= 1
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   146
    } else {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   147
      pool = conn :: pool
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   148
    }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   149
    logger.debug("Released connection. poolSize=%d".format(poolSize))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   150
    notifyAll
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   151
  }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   152
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   153
  def closeAllConnections_!(): Unit = synchronized {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   154
    logger.info("Closing all connections, poolSize=%d".format(poolSize))
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   155
    if (poolSize == 0) ()
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   156
    else {
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   157
      pool.foreach {c => tryo(c.close); poolSize -= 1}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   158
      pool = Nil
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   159
      
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   160
      if (poolSize > 0) wait(250)
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   161
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   162
      closeAllConnections_!()
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   163
    }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   164
  }
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   165
}
993582ca8d2e Import of tz-lift-misc@14a648a3676f
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
   166
// vim: set ts=2 sw=2 et: