释放从ConnectionPool借用的Connection [英] Release a Connection borrowed from ConnectionPool

查看:235
本文介绍了释放从ConnectionPool借用的Connection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).

我的疑问是:

  1. 通常,从ConnectionPool借来的Connection release '是什么意思?
  2. ScalikeJDBC中,如何从ConnectionPool中释放" Connection 借来的?
  1. In general, what does 'releasing' a Connection (that has been borrowed from ConnectionPool) mean?
  2. In ScalikeJDBC, how do I 'release' a Connection borrowed from ConnectionPool?

推荐答案

调用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆