释放从ConnectionPool借来的连接

时间:2018-04-27 07:16:32

标签: scala connection-pooling scalikejdbc

ScalikeJDBC's ConnectionPool docs page说:

  

借用连接

     

只需致电#borrow方法。

import scalikejdbc._
val conn: java.sql.Connection = ConnectionPool.borrow()
val conn: java.sql.Connection = ConnectionPool('named).borrow()
  

小心点。连接对象应该由您自己释放。

然而,没有提及如何做到这一点。

我总是可以Connection.close()但是通过' 发布' Connection, 我知道我应该Connection返回ConnectionPool 而不是关闭它(否则就是拥有ConnectionPool会被藐视)。

我的怀疑是:

  1. 一般情况下,' 发布' Connection(已从ConnectionPool借来的)是什么意思?
  2. ScalikeJDBC中,我如何发布' Connection
  3. 中的ConnectionPool 借用了

2 个答案:

答案 0 :(得分:2)

调用close很好。根据Oracle文档:Closing a connection instance that was obtained from a pooled connection does not close the physical database connection.。 scalikejdbc中的DBConnection只包装java.sql.Connection并将调用委托给close。使用scalikejdbc执行此操作的常用方法是使用using函数,该函数实际上是Java的try-with-resources实现。

有关JDBC的类似讨论,请参阅Closing JDBC Connections in Pool

答案 1 :(得分:0)

在第二次查看docs后,select a.student_id, b.student_name, group_concat(a.subject_id) as student_enrollment from stu_subjects a, student b where a.student_id = b.student_id group by a.student_id, b.student_name; 确实提供了一个 -- table: student > select * from student; +------------+--------------+ | student_id | student_name | +------------+--------------+ | 1 | Student-1 | | 2 | Student-2 | +------------+--------------+ -- table: subject > select * from subject; +------------+--------------+ | subject_id | subject_name | +------------+--------------+ | 1 | Maths | | 2 | Science | | 3 | English | | 4 | Telugu | | 5 | Social | +------------+--------------+ -- table: stu_subjects > select * from stu_subjects; +------------+------------+------------+ | stu_sub_id | student_id | subject_id | +------------+------------+------------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 1 | 3 | | 4 | 2 | 4 | | 5 | 2 | 1 | +------------+------------+------------+ -- query > select a.student_id, b.student_name, group_concat(a.subject_id) as student_enrollment from stu_subjects a, student b where a.student_id = b.student_id group by a.student_id, b.student_name; +------------+--------------+--------------------+ | student_id | student_name | student_enrollment | +------------+--------------+--------------------+ | 1 | Student-1 | 3,1,2 | | 2 | Student-2 | 4,1 | +------------+--------------+--------------------+ 方法,可自动实施贷款模式ScalikeJdbc返回到using

因此,您可以借用 连接,使用它,然后将其返回到,如下所示:

connection