问题
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