Skip to content

Commit 9325f26

Browse files
committed
revision
- @transaction and programmatic using the same example case. - XML format
1 parent 33b6562 commit 9325f26

8 files changed

Lines changed: 143 additions & 132 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.jta;
2+
3+
public class AuditService {
4+
5+
}

‎jta/pom.xml‎

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<groupId>com.baeldung</groupId>
6-
<artifactId>jta-demo</artifactId>
7-
<version>1.0-SNAPSHOT</version>
8-
<packaging>jar</packaging>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>jta-demo</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
99

10-
<description>JEE JTA demo</description>
10+
<description>JEE JTA demo</description>
1111

12-
<parent>
13-
<groupId>org.springframework.boot</groupId>
14-
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>2.0.4.RELEASE</version>
16-
<relativePath/>
17-
</parent>
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.4.RELEASE</version>
16+
<relativePath/>
17+
</parent>
1818

19-
<properties>
20-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22-
<java.version>1.8</java.version>
23-
</properties>
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
2424

25-
<dependencies>
26-
<dependency>
27-
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
29-
</dependency>
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
29+
</dependency>
3030
<dependency>
3131
<groupId>org.springframework.boot</groupId>
3232
<artifactId>spring-boot-starter-jdbc</artifactId>
3333
</dependency>
34-
<dependency>
35-
<groupId>org.springframework.boot</groupId>
36-
<artifactId>spring-boot-starter</artifactId>
37-
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter</artifactId>
37+
</dependency>
3838

39-
<dependency>
40-
<groupId>org.springframework.boot</groupId>
41-
<artifactId>spring-boot-starter-test</artifactId>
42-
<scope>test</scope>
43-
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
4444

4545
<dependency>
4646
<groupId>org.hsqldb</groupId>
@@ -49,40 +49,40 @@
4949
</dependency>
5050
</dependencies>
5151

52-
<profiles>
53-
<profile>
54-
<id>autoconfiguration</id>
55-
<build>
56-
<plugins>
57-
<plugin>
58-
<groupId>org.apache.maven.plugins</groupId>
59-
<artifactId>maven-surefire-plugin</artifactId>
60-
<executions>
61-
<execution>
62-
<phase>integration-test</phase>
63-
<goals>
64-
<goal>test</goal>
65-
</goals>
66-
<configuration>
67-
<excludes>
68-
<exclude>**/*LiveTest.java</exclude>
69-
<exclude>**/*IntegrationTest.java</exclude>
70-
<exclude>**/*IntTest.java</exclude>
71-
</excludes>
72-
<includes>
73-
<include>**/AutoconfigurationTest.java</include>
74-
</includes>
75-
</configuration>
76-
</execution>
77-
</executions>
78-
<configuration>
79-
<systemPropertyVariables>
80-
<test.mime>json</test.mime>
81-
</systemPropertyVariables>
82-
</configuration>
83-
</plugin>
84-
</plugins>
85-
</build>
86-
</profile>
87-
</profiles>
52+
<profiles>
53+
<profile>
54+
<id>autoconfiguration</id>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<executions>
61+
<execution>
62+
<phase>integration-test</phase>
63+
<goals>
64+
<goal>test</goal>
65+
</goals>
66+
<configuration>
67+
<excludes>
68+
<exclude>**/*LiveTest.java</exclude>
69+
<exclude>**/*IntegrationTest.java</exclude>
70+
<exclude>**/*IntTest.java</exclude>
71+
</excludes>
72+
<includes>
73+
<include>**/AutoconfigurationTest.java</include>
74+
</includes>
75+
</configuration>
76+
</execution>
77+
</executions>
78+
<configuration>
79+
<systemPropertyVariables>
80+
<test.mime>json</test.mime>
81+
</systemPropertyVariables>
82+
</configuration>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</profile>
87+
</profiles>
8888
</project>

‎jta/src/main/java/com/baeldung/jtademo/services/AuditService.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.baeldung.jtademo.services;
22

3+
import com.baeldung.jtademo.dto.TransferLog;
34
import org.springframework.beans.factory.annotation.Autowired;
45
import org.springframework.beans.factory.annotation.Qualifier;
56
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.jdbc.core.ResultSetExtractor;
68
import org.springframework.stereotype.Service;
79

810
import javax.transaction.Transactional;
@@ -18,8 +20,14 @@ public AuditService(@Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplate) {
1820
this.jdbcTemplate = jdbcTemplate;
1921
}
2022

21-
@Transactional
2223
public void log(String fromAccount, String toAccount, BigDecimal amount) {
2324
jdbcTemplate.update("insert into AUDIT_LOG(FROM_ACCOUNT, TO_ACCOUNT, AMOUNT) values ?,?,?", fromAccount, toAccount, amount);
2425
}
26+
27+
public TransferLog lastTransferLog() {
28+
return jdbcTemplate.query("select FROM_ACCOUNT,TO_ACCOUNT,AMOUNT from AUDIT_LOG order by ID desc", (ResultSetExtractor<TransferLog>) (rs) -> {
29+
if(!rs.next()) return null;
30+
return new TransferLog(rs.getString(1), rs.getString(2), BigDecimal.valueOf(rs.getDouble(3)));
31+
});
32+
}
2533
}

‎jta/src/main/java/com/baeldung/jtademo/services/BankAccountManualTxService.java‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎jta/src/main/java/com/baeldung/jtademo/services/BankAccountService.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Qualifier;
55
import org.springframework.jdbc.core.JdbcTemplate;
6+
import org.springframework.jdbc.core.ResultSetExtractor;
67
import org.springframework.stereotype.Service;
78

89
import javax.transaction.Transactional;
@@ -18,9 +19,15 @@ public BankAccountService(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTem
1819
this.jdbcTemplate = jdbcTemplate;
1920
}
2021

21-
@Transactional
2222
public void transfer(String fromAccountId, String toAccountId, BigDecimal amount) {
2323
jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE-? where ID=?", amount, fromAccountId);
2424
jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE+? where ID=?", amount, toAccountId);
2525
}
26+
27+
public BigDecimal balanceOf(String accountId) {
28+
return jdbcTemplate.query("select BALANCE from ACCOUNT where ID=?", new Object[] { accountId }, (ResultSetExtractor<BigDecimal>) (rs) -> {
29+
rs.next();
30+
return new BigDecimal(rs.getDouble(1));
31+
});
32+
}
2633
}

‎jta/src/main/java/com/baeldung/jtademo/services/TellerService.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,42 @@
44
import org.springframework.stereotype.Service;
55

66
import javax.transaction.Transactional;
7+
import javax.transaction.UserTransaction;
78
import java.math.BigDecimal;
89

910
@Service
1011
public class TellerService {
1112
private final BankAccountService bankAccountService;
1213
private final AuditService auditService;
14+
private final UserTransaction userTransaction;
1315

1416
@Autowired
15-
public TellerService(BankAccountService bankAccountService, AuditService auditService) {
17+
public TellerService(BankAccountService bankAccountService, AuditService auditService, UserTransaction userTransaction) {
1618
this.bankAccountService = bankAccountService;
1719
this.auditService = auditService;
20+
this.userTransaction = userTransaction;
1821
}
1922

2023
@Transactional
2124
public void executeTransfer(String fromAccontId, String toAccountId, BigDecimal amount) {
2225
bankAccountService.transfer(fromAccontId, toAccountId, amount);
2326
auditService.log(fromAccontId, toAccountId, amount);
27+
BigDecimal balance = bankAccountService.balanceOf(fromAccontId);
28+
if(balance.compareTo(BigDecimal.ZERO) <= 0) {
29+
throw new RuntimeException("Insufficient fund.");
30+
}
2431
}
2532

26-
@Transactional
27-
public void executeTransferFail(String fromAccontId, String toAccountId, BigDecimal amount) {
33+
public void executeTransferProgrammaticTx(String fromAccontId, String toAccountId, BigDecimal amount) throws Exception {
34+
userTransaction.begin();
2835
bankAccountService.transfer(fromAccontId, toAccountId, amount);
2936
auditService.log(fromAccontId, toAccountId, amount);
30-
throw new RuntimeException("Something wrong, rollback!");
37+
BigDecimal balance = bankAccountService.balanceOf(fromAccontId);
38+
if(balance.compareTo(BigDecimal.ZERO) <= 0) {
39+
userTransaction.rollback();
40+
throw new RuntimeException("Insufficient fund.");
41+
} else {
42+
userTransaction.commit();
43+
}
3144
}
3245
}

‎jta/src/main/java/com/baeldung/jtademo/services/TestHelper.java‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,5 @@ private void runScript(String scriptName, DataSource dataSouorce) throws SQLExce
4343
}
4444
}
4545

46-
public TransferLog lastTransferLog() {
47-
return jdbcTemplateAudit.query("select FROM_ACCOUNT,TO_ACCOUNT,AMOUNT from AUDIT_LOG order by ID desc", (ResultSetExtractor<TransferLog>) (rs) -> {
48-
if(!rs.next()) return null;
49-
return new TransferLog(rs.getString(1), rs.getString(2), BigDecimal.valueOf(rs.getDouble(3)));
50-
});
51-
}
5246

53-
public BigDecimal balanceOf(String accountId) {
54-
return jdbcTemplateAccount.query("select BALANCE from ACCOUNT where ID=?", new Object[] { accountId }, (ResultSetExtractor<BigDecimal>) (rs) -> {
55-
rs.next();
56-
return new BigDecimal(rs.getDouble(1));
57-
});
58-
}
5947
}
Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.baeldung.jtademo;
22

33
import com.baeldung.jtademo.dto.TransferLog;
4-
import com.baeldung.jtademo.services.BankAccountManualTxService;
5-
import com.baeldung.jtademo.services.TellerService;
6-
import com.baeldung.jtademo.services.TestHelper;
4+
import com.baeldung.jtademo.services.*;
75
import org.junit.Before;
86
import org.junit.Test;
97
import org.junit.runner.RunWith;
@@ -25,7 +23,10 @@ public class JtaDemoUnitTest {
2523
TellerService tellerService;
2624

2725
@Autowired
28-
BankAccountManualTxService bankAccountManualTxService;
26+
BankAccountService accountService;
27+
28+
@Autowired
29+
AuditService auditService;
2930

3031
@Before
3132
public void beforeTest() throws Exception {
@@ -34,37 +35,53 @@ public void beforeTest() throws Exception {
3435
}
3536

3637
@Test
37-
public void whenNoException_thenAllCommitted() throws Exception {
38+
public void givenAnnotationTx_whenNoException_thenAllCommitted() throws Exception {
3839
tellerService.executeTransfer("a0000001", "a0000002", BigDecimal.valueOf(500));
3940

40-
BigDecimal result = testHelper.balanceOf("a0000001");
41-
assertThat(testHelper.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
42-
assertThat(testHelper.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
41+
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
42+
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
4343

44-
TransferLog lastTransferLog = testHelper.lastTransferLog();
44+
TransferLog lastTransferLog = auditService.lastTransferLog();
4545
assertThat(lastTransferLog).isNotNull();
4646
assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001");
4747
assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002");
4848
assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500));
4949
}
5050

5151
@Test
52-
public void whenException_thenAllRolledBack() throws Exception {
52+
public void givenAnnotationTx_whenException_thenAllRolledBack() throws Exception {
5353
assertThatThrownBy(() -> {
54-
tellerService.executeTransferFail("a0000002", "a0000001", BigDecimal.valueOf(100));
55-
}).hasMessage("Something wrong, rollback!");
54+
tellerService.executeTransfer("a0000002", "a0000001", BigDecimal.valueOf(100000));
55+
}).hasMessage("Insufficient fund.");
56+
57+
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
58+
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));
59+
assertThat(auditService.lastTransferLog()).isNull();
60+
}
61+
62+
@Test
63+
public void givenProgrammaticTx_whenCommit_thenAllCommitted() throws Exception {
64+
tellerService.executeTransferProgrammaticTx("a0000001", "a0000002", BigDecimal.valueOf(500));
5665

57-
assertThat(testHelper.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
58-
assertThat(testHelper.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));
66+
BigDecimal result = accountService.balanceOf("a0000001");
67+
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
68+
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
5969

60-
assertThat(testHelper.lastTransferLog()).isNull();
70+
TransferLog lastTransferLog = auditService.lastTransferLog();
71+
assertThat(lastTransferLog).isNotNull();
72+
assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001");
73+
assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002");
74+
assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500));
6175
}
6276

6377
@Test
64-
public void givenBMT_whenNoException_thenAllCommitted() throws Exception {
65-
bankAccountManualTxService.transfer("a0000001", "a0000002", BigDecimal.valueOf(100));
78+
public void givenProgrammaticTx_whenRollback_thenAllRolledBack() throws Exception {
79+
assertThatThrownBy(() -> {
80+
tellerService.executeTransferProgrammaticTx("a0000002", "a0000001", BigDecimal.valueOf(100000));
81+
}).hasMessage("Insufficient fund.");
6682

67-
assertThat(testHelper.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(900));
68-
assertThat(testHelper.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2100));
83+
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
84+
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));
85+
assertThat(auditService.lastTransferLog()).isNull();
6986
}
7087
}

0 commit comments

Comments
 (0)