释放从ConnectionPool借用的Connection [英] Release a Connection borrowed from ConnectionPool
问题描述
ScalikeJDBC's
ConnectionPool
文档页面说:
浏览连接
只需调用#borrow
方法.
import scalikejdbc._
val conn: java.sql.Connection = ConnectionPool.borrow()
val conn: java.sql.Connection = ConnectionPool('named).borrow()
注意.连接对象应该由您自己释放.
Be careful. The connection object should be released by yourself.
但是没有提到如何做.
However there's no mention of how to do it.
我总是可以执行Connection.close()
,但是通过'释放'Connection
,
我了解我应该将Connection
还原回ConnectionPool
而不是 close (否则会违反使用
I can always do Connection.close()
but by 'releasing' Connection
,
I understand that I'm supposed to return the Connection
back to the ConnectionPool
and not close it (otherwise the purpose of having a ConnectionPool
would be defied).
我的疑问是:
- 通常,从
ConnectionPool
借来的Connection
的 release '是什么意思? - 在
ScalikeJDBC
中,如何从ConnectionPool
中释放"Connection
借来的?
- In general, what does 'releasing' a
Connection
(that has been borrowed fromConnectionPool
) mean? - In
ScalikeJDBC
, how do I 'release' aConnection
borrowed fromConnectionPool
?
推荐答案
调用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的实现.
Calling close
is fine. As per the Oracle docs: Closing a connection instance that was obtained from a pooled connection does not close the physical database connection.
. The DBConnection
in scalikejdbc just wraps the java.sql.Connection
and delegates calls to close
. The usual way of doing this with scalikejdbc is with the using
function which is essentially an implementation of Java's try-with-resources.
有关对JDBC的类似讨论,请参见关闭Pool中的JDBC连接.
See Closing JDBC Connections in Pool for a similar discussion on JDBC.
这篇关于释放从ConnectionPool借用的Connection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!