Results 1 to 10 of 18

Thread: Simple Hibernate Transaction Rollback test

Hybrid View

  1. #1

    Question 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

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    Can you post the rest of the config file ?

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    As Andrei said it would be useful to see the configuration and also the log file contents. I would have a look at TransactionTemplate though.
    http://www.springframework.org/docs/...n-programmatic
    Last edited by karldmoore; Aug 29th, 2007 at 11:28 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  4. #4

    Default

    hi,

    tx for your interest.
    Actualy I already tried TransactionTemplate but with the same result :-(. Before that I tried the declerative approach but also no go.

    The 'full' config:
    Code:
    <beans>
    	<bean name="projectDAO" class="com.laco.planningtool.dao.ProjectDAOImpl"
    		autowire="byName" />
    
    	<bean name="projectService"
    		class="com.laco.planningtool.service.ProjectServiceImpl">
    		<property name="projectDAO" ref="projectDAO" />
    		<property name="transactionManager" ref="transactionManager" />
    	</bean>
    
    	<bean name="userDAO" class="com.laco.planningtool.dao.UserDAOImpl" autowire="byName" />
    
    	<bean name="userService" class="com.laco.planningtool.service.UserServiceImpl">
    		<property name="userDAO" ref="userDAO" />
    	</bean>
    
    	<bean name="resourceDAO" class="com.laco.planningtool.dao.ResourceDAOImpl"
    		autowire="byName" />
    
    	<bean name="resourceService"
    		class="com.laco.planningtool.service.ResourceServiceImpl">
    		<property name="resourceDAO" ref="resourceDAO" />
    	</bean>
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    </beans>
    tx again,
    Stijn

  5. #5
    Join Date
    Mar 2007
    Posts
    515

    Default

    The configuration looks ok to me.
    Do you have multiple session factories in your config ? What properties do your daos have ?
    Try performing a test by manual setting daos' properties.

  6. #6
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    I don't see the dataSource or the sessionFactory. Also, what database are you using?
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  7. #7

    Default

    the dao only has one property and that's the hibernatetemplate so I don't think that's related?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •