BAEL-6852: Can @Transactional and @Async work together? (#16126)
* adding tests and repo classes * wrapping up logic] * removing test * removing sql files * changing to throw exception on optional] * introducing property in pom
This commit is contained in:
parent
a9f5f9fab5
commit
28bb26ceb2
|
@ -60,6 +60,11 @@
|
|||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring.tx.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -84,6 +89,7 @@
|
|||
<modelmapper.version>3.2.0</modelmapper.version>
|
||||
<commons-codec.version>1.16.1</commons-codec.version>
|
||||
<lombok.version>1.18.30</lombok.version>
|
||||
<spring.tx.version>6.1.4</spring.tx.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.transactionalandasync;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
@Table(name = "account")
|
||||
@Data
|
||||
public class Account {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
|
||||
@Column(name = "balance")
|
||||
private BigDecimal balance;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.transactionalandasync;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface AccountRepository extends JpaRepository<Account, Long> {
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.transactionalandasync;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@Transactional
|
||||
public class AccountService {
|
||||
|
||||
private final AccountRepository accountRepository;
|
||||
|
||||
@Async
|
||||
public void transferAsync(Long depositorId, Long favoredId, BigDecimal amount) {
|
||||
transfer(depositorId, favoredId, amount);
|
||||
|
||||
printReceipt();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void transfer(Long depositorId, Long favoredId, BigDecimal amount) {
|
||||
Account depositorAccount = accountRepository.findById(depositorId)
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
Account favoredAccount = accountRepository.findById(favoredId)
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
|
||||
depositorAccount.setBalance(depositorAccount.getBalance().subtract(amount));
|
||||
favoredAccount.setBalance(favoredAccount.getBalance().add(amount));
|
||||
|
||||
accountRepository.save(depositorAccount);
|
||||
accountRepository.save(favoredAccount);
|
||||
}
|
||||
|
||||
public void printReceipt() {
|
||||
// logic to print the receipt
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.transactionalandasync;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BankAccountApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BankAccountApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue