javakotlinthread-safetyblockchaincorda

serviceHub.startFlow from a cordaService class versus subFlow calling from Corda flow


Is there a difference in calling one flow as subflow from another corda flow versus calling the same flow from a cordaservice class as serviceHub.startFlow?

In terms of thread utilization or suspending etc?

I encountered a scenario where calling a flow from CordaService function got me error like java.lang.IllegalArgumentException: Transaction context is missing. This might happen if a suspendable method is not annotated with @Suspendable annotation.

whereas calling the same flows from another flow using subflows worked fine.


Solution

  • When we call a flow from service it good practice to call it from its own Thread to avoid Dead locks of transaction update.

    Inside @CordaService class

    private companion object {
            val executor: Executor = Executors.newCachedThreadPool()
          }
        -------------------------------------------------------------------
          executor.execute {
            serviceHub.startFlow(RegisterCarFlow(update))
          }