Doobie流式查询导致无效操作

时间:2017-07-12 10:05:43

标签: scala jdbc db2

Doobie

在Scala或Doobie中没有太多经验我试图从DB2数据库中选择数据。以下查询工作正常,并按预期打印5名员工。

import doobie.imports._, scalaz.effect.IO

object ScalaDoobieSelect extends App {
    val urlPrefix = "jdbc:db2:"
    val schema = "SCHEMA"
    val obdcName = "ODBC"
    val url = urlPrefix + obdcName + ":" +
              "currentSchema=" + schema + ";" +
              "currentFunctionPath=" + schema + ";"
    val driver = "com.ibm.db2.jcc.DB2Driver"
    val username = "username"
    val password = "password"

    implicit val han = LogHandler.jdkLogHandler       // (ii)

    val xa = DriverManagerTransactor[IO](
        driver, url, username, password
    )

    case class User(id: String, name: String)

    def find(): ConnectionIO[List[User]] =
        sql"SELECT ID, NAME FROM EMPLOYEE FETCH FIRST 10 ROWS ONLY"
            .query[User]
            .process
            .take(5)                                  // (i)
            .list

    find()
        .transact(xa)
        .unsafePerformIO
        .foreach(e => println("ID = %s, NAME = %s".format(e.id, e.name)))
}

问题

如果我想要阅读所有选定的行并移除take(5),那么我有.process.list而不是.process.take(5).list,我会收到以下错误消息。 (ⅰ)

com.ibm.db2.jcc.am.SqlException: [jcc][t4][10120][10898][3.64.133] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=null

我想知道take(5)更改它不会返回错误。为了获得有关无效操作的更多信息,我尝试启用日志记录。 (ii)不幸的是,logging is not supported for streaming。如何获得有关哪种操作导致此错误的更多信息?

普通JDBC

下面,在我看来,普通的JDBC查询按预期工作并返回所有10行。

import java.sql.{Connection,DriverManager}

object ScalaJdbcConnectSelect extends App {
    val urlPrefix = "jdbc:db2:"
    val schema = "SCHEMA"
    val obdcName = "ODBC"
    val url = urlPrefix + obdcName + ":" +
              "currentSchema=" + schema + ";" +
              "currentFunctionPath=" + schema + ";"
    val driver = "com.ibm.db2.jcc.DB2Driver"
    val username = "username"
    val password = "password"

    var connection:Connection = _
    try {
        Class.forName(driver)
        connection = DriverManager.getConnection(url, username, password)
        val statement = connection.createStatement
        val rs = statement.executeQuery(
            "SELECT ID, NAME FROM EMPLOYEE FETCH FIRST 10 ROWS ONLY"
        )
        while (rs.next) {
            val id = rs.getString("ID")
            val name = rs.getString("NAME")
            println("ID = %s, NAME = %s".format(id,name))
        }
    } catch {
        case e: Exception => e.printStackTrace
    }
    connection.close
}

环境

从错误消息中可以看出,我使用的是db2jcc.jar版本3.64.133。 DB2在版本11中使用。

0 个答案:

没有答案