Skip to content

Instantly share code, notes, and snippets.

@kubawieczorek
Created January 21, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubawieczorek/7b93f3ef8da2ba9456eab1e0bcad391b to your computer and use it in GitHub Desktop.
Save kubawieczorek/7b93f3ef8da2ba9456eab1e0bcad391b to your computer and use it in GitHub Desktop.
connection.setAutoCommit(false);
connection.setSavepoint();
try (PreparedStatement selectAccountStatementFrom = connection.prepareStatement(SELECT_ACCOUNT_BY_OWNER_SQL);
PreparedStatement selectAccountStatementTo = connection.prepareStatement(SELECT_ACCOUNT_BY_OWNER_SQL);
PreparedStatement updateAccountStatement = connection.prepareStatement(UPDATE_ACCOUNT_SQL)) {
ResultSet accountFrom = findAccount(bankTransfer.getFrom(), selectAccountStatementFrom);
ResultSet accountTo = findAccount(bankTransfer.getTo(), selectAccountStatementTo);
BigDecimal amount = bankTransfer.getAmount();
if (dataPresent(accountFrom, accountTo)) {
if (sufficientAccountBalance(amount, accountFrom)) {
decreaseAccountBalance(updateAccountStatement, accountFrom, amount);
updateAccountStatement.executeUpdate();
increaseAccountBalance(updateAccountStatement, accountTo, amount);
updateAccountStatement.executeUpdate();
} else {
throw new BusinessException("Insufficient account balance");
}
} else {
throw new BusinessException("Incorrect account provided");
}
connection.commit();
} catch (Exception e) {
connection.rollback();
System.out.printf("Error when performing transfer %s \n", e.getMessage());
throw e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment