In Scalar DB, can I leave a Transaction as is if I don't need it or do I need to do some clean up

大城市里の小女人 提交于 2021-01-29 14:24:05

问题


The question is about Scalar DB (https://github.com/scalar-labs/scalardb). In the following code, I check if a user exists. If it does, I throw an exception. If it doesn't I go about adding it in tables. I am creating a DistributedTransaction instance at the beginning and am using it in all transactions.

val transaction: DistributedTransaction = transactionService.start
logger.trace("transaction started: " + transaction);
//Perform the operations you want to group in the transaction
val userExists:Boolean = userShouldNotExist(transaction,userKey)
if(!userExists) {
  userTransactionRepository.addUser(transaction, user)
  userProfileAndPortfolioTransactionRepository.addUserProfileAndPortfolio(transaction, user)
  userTokenTransactionRepository.addToken(transaction, emailToken, user)
  commitTransaction(transaction)
  (user: User, profileAndPortfolio: ExternalUserProfile, emailToken: UserToken)
} else {
  throw DuplicateUserException(user)
}

If the else block, do I need to do some cleanup/finalisation with DistributedTransaction or can I just leave it as is?


回答1:


You can basically leave it as it is.

Please note that even if !userExists returns true, the user could be created after the checking by another transaction. In such case, commit will throw an exception.



来源:https://stackoverflow.com/questions/63009148/in-scalar-db-can-i-leave-a-transaction-as-is-if-i-dont-need-it-or-do-i-need-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!