#88 Money Transfer Spring Service Review

Companies:
OTP Bank
Themes:
SpringTransactionsCode Review

Perform a code review:

@Data
class Account extends Object {
    Float money;
    String userName;
}

@Service
class Main_Account_Service extends Object {

    @Autowired
    public Datasource datasource;

    /* Transfers money from one account to another */
    public void TransferMoney(Account from, Account to, Float money) {
        // lock the first account
        synchronized(from) {
            // lock the second account
            synchronized(to) {
                // transfer the money
                from.money = from.money - money;
                to.money = to.money + money;

                // save the result to the database
                save(from);
                save(to);
            }
        }
    }

    /* Saves an account to the database */
    public void save(Account account) {
        // database connection
        Connection conn = datasource.getConnection();
        try {
            // create a query
            Statement s = conn.createStatement();

            // execute the query
            s.execute("UPDATE account SET money = " + account.money + " WHERE userName = '" + account.userName + "'");
        } finally {
            // close the database connection
            conn.close();
        }
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in