Announcement Announcement Module
Collapse
No announcement yet.
Spring Transactional Annotation issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Transactional Annotation issue

    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

  • #2
    Let me add some more information about the issue.

    Here are my DAO methods. Both DAO methods are in separate classes which are called from above mentioned Service method.

    Code:
    @Transactional
    public Integer saveLocation(LocationDto locationDto) {
    		
    	Session session = sessionFactory.openSession();
    
    	Integer locationId = null;
    		
    	Location location = new Location();
    		
    	dtoToEntity(locationDto, location);
    		
    	try {
    		locationId = (Integer) session.save(location);
    	} catch (Exception ex) {
    		ex.printStackTrace();
    	} finally {
    		if(session.isOpen())
    			session.close();
    	}
    		
    	return locationId;
    }
    Code:
    @Transactional
    public Integer saveLocationMapping(LocationMappingDto locationMappingDto) {
    	Integer mappingId = null;
    		
    	Session session = sessionFactory.openSession();
    		
    	LocationMapping locationMapping = new LocationMapping();
    		
    	dtoToEntity(locationMappingDto, locationMapping);
    		
    	try {
    		mappingId = (Integer) session.save(locationMapping);
    	} catch (Exception ex) {
    		ex.printStackTrace();
    	} finally {
    		if(session.isOpen())
    			session.close();
    	}
    		
    	return mappingId;
    }
    These two methods persists data at their own call, instead of persisting to the database after operation completed from both DAO. First method call persists first & when second method called second DAO operation will be persisted.

    Comment

    Working...
    X