2

运行以下规范后,该表存在。我希望它永远不会出现,因为它应该只存在于最终回滚的事务中。

import org.specs2.mutable.Specification
import scalikejdbc.{DB, NamedDB}
import scalikejdbc.specs2.mutable.AutoRollback

class MyQuerySpec extends Specification with ArbitraryInput {

  sequential

  DBs.setup('myDB)

  "creating the table" in new AutoRollback {
    override def db(): DB = NamedDB('myDB).toDB()
    private val tableName = s"test_${UUID.randomUUID().toString.replaceAll("-", "_")}"
    private val query = new MyQuery(tableName)

    query.createTable
    ok
  }
}

该行DBs.setup('myDB)不是示例的一部分。但是如果我删除它,我会得到例外java.lang.IllegalStateException: Connection pool is not yet initialized.(name:'myDB)

来源MyQuery.create

SQL(s"DROP TABLE IF EXISTS $tableName").execute().apply()
SQL(s"""
     |CREATE TABLE $tableName (
     |  id               bigint PRIMARY KEY
     |)""".stripMargin).execute().apply()

配置:

db {
  myDB {
    driver = "org.postgresql.Driver"
    url = "****"
    user = "****"
    password = "****"
    poolInitialSize = 1
    poolMaxSize = 300
    poolConnectionTimeoutMillis = 120000
    poolValidationQuery = "select 1 as one"
    poolFactoryName = "commons-dbcp2"
  }
}

ScalikeJDBC v2.2.9

4

1 回答 1

1

必须接受这样的MyQuery#createTable隐式参数:

def createTable(implicit session: DBSession)

于 2015-11-17T23:33:31.197 回答