From 2d2a64acbe74fdb9da86e582362e6df28590db8c Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Fri, 25 Dec 2020 09:06:51 +0530 Subject: [PATCH 1/4] Added changes to show transaction roll back --- .../baeldung/spring/transaction/Course.java | 35 +++++++++++++++++ .../spring/transaction/CourseDao.java | 24 ++++++++++++ .../spring/transaction/CourseService.java | 38 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/Course.java create mode 100644 persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java create mode 100644 persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/Course.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/Course.java new file mode 100644 index 0000000000..5add1e4a99 --- /dev/null +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/Course.java @@ -0,0 +1,35 @@ +package com.baeldung.spring.transaction; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "course") +public class Course implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "id") + private Long id; + + public Course() { + } + + public Course(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + +} diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java new file mode 100644 index 0000000000..489784bb40 --- /dev/null +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.transaction; + +import java.sql.SQLException; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.stereotype.Repository; + +import com.baeldung.spring.hibernate.AbstractHibernateDao; + +@Repository +public class CourseDao extends AbstractHibernateDao { + public CourseDao() { + super(); + setClazz(Course.class); + } + + public Course createWithRuntimeException(final Course entity) { + throw new DataIntegrityViolationException("Throwing exception for demoing Rollback!!!"); + } + + public Course createWithCheckedException(final Course entity) throws SQLException { + throw new SQLException("Throwing exception for demoing Rollback!!!"); + } +} diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java new file mode 100644 index 0000000000..1478c5bd1d --- /dev/null +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java @@ -0,0 +1,38 @@ +package com.baeldung.spring.transaction; + +import java.sql.SQLException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.TransactionAspectSupport; + +@Service +public class CourseService { + + @Autowired + private CourseDao courseDao; + + @Transactional + public void createCourseDeclarativeWithRuntimeException(Course course) { + courseDao.createWithRuntimeException(course); + } + + @Transactional(rollbackFor = { SQLException.class }) + public void createCourseDeclarativeWithCheckedException(Course course) throws SQLException { + courseDao.createWithCheckedException(course); + } + + public void createCourseDefaultRatingProgramatic(Course course) { + try { + courseDao.createWithRuntimeException(course); + } catch (Exception e) { + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + } + } + + public Course findById(Long id) { + return courseDao.findOne(id); + } + +} From d44dacaa2b69659b1d257229650c2c7598c09761 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Sat, 26 Dec 2020 06:14:52 +0530 Subject: [PATCH 2/4] Removed unwanted method --- .../java/com/baeldung/spring/transaction/CourseService.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java index 1478c5bd1d..21f947a925 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java @@ -31,8 +31,4 @@ public class CourseService { } } - public Course findById(Long id) { - return courseDao.findOne(id); - } - } From 1b4e5782ec11316b254c8faec298dc66d433dfdc Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Mon, 4 Jan 2021 09:05:55 +0530 Subject: [PATCH 3/4] BAEL-4624: Review comments --- .../com/baeldung/spring/transaction/CourseDao.java | 11 ----------- .../baeldung/spring/transaction/CourseService.java | 10 +++++++--- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java index 489784bb40..adf138ba67 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseDao.java @@ -1,8 +1,5 @@ package com.baeldung.spring.transaction; -import java.sql.SQLException; - -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Repository; import com.baeldung.spring.hibernate.AbstractHibernateDao; @@ -13,12 +10,4 @@ public class CourseDao extends AbstractHibernateDao { super(); setClazz(Course.class); } - - public Course createWithRuntimeException(final Course entity) { - throw new DataIntegrityViolationException("Throwing exception for demoing Rollback!!!"); - } - - public Course createWithCheckedException(final Course entity) throws SQLException { - throw new SQLException("Throwing exception for demoing Rollback!!!"); - } } diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java index 21f947a925..400c7d4843 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java @@ -3,6 +3,7 @@ package com.baeldung.spring.transaction; import java.sql.SQLException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; @@ -15,17 +16,20 @@ public class CourseService { @Transactional public void createCourseDeclarativeWithRuntimeException(Course course) { - courseDao.createWithRuntimeException(course); + courseDao.create(course); + throw new DataIntegrityViolationException("Throwing exception for demoing Rollback!!!"); } @Transactional(rollbackFor = { SQLException.class }) public void createCourseDeclarativeWithCheckedException(Course course) throws SQLException { - courseDao.createWithCheckedException(course); + courseDao.create(course); + throw new SQLException("Throwing exception for demoing Rollback!!!"); } public void createCourseDefaultRatingProgramatic(Course course) { try { - courseDao.createWithRuntimeException(course); + courseDao.create(course); + throw new DataIntegrityViolationException("Throwing exception for demoing Rollback!!!"); } catch (Exception e) { TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } From 3bf9bf40948c308eeaaed1e9dc5943d37bfe98c8 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Tue, 5 Jan 2021 20:17:31 +0530 Subject: [PATCH 4/4] Added no roll back example --- .../java/com/baeldung/spring/transaction/CourseService.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java index 400c7d4843..05e3f32cee 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/spring/transaction/CourseService.java @@ -35,4 +35,10 @@ public class CourseService { } } + @Transactional(noRollbackFor = { SQLException.class }) + public void createCourseDeclarativeWithNoRollBack(Course course) throws SQLException { + courseDao.create(course); + throw new SQLException("Throwing exception for demoing Rollback!!!"); + } + }