Merge pull request #12018 from hkhan/JAVA-10626-move-jta-module

[JAVA-10626] Move jta module to spring-persistence
This commit is contained in:
kwoyke 2022-04-04 12:56:14 +02:00 committed by GitHub
commit ec1c82abb5
19 changed files with 59 additions and 124 deletions

View File

@ -1,6 +0,0 @@
## JTA
This module contains articles about the Java Transaction API (JTA).
### Relevant Articles:
- [Guide to Jakarta EE JTA](https://www.baeldung.com/jee-jta)

View File

@ -1,55 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>jta</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jta</name>
<packaging>jar</packaging>
<description>JEE JTA demo</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>${log4j2.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
</dependencies>
<properties>
<spring-boot.version>2.4.7</spring-boot.version>
<log4j2.version>2.17.1</log4j2.version>
</properties>
</project>

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="15 seconds" debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -6,6 +6,7 @@
/data /data
/src/main/webapp/WEB-INF/classes /src/main/webapp/WEB-INF/classes
*/META-INF/* */META-INF/*
/transaction-logs
# Packaged files # # Packaged files #
*.jar *.jar

View File

@ -8,6 +8,7 @@
- [Transactional Annotations: Spring vs. JTA](https://www.baeldung.com/spring-vs-jta-transactional) - [Transactional Annotations: Spring vs. JTA](https://www.baeldung.com/spring-vs-jta-transactional)
- [Test a Mock JNDI Datasource with Spring](https://www.baeldung.com/spring-mock-jndi-datasource) - [Test a Mock JNDI Datasource with Spring](https://www.baeldung.com/spring-mock-jndi-datasource)
- [Detecting If a Spring Transaction Is Active](https://www.baeldung.com/spring-transaction-active) - [Detecting If a Spring Transaction Is Active](https://www.baeldung.com/spring-transaction-active)
- [Guide to Jakarta EE JTA](https://www.baeldung.com/jee-jta)
### Eclipse Config ### Eclipse Config
After importing the project into Eclipse, you may see the following error: After importing the project into Eclipse, you may see the following error:

View File

@ -51,6 +51,21 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<version>${spring-boot-starter.version}</version> <version>${spring-boot-starter.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
<version>${spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
@ -79,15 +94,13 @@
</dependencies> </dependencies>
<properties> <properties>
<!-- Spring --> <org.springframework.version>5.3.18</org.springframework.version>
<org.springframework.version>5.2.4.RELEASE</org.springframework.version> <spring-boot-starter.version>2.6.6</spring-boot-starter.version>
<spring-boot-starter.version>2.3.3.RELEASE</spring-boot-starter.version>
<!-- persistence -->
<persistence-api.version>2.2</persistence-api.version> <persistence-api.version>2.2</persistence-api.version>
<transaction-api.version>1.3</transaction-api.version> <transaction-api.version>1.3</transaction-api.version>
<spring-data-jpa.version>2.2.7.RELEASE</spring-data-jpa.version> <spring-data-jpa.version>2.2.7.RELEASE</spring-data-jpa.version>
<!-- simple-jndi -->
<simple-jndi.version>0.23.0</simple-jndi.version> <simple-jndi.version>0.23.0</simple-jndi.version>
<hsqldb.version>2.5.2</hsqldb.version>
</properties> </properties>
</project> </project>

View File

@ -3,7 +3,7 @@ package com.baeldung.jtademo;
import org.hsqldb.jdbc.pool.JDBCXADataSource; import org.hsqldb.jdbc.pool.JDBCXADataSource;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.jta.bitronix.BitronixXADataSourceWrapper; import org.springframework.boot.jta.atomikos.AtomikosXADataSourceWrapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -32,7 +32,7 @@ public class JtaDemoApplication {
JDBCXADataSource dataSource = new JDBCXADataSource(); JDBCXADataSource dataSource = new JDBCXADataSource();
dataSource.setUrl(connectionUrl); dataSource.setUrl(connectionUrl);
dataSource.setUser("sa"); dataSource.setUser("sa");
BitronixXADataSourceWrapper wrapper = new BitronixXADataSourceWrapper(); AtomikosXADataSourceWrapper wrapper = new AtomikosXADataSourceWrapper();
return wrapper.wrapDataSource(dataSource); return wrapper.wrapDataSource(dataSource);
} }

View File

@ -4,7 +4,6 @@ import com.baeldung.jtademo.dto.TransferLog;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -24,10 +23,17 @@ public class AuditService {
} }
public TransferLog lastTransferLog() { public TransferLog lastTransferLog() {
return jdbcTemplate.query("select FROM_ACCOUNT,TO_ACCOUNT,AMOUNT from AUDIT_LOG order by ID desc", (ResultSetExtractor<TransferLog>) (rs) -> { return jdbcTemplate.query(
if (!rs.next()) "select FROM_ACCOUNT,TO_ACCOUNT,AMOUNT from AUDIT_LOG order by ID desc",
rs -> {
if (!rs.next()) {
return null; return null;
return new TransferLog(rs.getString(1), rs.getString(2), BigDecimal.valueOf(rs.getDouble(3))); }
return new TransferLog(
rs.getString(1),
rs.getString(2),
BigDecimal.valueOf(rs.getDouble(3))
);
}); });
} }
} }

View File

@ -3,7 +3,6 @@ package com.baeldung.jtademo.services;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -24,9 +23,12 @@ public class BankAccountService {
} }
public BigDecimal balanceOf(String accountId) { public BigDecimal balanceOf(String accountId) {
return jdbcTemplate.query("select BALANCE from ACCOUNT where ID=?", new Object[] { accountId }, (ResultSetExtractor<BigDecimal>) (rs) -> { return jdbcTemplate.query(
"select BALANCE from ACCOUNT where ID=?",
new Object[] { accountId },
rs -> {
rs.next(); rs.next();
return new BigDecimal(rs.getDouble(1)); return BigDecimal.valueOf(rs.getDouble(1));
}); });
} }
} }

View File

@ -32,10 +32,10 @@ public class TestHelper {
runScript("audit.sql", jdbcTemplateAudit.getDataSource()); runScript("audit.sql", jdbcTemplateAudit.getDataSource());
} }
private void runScript(String scriptName, DataSource dataSouorce) throws SQLException { private void runScript(String scriptName, DataSource dataSource) throws SQLException {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
Resource script = resourceLoader.getResource(scriptName); Resource script = resourceLoader.getResource(scriptName);
try (Connection con = dataSouorce.getConnection()) { try (Connection con = dataSource.getConnection()) {
ScriptUtils.executeSqlScript(con, script); ScriptUtils.executeSqlScript(con, script);
} }
} }

View File

@ -0,0 +1 @@
spring.jta.atomikos.properties.log-base-name=atomikos-log

View File

@ -5,19 +5,19 @@ import com.baeldung.jtademo.services.AuditService;
import com.baeldung.jtademo.services.BankAccountService; import com.baeldung.jtademo.services.BankAccountService;
import com.baeldung.jtademo.services.TellerService; import com.baeldung.jtademo.services.TellerService;
import com.baeldung.jtademo.services.TestHelper; import com.baeldung.jtademo.services.TestHelper;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.math.BigDecimal; import java.math.BigDecimal;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
@RunWith(SpringRunner.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = JtaDemoApplication.class) @SpringBootTest(classes = JtaDemoApplication.class)
public class JtaDemoUnitTest { public class JtaDemoUnitTest {
@Autowired @Autowired
@ -32,31 +32,31 @@ public class JtaDemoUnitTest {
@Autowired @Autowired
AuditService auditService; AuditService auditService;
@Before @BeforeEach
public void beforeTest() throws Exception { public void beforeTest() throws Exception {
testHelper.runAuditDbInit(); testHelper.runAuditDbInit();
testHelper.runAccountDbInit(); testHelper.runAccountDbInit();
} }
@Test @Test
public void givenAnnotationTx_whenNoException_thenAllCommitted() throws Exception { public void givenAnnotationTx_whenNoException_thenAllCommitted() {
tellerService.executeTransfer("a0000001", "a0000002", BigDecimal.valueOf(500)); tellerService.executeTransfer("a0000001", "a0000002", BigDecimal.valueOf(500));
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500)); assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500)); assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
TransferLog lastTransferLog = auditService.lastTransferLog(); TransferLog lastTransferLog = auditService.lastTransferLog();
assertThat(lastTransferLog).isNotNull();
assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001"); assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001");
assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002"); assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002");
assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500)); assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500));
} }
@Test @Test
public void givenAnnotationTx_whenException_thenAllRolledBack() throws Exception { public void givenAnnotationTx_whenException_thenAllRolledBack() {
assertThatThrownBy(() -> { assertThatThrownBy(
tellerService.executeTransfer("a0000002", "a0000001", BigDecimal.valueOf(100000)); () -> tellerService.executeTransfer("a0000002", "a0000001", BigDecimal.valueOf(100000))
}).hasMessage("Insufficient fund."); ).hasMessage("Insufficient fund.");
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000)); assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000)); assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));
@ -67,22 +67,21 @@ public class JtaDemoUnitTest {
public void givenProgrammaticTx_whenCommit_thenAllCommitted() throws Exception { public void givenProgrammaticTx_whenCommit_thenAllCommitted() throws Exception {
tellerService.executeTransferProgrammaticTx("a0000001", "a0000002", BigDecimal.valueOf(500)); tellerService.executeTransferProgrammaticTx("a0000001", "a0000002", BigDecimal.valueOf(500));
BigDecimal result = accountService.balanceOf("a0000001");
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500)); assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500)); assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
TransferLog lastTransferLog = auditService.lastTransferLog(); TransferLog lastTransferLog = auditService.lastTransferLog();
assertThat(lastTransferLog).isNotNull();
assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001"); assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001");
assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002"); assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002");
assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500)); assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500));
} }
@Test @Test
public void givenProgrammaticTx_whenRollback_thenAllRolledBack() throws Exception { public void givenProgrammaticTx_whenRollback_thenAllRolledBack() {
assertThatThrownBy(() -> { assertThatThrownBy(
tellerService.executeTransferProgrammaticTx("a0000002", "a0000001", BigDecimal.valueOf(100000)); () -> tellerService.executeTransferProgrammaticTx("a0000002", "a0000001", BigDecimal.valueOf(100000))
}).hasMessage("Insufficient fund."); ).hasMessage("Insufficient fund.");
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000)); assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000)); assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));

View File

@ -476,7 +476,6 @@
<module>json-2</module> <module>json-2</module>
<module>json-path</module> <module>json-path</module>
<module>jsoup</module> <module>jsoup</module>
<module>jta</module>
<module>kubernetes</module> <module>kubernetes</module>
<module>ksqldb</module> <module>ksqldb</module>
<!-- <module>lagom</module> --> <!-- Not a maven project --> <!-- <module>lagom</module> --> <!-- Not a maven project -->
@ -963,7 +962,6 @@
<module>json-2</module> <module>json-2</module>
<module>json-path</module> <module>json-path</module>
<module>jsoup</module> <module>jsoup</module>
<module>jta</module>
<module>ksqldb</module> <module>ksqldb</module>