Simple Hibernate Transaction Rollback test
hi,
I'm kinda new with spring and I wanted to try out the hibernate transactionmanager. The scenario is simple enough: I start a transaction (programmatically), add a Project to the DB and in the same transaction I add a User. The latter will throw (as intended) an exception because one of it's mandatory fields is null. In the catch-block I do a rollback and I would expect the insert of the project to have been rolled back. However this is not the case; the project is added to the DB and is not removed.
Source AbstractService;
Code:
public class AbstractService
{
protected PlatformTransactionManager transactionManager = null;
public void setTransactionManager(PlatformTransactionManager transactionManager)
{
this.transactionManager = transactionManager;
}
protected TransactionStatus startNewTransaction()
{
TransactionStatus status = null;
DefaultTransactionDefinition definition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
if (transactionManager != null)
{
status = transactionManager.getTransaction(definition);
}
return status;
}
}
ProjectServiceImpl extends AbstractService:
Source createProject (= the test method)
Code:
public void createProject(Project project, User creator)
{
TransactionStatus status = startNewTransaction();
try
{
project.setVersion(new Integer(1));
projectDAO.save(project); //does a simple hibernatetemplate.save
User u = new User();
projectDAO.save(u);
transactionManager.commit(status);
}
catch (Exception e)
{
transactionManager.rollback(status);
}
}
declaration of transactionmanager:
Code:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
I hope someone can help me out; been struggling way too long on this.
Stijn