Created
January 21, 2023 16:47
-
-
Save kubawieczorek/7b93f3ef8da2ba9456eab1e0bcad391b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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