I am trying to insert records into a database with DB actor. I have millions of record. However after operating, the database has only ten records. I know that the database connection is a state, and I think there is a problem with this condition. Here is my represents of code.
class DBActor extends Actor with DBConfig {
override def receive: Receive = {
case Message(id, title) =>
db.run(products += Product(id, title))
}
}
The database is a relational database, 'products' is a TableQuery and DBConfig has a database connection and session. What is the best way to insert records with this actor under guarantee.
Persist records using batch instead of one by one. db.run() method are asynchronous so it returns future immediate and execution happens later on different thread so Actor is not doing anything except a method call(db.run).You should resister callback(onFailure) on result of db.run() method so you can see if any failure happens. See example (it is not compiled code):
case object Insert
case object Flush
class DBActor extends Actor with DBConfig {
implicit val dispatcher = context.dispatcher
val bulkLimit:Int = 1000
val flushTime:Int = 10
var documents = List.empty[Product]
/***
* Start automatic flushing when actor start
*/
context.system.scheduler.scheduleOnce(flushTime second, self, Flush)
def receive:Receive={
case document: Document =>
documents =documents :+ document
log.info(s"DBActor received document [total count ${documents.length}]")
if (documents.length >= bulkLimit) self ! Insert
case Insert =>
if (documents.length > 0) {
val batch = documents.take(bulkLimit)
db.run(products ++= batch).onFailure { case ex: Exception => log.error("Getting error on persisting data", ex) }
documents = documents.drop(bulkLimit)
}
case Flush =>
if (documents.length > 0) self ! Insert
context.system.scheduler.scheduleOnce(flushTime second, self, Flush)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.