BAEL-4624: Review comments

This commit is contained in:
Amitabh Tiwari 2021-01-04 09:05:55 +05:30
parent d44dacaa2b
commit 1b4e5782ec
2 changed files with 7 additions and 14 deletions

View File

@ -1,8 +1,5 @@
package com.baeldung.spring.transaction; package com.baeldung.spring.transaction;
import java.sql.SQLException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.baeldung.spring.hibernate.AbstractHibernateDao; import com.baeldung.spring.hibernate.AbstractHibernateDao;
@ -13,12 +10,4 @@ public class CourseDao extends AbstractHibernateDao<Course> {
super(); super();
setClazz(Course.class); 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!!!");
}
} }

View File

@ -3,6 +3,7 @@ package com.baeldung.spring.transaction;
import java.sql.SQLException; import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.transaction.interceptor.TransactionAspectSupport;
@ -15,17 +16,20 @@ public class CourseService {
@Transactional @Transactional
public void createCourseDeclarativeWithRuntimeException(Course course) { public void createCourseDeclarativeWithRuntimeException(Course course) {
courseDao.createWithRuntimeException(course); courseDao.create(course);
throw new DataIntegrityViolationException("Throwing exception for demoing Rollback!!!");
} }
@Transactional(rollbackFor = { SQLException.class }) @Transactional(rollbackFor = { SQLException.class })
public void createCourseDeclarativeWithCheckedException(Course course) throws SQLException { 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) { public void createCourseDefaultRatingProgramatic(Course course) {
try { try {
courseDao.createWithRuntimeException(course); courseDao.create(course);
throw new DataIntegrityViolationException("Throwing exception for demoing Rollback!!!");
} catch (Exception e) { } catch (Exception e) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
} }