BAEL-3849 Implemented example code for comparing Transactional. (#9261)

This commit is contained in:
Cicio Flaviu 2020-05-10 08:43:51 +03:00 committed by GitHub
parent d6a4b3c916
commit af387990c8
5 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.transactional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class TransactionalCompareApplication {
public static void main(String[] args) {
SpringApplication.run(TransactionalCompareApplication.class, args);
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.spring.transactional.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String make;
private String model;
public Car() {
}
public Car(Long id, String make, String model) {
this.id = id;
this.make = make;
this.model = model;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Override
public String toString() {
return "Car{" +
"id=" + id +
", make='" + make + '\'' +
", model='" + model + '\'' +
'}';
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.spring.transactional.repository;
import com.baeldung.spring.transactional.entity.Car;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CarRepository extends JpaRepository<Car, Long> {
}

View File

@ -0,0 +1,25 @@
package com.baeldung.spring.transactional.service;
import com.baeldung.spring.transactional.entity.Car;
import com.baeldung.spring.transactional.repository.CarRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityExistsException;
@Service
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.SUPPORTS, readOnly = false, timeout = 30)
public class CarService {
@Autowired
private CarRepository carRepository;
@Transactional(rollbackFor = IllegalArgumentException.class, noRollbackFor = EntityExistsException.class,
rollbackForClassName = "IllegalArgumentException", noRollbackForClassName = "EntityExistsException")
public Car save(Car car) {
return carRepository.save(car);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.spring.transactional.service;
import com.baeldung.spring.transactional.entity.Car;
import com.baeldung.spring.transactional.repository.CarRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.EntityExistsException;
import javax.transaction.Transactional;
@Service
@Transactional(Transactional.TxType.SUPPORTS)
public class RentalService {
@Autowired
private CarRepository carRepository;
@Transactional(rollbackOn = IllegalArgumentException.class, dontRollbackOn = EntityExistsException.class)
public Car rent(Car car) {
return carRepository.save(car);
}
}