Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public long balanceOf(long accountId) {
}

// TODO Task 8: rename returns normally, and the new name never reaches the database. No error anywhere.
@Transactional
public void rename(long accountId, String newOwner) {
TransactionTrace.record("AccountService.rename");
Account account = accounts.require(accountId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.amigoscode.transactional.audit.AuditService;
import com.amigoscode.transactional.trace.TransactionTrace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/** Cash withdrawals. Every attempt is written to the audit log first, then the money is taken. */
Expand All @@ -19,7 +20,7 @@ public AtmService(AccountRepository accounts, AuditService audit) {
this.audit = audit;
}

@Transactional
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void withdraw(long accountId, long amount) {
TransactionTrace.record("AtmService.withdraw");
audit.record("ATM_WITHDRAWAL_ATTEMPT", "account " + accountId + ", amount " + amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.amigoscode.transactional.trace.TransactionTrace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/** Writes the audit log. Security reads it to see every attempt, including the ones that failed. */
Expand All @@ -15,7 +16,7 @@ public AuditService(AuditRepository auditLog) {
}

// TODO Task 6: when the caller's transaction rolls back, this row disappears with it. It must stay.
@Transactional
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void record(String action, String detail) {
TransactionTrace.record("AuditService.record");
auditLog.save(new AuditEntry(action, detail));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public BillService(AccountRepository accounts) {

// TODO Task 4: when the bill is rejected the payer still loses the money. The method is transactional
// and the call goes through the proxy this time. Look at what kind of exception is thrown.
@Transactional
@Transactional(rollbackFor = BillRejectedException.class)
public void payBill(long payerId, long billerId, long amount) throws BillRejectedException {
TransactionTrace.record("BillService.payBill");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public FeeService(AccountRepository accounts) {

// TODO Task 8: this method crashes with a NullPointerException on a field the constructor sets. Find out why.
@Transactional
public final void chargeMonthlyFee(long accountId) {
public void chargeMonthlyFee(long accountId) {
TransactionTrace.record("FeeService.chargeMonthlyFee");
Account account = accounts.require(accountId);
account.withdraw(MONTHLY_FEE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.amigoscode.transactional.trace.TransactionTrace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/** Executes one standing order: a payment the customer set up in advance. */
@Service
Expand Down Expand Up @@ -32,6 +33,9 @@ public boolean execute(long fromId, long toId, long amount) {
accounts.save(to);
return true;
} catch (RuntimeException e) {
TransactionAspectSupport
.currentTransactionStatus()
.setRollbackOnly();
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.amigoscode.transactional.account.AccountRepository;
import com.amigoscode.transactional.trace.TransactionTrace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/** Moves money from one account to another. Both balances must change, or neither. */
Expand All @@ -18,6 +19,7 @@ public TransferService(AccountRepository accounts) {

/** The method the rest of the application calls. */
// TODO Task 2: a failed transfer loses money, even though moveMoney below "is @Transactional". Why? Fix it.
@Transactional
public void transfer(long fromId, long toId, long amount) {
TransactionTrace.record("TransferService.transfer");
if (fromId == toId) {
Expand All @@ -27,7 +29,6 @@ public void transfer(long fromId, long toId, long amount) {
}

/** Debits one account and credits the other, in one transaction. */
@Transactional
public void moveMoney(long fromId, long toId, long amount) {
TransactionTrace.record("TransferService.moveMoney");

Expand Down
33 changes: 33 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kostas.practice</groupId>
<artifactId>java-practice</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<modules>
<module>array-drills</module>
<module>bookstore-api</module>
<module>data-structure-drills</module>
<module>delivery-food-system</module>
<module>event-registration</module>
<module>fix-the-service</module>
<module>flight-feasibility</module>
<module>karat-drills</module>
<module>ledger</module>
<module>ledger-http</module>
<module>load-balancer</module>
<module>order-api</module>
<module>order-tracking</module>
<module>rate-limiter</module>
<module>rider-dispatch</module>
<module>secret-storage</module>
<module>sql-transfer</module>
<module>string-drills</module>
<module>todo-api</module>
<module>url-shortener</module>
</modules>
</project>