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
会被藐视)。
我的怀疑是:
Connection
(已从ConnectionPool
借来的)是什么意思?ScalikeJDBC
中,我如何发布' Connection
ConnectionPool
借用了
醇>
答案 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