Db.Transactis the simplest way to create a transaction in Starcounter. It declares a transactional scope and runs synchronously, as described on the previous page. The argument passed to the
Db.Transactmethod is a delegate containing the code to run within the transaction. In code, it looks like this:
Db.Transactis synchronous, it sometimes becomes a performance bottleneck. Starcounter handles this by automatically scaling the number of working threads to continue processing requests even if some handlers are blocked. The maximum number of working threads is the number of CPU cores multiplied by 254, so with four cores, there would be a maximum of 1016 working threads. When these threads are occupied, the next
Db.Transactcall in line will have to wait until a thread is freed. Db.TransactAsync circumvents this.
Db.Transactis an implementation of
Db.TransactAsyncwith a thin wrapper that synchronously waits for the returned
Db.TransactAsyncis the asynchronous counterpart of
Db.Transact. It gives the developer more control to balance throughput and latency. The function returns a
Taskthat is marked as completed when flushing the transaction log for the transaction. Thus, the database operation itself is synchronous while flushing to the transaction log is asynchronous.
Db.TransactAsyncare syntactically identical:
Db.Transact, but it comes with certain risks; for example, if there's a power outage or other hardware failure after the email is sent but before writing to the log, the email will be incorrect - even if the user got a confirmation, the order will not be in the database since it was never written to the transaction log.
Db.TransactAsyncand then wait for all transactions at once with
TaskFactory.ContinueWhenAll. Otherwise, the latency of the handler will degrade.
Db.TransactAsyncAPI, it's tempting to use async/await in applications. Syntactically it's possible, although it's not that useful due to these limitations:
Db.TransactAsyncwill accumulate all the individual latencies. The right stategy in this case is to make a list of tasks and then await them at once.
Db.TransactAsyncshould execute in as short time as possible because conflicts are likelier the longer the transaction is. Conflicts requires long transactions to run more times which can be expensive. The solution is to break the transaction into smaller transactions.