Update 12/12/2011
The application is making use of a Generic DAO class that can be found here.
Based on re-reading the spring manual on the usage of @Repository it would appear that I need to add this to my GenericDAO implementation in order for spring to handle the exception translations.
I added the @Repository to the GenericDAO but, results did not change. The parent object is still being persisted into the database.
Using Spring 2.5.6, JBoss 5.1 with its hibernate version, Spring MVC, MySQL
I am doing some testing to make sure that my transactions will get rolled back correctly in the event of an exception. In the below code the second method: createProfile will throw a NullPointerException which prevents the method from completing. I had expected that the transaction would of been rolled back. What I am finding is that user object is persisted into the database. What I want is that if the second operation fails then the first operation needs to roll back as well.
I have done some changes in which I changed the propagation to REQUIRED_NEW for the parent method but did not see any changes in the behavior.
I have turned on logging for spring transaction.
I have been looking over different posting here and reviewing what spring has to say on transactions as well. I seem to be missing something along the lines of transaction behavior.
What am I doing incorrect here.
I have the following defined
@Transactional(propagation = Propagation.SUPPORTS)
public class ProfileServiceImpl implements ProfileService
{
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public boolean createProfile(UserDTO pUserDTO, ContactInfoDTO pContactInfoDTO)
{
boolean retVal = false;
if(this.createProfile(pUserDTO))
{
if(this.addAddressToUser(pContactInfoDTO, pUserDTO.getId()))
{
retVal = true;
}
}
return retVal;
}
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public boolean createProfile(UserDTO userDTO)
{
boolean retVal = false;
if(this.getProfileQueries().isLoginUnique(userDTO.getLogin(),userDTO.getSiteInfoId()))
{
User user = new User();
BeanUtils.copyProperties(userDTO, user);
user.setPassword(this.stringDigester.digest(userDTO.getPassword()));
user.setSiteInfo(this.getSiteInfoDao().read(userDTO.getSiteInfoId()));
user.setSecurityLevel(SecurityLevel.ROLE_USER);
user.setStatus(Status.ACTIVE);
Long pk = this.getUserDao().create(user);
userDTO.setId(pk);
retVal = true;
}
return retVal;
}
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public boolean addAddressToUser(ContactInfoDTO pAddress, Long pProfileId)
{
boolean retVal = false;
if(null != pAddress && null != pProfileId)
{
ContactInfo contactInfo = new ContactInfo();
BeanUtils.copyProperties(pAddress, contactInfo);
User user = this.getUserDao().read(pProfileId);
contactInfo.setUser(user);
this.getContactInfoDao().create(contactInfo);
user.getUserAddress().setProfileAddress(contactInfo);
user.getUserAddress().setBillingAddress(contactInfo);
this.getUserDao().update(user);
retVal = true;
}
return retVal;
}
}
Data config
<!-- TRANSACTION MANAGER-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- ANNOTATION DRIVEN TRANSACTIONS -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/MySqlDS"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="jndiDataSource"/>
<property name="annotatedClasses">
<list>
<value>com.vsg.dataaccess.user.entity.User</value>
<value>com.vsg.dataaccess.user.entity.ContactInfo</value>
<value>com.vsg.dataaccess.user.entity.UserAddress</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="org.hibernate.envers.audit_table_suffix">_aud</prop>
<prop key="org.hibernate.envers.revision_field_name">rev_number</prop>
<prop key="org.hibernate.envers.revision_type_field_name">rev_type</prop>
<prop key="org.hibernate.envers.revision_on_collection_change">true</prop>
</props>
</property>
<property name="entityInterceptor">
<bean class="vsg.ecotrak.dataaccess.framework.hibernate.interceptor.AuditTrailInterceptor"/>
</property>
</bean>