Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Spring Transactional Annotation issue

$
0
0
I am using annotation based Spring IOC & Transaction in my web application with Hibernate ORM.

During the development I came across a situation where two separate DAO methods perform database persistence operation & this situation must either success or entirely fail, no single operation should be persisted & other remain with some error. For this situation I've put an annotation on Service layer.

Here is a snippet of Service class:
Code:

@Transactional
public Integer saveLocation(LocationDto locationDto) {
        Integer locationId = locationDao.saveLocation(locationDto);

        if(locationId != null) {
                LocationMappingDto locationMappingDto = new LocationMappingDto();

                locationMappingDto.setLocationId(locationId);
                locationMappingDto.setLocationParentId(locationDto.getLocationId());

                if(locationMappingDao.saveLocationMapping(locationMappingDto) != null) {
                        return locationId;
                }
        }
        return locationId;
}

In DAO save operation is performed using Hibernate's Session. After each operation it will save the entry using save() method of Session object. So, it will call save twice, one from locationDao and another from locationMappingDao.

Issue:
It is saving first entry persisted it to the database & sequentially save second entry to the DB. As per Transaction concept, both entry should be persisted to the database at a single time, otherwise ACID property will not be maintained.

Please suggest any way to solve this problem. Also suggest if I am wrong somewhere to declare annotation.

Let me know if require more information regarding the same.

Thanks

Viewing all articles
Browse latest Browse all 297

Trending Articles