我想检查数据库中是否存在用户。我需要使用commit吗?这段代码是获取值的正确方法吗?
val transaction: DistributedTransaction = transactionService.start
logger.trace("transaction started: " + transaction);
//Perform the operations you want to group in the transaction
val pUserKey = new Key(new IntValue("bucket", utilities.bucketIDFromEmail(userKeys.bucket)),
new TextValue("email", userKeys.email)
)
val cUserKey = new Key(
new TextValue("authprovider", "credentials"),
new TextValue("firstname", userKeys.firstName),
new TextValue("lastname", userKeys.lastName)
)
logger.trace("created keys. ")
logger.trace("getting user")
val get:Get = new Get(pUserKey,cUserKey);
val result:Optional[Result] = transaction.get(get);
if(result.isPresent){
logger.trace(s"found user ${result}")
} else {
logger.trace(s"user doesn't exist")
}
try {
logger.trace(s"committing")
transaction.commit()
} catch {
case e1:UnknownTransactionStatusException =>{
logger.error("error in commiting. Unknowns status")
throw e1;
}
case e2:CommitException =>{
logger.error("error in commiting. Rolling back")
transaction.abort();
throw e2;
}
}
什么时候会实际获取这些值?当我打电话transaction.get或打电话时transaction.commit?