当我调用runMain
任务并且VM干净地退出时,它会中断任何已经有关闭钩子的守护程序线程,这意味着这些线程钩子没有被执行。
[debug] Interrupting remaining threads (should be all daemons).
[debug] Interrupting thread Thread-7
[debug] Interrupted Thread-7
[debug] Interrupting thread MVStore background writer nio:/Users/bryan/db/example-app.mv.db
[debug] Interrupted MVStore background writer nio:/Users/bryan/db/example-app.mv.db
[debug] Interrupting thread HikariPool-15 housekeeper
[debug] Interrupted HikariPool-15 housekeeper
[debug] Sandboxed run complete..
[debug] Exited with code 0
在这种情况下,H2服务器不会干净地关闭,从而在后备存储上留下锁。
有效的代码是:
def main(args: Array[String]): Unit = {
val props = getProps(args)
val ds = new HikariDataSource
try {
ds.setDriverClassName(prop.getProperty("db.driver"))
ds.setJdbcUrl(prop.getProperty("db.url"))
ds.setUsername(prop.getProperty("db.user"))
ds.setPassword(prop.getProperty("db.pass"))
val jt = new JdbcTemplate(ds)
jt.execute("create table if not exists TEST (id identity, name varchar)")
} catch {
case e: Throwable => {
e.printStackTrace()
}
ds.close()
}
恕我直言,SBT的这种行为是一个错误,它应该在执行任何中断之前等待所有已注册的关闭挂钩完成。