(あなたの質問は、ストリーミングソースの作成についてであるので、この答えはストリーミングのサポートの唯一の出版社側に対処し、加入者側が無視されます。)ストリーミング用
サポートデータベースのリターンクエリは、いくつかの行を結果する必要があり
一度にすべてではなく、典型的にはカーソルに基づいている。異なるデータベースには、これを有効にするさまざまな方法があります。 ScalikeJDBCはネイティブでMySQLとPostgreSQLドライバ用にストリーミングiterator
メソッドの使用をサポートしています。なぜならthisのMySQLとPostgreSQLの
import scalikejdbc._
import scalikejdbc.streams._
// set up a connection pool
import scala.concurrent.ExecutionContext.Implicits.global
val publisher: DatabasePublisher[Int] = DB.readOnlyStream {
sql"select id from users order by id".map(r => r.get[Int]("id")).iterator
}
上記作品:最後case
句はScalikeJDBCがないことを意味していること
/**
* Forcibly changes the database session to be cursor query ready.
*/
val defaultDBSessionForceAdjuster: DBSessionForceAdjuster = (session) => {
// setup required settings to enable cursor operations
session.connectionAttributes.driverName match {
case Some(driver) if driver == "com.mysql.jdbc.Driver" && session.fetchSize.exists(_ > 0) =>
/*
* MySQL - https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-implementation-notes.html
*
* StreamAction.StreamingInvoker prepares the following required settings in advance:
*
* - java.sql.ResultSet.TYPE_FORWARD_ONLY
* - java.sql.ResultSet.CONCUR_READ_ONLY
*
* If the fetchSize is set as 0 or less, we need to forcibly change the value with the Int min value.
*/
session.fetchSize(Int.MinValue)
case Some(driver) if driver == "org.postgresql.Driver" =>
/*
* PostgreSQL - https://jdbc.postgresql.org/documentation/94/query.html
*
* - java.sql.Connection#autocommit false
* - java.sql.ResultSet.TYPE_FORWARD_ONLY
*/
session.conn.setAutoCommit(false)
case _ =>
}
}
お知らせつまり、MySQLとPostgreSQLのドライバで、次のような作品ですデフォルトではは、MySQLおよびPostgreSQLのドライバ以外のストリーミングiterator
をサポートしています。
これは、ストリーミングに他のドライバを使用できないという意味ではありません。どのようなドキュメントが言っていることは、あなたがのように、DBSession
属性をカスタマイズする必要があることは、MySQLとPostgreSQL以外のデータベースのためのストリーミングを有効にすることです
val publisher: DatabasePublisher[Int] = DB readOnlyStream {
sql"select id from users".map(r => r.int("id"))
.iterator
.withDBSessionForceAdjuster(session => {
session.conn.setAutoCommit(true)
})
}
:あなたが引用されたドキュメントのセクションには、次のコードサンプルを持っています上記の例では、カーソルのサポートが有効になっています。このカスタマイズが正確に何をするか(例えば、fetchSize
を調整するか、または接続上でautoCommit
を無効にする)は、ドライバによって異なります(一度に少数の行のクエリ結果の取得をドライバがサポートしていると仮定します)。