Native Material in Slick Transaction

I am using Slick 3.1.1 and I would like to implement my own things on a Slick transaction.

def findSomeProducts = table.getSomeProducts() //db operation
def productTableUpdate = doSomeStuff1() //db operation
def priceTableUpdate = doSomeStuff2() //db operation

def updateElasticCache = updateIndexOfProduct() //this is not a database operation

I have these fetch functions. Firstly, I get some products from db and after updating the tables. In the end, I need to run the updateElasticCache method. If the updateElasticCache method fails, I would like to roll back whole db procceses.

I cannot use (for { ... } yield ()).transactionallythis code because it is not applicable for my cases. This is "transactional" waiting for db action. But I want to add another functionality that is not a db process.

Is it possible? How can i achieve this?

+4
source share
1 answer

DBIO.from

!!! non db db slick DBIO DBIO.from

, " " , DBIO db.

DBIO.from . . DBIO.from DBIOAction. db -db db .

def updateElasticCache: Future[Unit] = Future(doSomething())

, db

def createUser(user: User): DBIO[Int] = ???

, createUser , .

val action = createUser.flatMap { _ => DBIO.from(updateElasticCache()) }.transactionally
db.run(action)

, updateElasticCache tx, .

, .

def updateStats: DBIO[Int] = ???
val rollbackActions =
  (for {
     cStatus <- createUser()
     uStatus <- updateStats()
     result <- DBIO.from(updateElasticCache())
   } yield result).transactionally
 db.run(rollbackActions)

, updateElasticCache

+3

Source: https://habr.com/ru/post/1653477/


All Articles