Skip to content

Commit 2ead92d

Browse files
author
Rudi Adianto
committed
BAEL-2081
1 parent c2c711f commit 2ead92d

13 files changed

Lines changed: 405 additions & 0 deletions

File tree

‎jta/pom.xml‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
9+
10+
<description>JEE JTA demo</description>
11+
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>
18+
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>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-jdbc</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.hsqldb</groupId>
47+
<artifactId>hsqldb</artifactId>
48+
<version>2.4.1</version>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.jtademo;
2+
3+
import com.baeldung.jtademo.services.TellerService;
4+
import org.hsqldb.jdbc.JDBCDataSourceFactory;
5+
import org.hsqldb.jdbc.pool.JDBCXADataSource;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
11+
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
import org.springframework.boot.jta.bitronix.BitronixXADataSourceWrapper;
13+
import org.springframework.boot.jta.bitronix.PoolingDataSourceBean;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.core.io.DefaultResourceLoader;
16+
import org.springframework.core.io.Resource;
17+
import org.springframework.jdbc.core.JdbcTemplate;
18+
import org.springframework.jdbc.core.ResultSetExtractor;
19+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
20+
import org.springframework.jdbc.datasource.init.ScriptUtils;
21+
import org.springframework.transaction.annotation.EnableTransactionManagement;
22+
import org.springframework.util.Assert;
23+
24+
import javax.sql.DataSource;
25+
import java.math.BigDecimal;
26+
import java.sql.Connection;
27+
import java.sql.SQLException;
28+
29+
@EnableAutoConfiguration
30+
@EnableTransactionManagement
31+
public class JtaDemoApplication {
32+
33+
@Bean("dataSourceAccount")
34+
public DataSource dataSource() throws Exception {
35+
return createHsqlXADatasource("jdbc:hsqldb:mem:accountDb");
36+
}
37+
38+
@Bean("dataSourceAudit")
39+
public DataSource dataSourceAudit() throws Exception {
40+
return createHsqlXADatasource("jdbc:hsqldb:mem:auditDb");
41+
}
42+
43+
private DataSource createHsqlXADatasource(String connectionUrl) throws Exception {
44+
JDBCXADataSource dataSource = new JDBCXADataSource();
45+
dataSource.setUrl(connectionUrl);
46+
dataSource.setUser("sa");
47+
BitronixXADataSourceWrapper wrapper = new BitronixXADataSourceWrapper();
48+
return wrapper.wrapDataSource(dataSource);
49+
}
50+
51+
@Bean("jdbcTemplateAccount")
52+
public JdbcTemplate jdbcTemplate(@Qualifier("dataSourceAccount") DataSource dataSource) {
53+
return new JdbcTemplate(dataSource);
54+
}
55+
56+
@Bean("jdbcTemplateAudit")
57+
public JdbcTemplate jdbcTemplateAudit(@Qualifier("dataSourceAudit") DataSource dataSource) {
58+
return new JdbcTemplate(dataSource);
59+
}
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.jtademo.dto;
2+
3+
import java.math.BigDecimal;
4+
5+
public class TransferLog {
6+
private String fromAccountId;
7+
private String toAccountId;
8+
private BigDecimal amount;
9+
10+
public TransferLog(String fromAccountId, String toAccountId, BigDecimal amount) {
11+
this.fromAccountId = fromAccountId;
12+
this.toAccountId = toAccountId;
13+
this.amount = amount;
14+
}
15+
16+
public String getFromAccountId() {
17+
return fromAccountId;
18+
}
19+
20+
public String getToAccountId() {
21+
return toAccountId;
22+
}
23+
24+
public BigDecimal getAmount() {
25+
return amount;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jtademo.services;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.jdbc.core.JdbcTemplate;
6+
import org.springframework.stereotype.Service;
7+
8+
import javax.transaction.Transactional;
9+
import java.math.BigDecimal;
10+
11+
@Service
12+
public class AuditService {
13+
14+
final JdbcTemplate jdbcTemplate;
15+
16+
@Autowired
17+
public AuditService(@Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplate) {
18+
this.jdbcTemplate = jdbcTemplate;
19+
}
20+
21+
@Transactional
22+
public void log(String fromAccount, String toAccount, BigDecimal amount) {
23+
jdbcTemplate.update("insert into AUDIT_LOG(FROM_ACCOUNT, TO_ACCOUNT, AMOUNT) values ?,?,?", fromAccount, toAccount, amount);
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.jtademo.services;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.jdbc.core.JdbcTemplate;
6+
import org.springframework.stereotype.Component;
7+
8+
import javax.annotation.Resource;
9+
import javax.transaction.UserTransaction;
10+
import java.math.BigDecimal;
11+
12+
@Component
13+
public class BankAccountManualTxService {
14+
@Resource
15+
UserTransaction userTransaction;
16+
17+
@Autowired
18+
@Qualifier("jdbcTemplateAccount")
19+
JdbcTemplate jdbcTemplate;
20+
21+
public void transfer(String fromAccountId, String toAccountId, BigDecimal amount) throws Exception {
22+
userTransaction.begin();
23+
jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE-? where ID=?", amount, fromAccountId);
24+
jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE+? where ID=?", amount, toAccountId);
25+
userTransaction.commit();
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.jtademo.services;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.jdbc.core.JdbcTemplate;
6+
import org.springframework.jdbc.core.ResultSetExtractor;
7+
import org.springframework.stereotype.Service;
8+
9+
import javax.transaction.Transactional;
10+
import java.math.BigDecimal;
11+
12+
@Service
13+
public class BankAccountService {
14+
15+
final JdbcTemplate jdbcTemplate;
16+
17+
@Autowired
18+
public BankAccountService(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTemplate) {
19+
this.jdbcTemplate = jdbcTemplate;
20+
}
21+
22+
@Transactional
23+
public void transfer(String fromAccountId, String toAccountId, BigDecimal amount) {
24+
jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE-? where ID=?", amount, fromAccountId);
25+
jdbcTemplate.update("update ACCOUNT set BALANCE=BALANCE+? where ID=?", amount, toAccountId);
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.jtademo.services;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
import javax.transaction.Transactional;
7+
import java.math.BigDecimal;
8+
9+
@Service
10+
public class TellerService {
11+
private final BankAccountService bankAccountService;
12+
private final AuditService auditService;
13+
14+
@Autowired
15+
public TellerService(BankAccountService bankAccountService, AuditService auditService) {
16+
this.bankAccountService = bankAccountService;
17+
this.auditService = auditService;
18+
}
19+
20+
@Transactional
21+
public void executeTransfer(String fromAccontId, String toAccountId, BigDecimal amount) {
22+
bankAccountService.transfer(fromAccontId, toAccountId, amount);
23+
auditService.log(fromAccontId, toAccountId, amount);
24+
}
25+
26+
@Transactional
27+
public void executeTransferFail(String fromAccontId, String toAccountId, BigDecimal amount) {
28+
bankAccountService.transfer(fromAccontId, toAccountId, amount);
29+
auditService.log(fromAccontId, toAccountId, amount);
30+
throw new RuntimeException("Something wrong, rollback!");
31+
}
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.jtademo.services;
2+
3+
import com.baeldung.jtademo.dto.TransferLog;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.core.io.DefaultResourceLoader;
7+
import org.springframework.core.io.Resource;
8+
import org.springframework.jdbc.core.JdbcTemplate;
9+
import org.springframework.jdbc.core.ResultSetExtractor;
10+
import org.springframework.jdbc.datasource.init.ScriptUtils;
11+
import org.springframework.stereotype.Component;
12+
13+
import javax.sql.DataSource;
14+
import java.math.BigDecimal;
15+
import java.sql.Connection;
16+
import java.sql.SQLException;
17+
18+
@Component
19+
public class TestHelper {
20+
final JdbcTemplate jdbcTemplateAccount;
21+
22+
final JdbcTemplate jdbcTemplateAudit;
23+
24+
@Autowired
25+
public TestHelper(@Qualifier("jdbcTemplateAccount") JdbcTemplate jdbcTemplateAccount, @Qualifier("jdbcTemplateAudit") JdbcTemplate jdbcTemplateAudit) {
26+
this.jdbcTemplateAccount = jdbcTemplateAccount;
27+
this.jdbcTemplateAudit = jdbcTemplateAudit;
28+
}
29+
30+
public void runAccountDbInit() throws SQLException {
31+
runScript("account.sql", jdbcTemplateAccount.getDataSource());
32+
}
33+
34+
public void runAuditDbInit() throws SQLException {
35+
runScript("audit.sql", jdbcTemplateAudit.getDataSource());
36+
}
37+
38+
private void runScript(String scriptName, DataSource dataSouorce) throws SQLException {
39+
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
40+
Resource script = resourceLoader.getResource(scriptName);
41+
try (Connection con = dataSouorce.getConnection()) {
42+
ScriptUtils.executeSqlScript(con, script);
43+
}
44+
}
45+
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+
}
52+
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+
}
59+
}

‎jta/src/main/resources/account.sql‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DROP SCHEMA PUBLIC CASCADE;
2+
3+
create table ACCOUNT (
4+
ID char(8) PRIMARY KEY,
5+
BALANCE NUMERIC(28,10)
6+
);
7+
8+
insert into ACCOUNT(ID, BALANCE) values ('a0000001', 1000);
9+
insert into ACCOUNT(ID, BALANCE) values ('a0000002', 2000);

‎jta/src/main/resources/application.properties‎

Whitespace-only changes.

0 commit comments

Comments
 (0)