DEV Community

Victor Chigbo
Victor Chigbo

Posted on

Java ATM CLI Dev Log #2: Transfer Cash, Hanging?

I never expected building this feature to be so crazy to me.

While building the feature, everything was going smoothly, until the function started to hang during execution. I did some deep dive with ChatGPT and Gemini to see if I could get to the bottom of it. In the process, I got to learn about COMMIT, ROLLBACK, what transactions are in databases and why they are important in financial applications like this one which I am building.

Transactions are basically a sequence of SQL statements that are treated as a single logical unit, which starts with a data change and ending it using COMMIT or ROLLBACK. COMMIT means that the transaction was successfully while ROLLBACK is to roll back any changes made in the event of an error occurring.

Going into the project proper, these were the pieces of code I used for the transfer transactions:

// AccountService.java
public static void transferCash(Account sender, Account receiver, double amount) {
        if (sender.getBalance() > amount) {
            double newSenderBalance = sender.getBalance() - amount;
            double newReceiverBalance = receiver.getBalance() + amount;

            boolean isTransactionSuccessful = AccountDAO.makeTransfer(sender.getId(), receiver.getId(), sender.getAccountType(), receiver.getAccountType(), newSenderBalance, newReceiverBalance);

            if(isTransactionSuccessful) {
                System.out.println("Transaction Successful!");
                sender.setBalance(newSenderBalance);
                System.out.println("Withdrawal successful. New balance: $" + sender.getBalance());
                sender.stringifyAccount();
            } else {
                System.out.println("Transaction Failed!");
            }

        } else {
            System.out.println("Insufficient funds.");
        } 
    }
Enter fullscreen mode Exit fullscreen mode
// AccountDAO.java
public static boolean makeTransfer(int senderId, int receiverId, String senderAccountType, String receiverAccountType, double newSenderBalance, double newReceiverBalance) {
        String senderQuery = "UPDATE accounts SET balance = ? WHERE customerId = ? AND accountType = ?";
        String receiverQuery = "UPDATE accounts SET balance = ? WHERE customerId = ? AND accountType = ?";
        boolean isTransactionSuccessful = false;

        try (
            Connection conn = DBHelper.getConnection()) {
            conn.setAutoCommit(false);

            // For the sender account
            try (PreparedStatement senderStmt = conn.prepareStatement(senderQuery);) {
                senderStmt.setDouble(1, newSenderBalance);
                senderStmt.setInt(2, senderId);
                senderStmt.setString(3, senderAccountType);
                int senderRowsAffected = senderStmt.executeUpdate();

                if (senderRowsAffected == 0) {
                    conn.rollback();
                    return false;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                conn.rollback();
                return false;
            }

            // For the receiver account
            try (PreparedStatement receiverStmt = conn.prepareStatement(receiverQuery)) {
                receiverStmt.setDouble(1, newReceiverBalance);
                receiverStmt.setInt(2, receiverId);
                receiverStmt.setString(3, receiverAccountType);
                int receiverRowsAffected = receiverStmt.executeUpdate();

                if (receiverRowsAffected == 0) {
                    conn.rollback();
                    return false;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                conn.rollback();
                return false;
            }


            conn.commit();   
            System.out.println("\nBalance Updated Successfully!");

            isTransactionSuccessful = true;

        } catch (Exception e) {
            e.printStackTrace();

            isTransactionSuccessful = false;
        }

        return isTransactionSuccessful;
    }
Enter fullscreen mode Exit fullscreen mode

After talking to a dear friend of mine who is a senior engineer who has some experience working with Java in a bank, he told me that this was actually bad practice (Damn ๐Ÿ˜‘). So I did some modifications in the AccountService and came up with these two functions, debitAccount and creditAccount. Makes sense, right? ๐Ÿ˜…

// AccountService.java
public static void debitAccount(Account account, double amount) {
        if (account.getBalance() > amount) {
            double newBalance = account.getBalance() - amount;
            boolean isTransactionSuccessful = AccountDAO.changeBalance(account.getId(), account.getAccountType(), newBalance);

            if(isTransactionSuccessful) {
                System.out.println("Transaction Successful!");
                account.setBalance(newBalance);
                System.out.println("Withdrawal successful. New balance: $" + account.getBalance());
                account.stringifyAccount();
            } else {
                System.out.println("Transaction Failed!");
            }
        }  else {
            System.out.println("Insufficient funds.");
        }
    }

    public static void creditAccount(Account account, double amount) {
        double newBalance = account.getBalance() + amount;
            boolean isTransactionSuccessful = AccountDAO.changeBalance(account.getId(), account.getAccountType(), newBalance);

            if(isTransactionSuccessful) {
                System.out.println("Transaction Successful!");
                account.setBalance(newBalance);
                System.out.println("Withdrawal successful. New balance: $" + account.getBalance());
                account.stringifyAccount();
            } else {
                System.out.println("Transaction Failed!");
            }
    }
Enter fullscreen mode Exit fullscreen mode

But after everything I did, it all finally led to this:

The ATM CLI hangs when performing a transaction

At this point, I decided to stop and finish up the remaining features of the application, then come back to this one and fix it properly. By then, I must have gain some mastery in how to deal with transactions properly in Java.

If you want to check out the GitHub repo, you can click here and see it for yourself. Well, that's it for now.

Until the next commit, bye ๐Ÿ‘‹

Top comments (0)