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(); }