Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungFliproom: a room changeover is a content problem(21.09.2026 um 04:10 Uhr)
Sichere ProgrammierungNova Adiutrix: My Second Agent Built My First Project's To-Do List(21.09.2026 um 04:11 Uhr)
Sichere ProgrammierungFliproom: a room changeover is a content problem(21.09.2026 um 04:10 Uhr)
Sichere ProgrammierungNova Adiutrix: My Second Agent Built My First Project's To-Do List(21.09.2026 um 04:11 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

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

Reagiere als Erste:r — dein Feedback zählt!

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.");
        } 
    }
// 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;
    }

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!");
            }
    }

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 👋

Ähnliche Beiträge
🔍 Verwandte News

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

Thematisch verwandte Begriffe: Java, Transfer, Cash, Hanging · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93977 | A vulnerability was determined in code-projects Assessment Management 1.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick