2

How do we perform multiple inserts in the same transaction?

  def insertData(dataList: List[Data])(implicit session: DBSession = autoSession) = {

    // todo: this is probably opening and closing a connection every time?
    dataList.foreach(data => insertData(data))
  }

  def insertData(data: Data) = withSQL {
    val t = DataTable.column
    insert.into(DataTable).namedValues(
      d.name -> data.name,
      d.title -> data.title
    )
  }.update().apply()

It would not be efficient to have a different transaction for every insert if these numbered in the thousands and up.

http://scalikejdbc.org/documentation/operations.html

BAR
  • 15,909
  • 27
  • 97
  • 185

1 Answers1

3

Modify your insertData method like this:

def insertData(data: Data)(implicit session: DBSession = AutoSession) = withSQL {
  val t = DataTable.column
  insert.into(DataTable).namedValues(
    d.name -> data.name,
    d.title -> data.title
  )
}.update().apply()

And then, use DB.localTx:

DB.localTx { implicit s =>
  dataList.foreach(data => insertData(data))
}
BAR
  • 15,909
  • 27
  • 97
  • 185
Kazuhiro Sera
  • 1,822
  • 12
  • 15