这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
@@ -0,0 +1,15 @@
package com.baeldung.transactional;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baeldung.transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderService {

@Autowired
private TestOrderRepository repository;

@Transactional
public void createOrder(TestOrder order) {
repository.save(order);
}

@Transactional
public void createOrderPublic(TestOrder order) {
repository.save(order);
throw new RuntimeException("Rollback createOrderPublic");
}

@Transactional
void createOrderPackagePrivate(TestOrder order) {
repository.save(order);
throw new RuntimeException("Rollback createOrderPackagePrivate");
}

@Transactional
protected void createOrderProtected(TestOrder order) {
repository.save(order);
throw new RuntimeException("Rollback createOrderProtected");
}

@Transactional
private void createOrderPrivate(TestOrder order) {
repository.save(order);
throw new RuntimeException("Rollback createOrderPrivate");
}

public void callPrivate(TestOrder order) {
createOrderPrivate(order);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.transactional;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "test_order")
public class TestOrder {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TestOrderRepository extends JpaRepository<TestOrder, Long> {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Very clean

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.baeldung.transactional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = { Application.class, OrderService.class, TestOrderRepository.class })
class TestOrderServiceTest {

@Autowired
private OrderService underTest;

@Autowired
private TestOrderRepository repository;

@AfterEach
void afterEach() {
repository.deleteAll();
}

@Test
void givenPublicTransactionalMethod_whenCallingIt_thenShouldRollbackOnException() {
assertThat(repository.findAll()).isEmpty();

assertThatThrownBy(() -> underTest.createOrderPublic(new TestOrder())).isNotNull();

assertThat(repository.findAll()).isEmpty();
}

@Test
void givenPackagePrivateTransactionalMethod_whenCallingIt_thenShouldRollbackOnException() {
assertThat(repository.findAll()).isEmpty();

assertThatThrownBy(() -> underTest.createOrderPackagePrivate(new TestOrder())).isNotNull();

assertThat(repository.findAll()).isEmpty();
}

@Test
void givenProtectedTransactionalMethod_whenCallingIt_thenShouldRollbackOnException() {
assertThat(repository.findAll()).isEmpty();

assertThatThrownBy(() -> underTest.createOrderProtected(new TestOrder())).isNotNull();

assertThat(repository.findAll()).isEmpty();
}

@Test
void givenPrivateTransactionalMethod_whenCallingIt_thenShouldNotRollbackOnException() {
assertThat(repository.findAll()).isEmpty();

assertThatThrownBy(() -> underTest.callPrivate(new TestOrder())).isNotNull();

assertThat(repository.findAll()).hasSize(1);
}
}