0

I am using @Transactional annotation to test inserting some data into one of the tables through a Spring application. My test succeeds, but I do not see any data being inserted into the table.

Here are the relevant code snippets:

MyTest:

@TransactionConfiguration(transactionManager="MyTxManager")
@Transactional
public class MyTest {
    @Autowired
    private DataProvider provider;

    @Test
    @Transactional
    void testInsert() {
        Order purchaseOrder = new Order("ID1", "LER", "VDR1", 0, );
        provider.addRow(purchaseOrder);
    }

}

DataProvider:

public class DataProvider extends DatabaseProvider {

    // some stuff...
    @Transactional
    public void insertRow(Order purchaseOrder) {
        /* 
         * SessionFactory is got through autowiring 
         * and is working perfectly fine.
         */
        Session session = sessionFactory.getCurrentSession();

        // This should save the row into the table - which it doesn't :(
        session.save(purchaseOrder);

    }

}

information-providers.xml

<bean id="DataProvider" class="com.util.DataProvider">
    <property name="sessionFactory" ref="MySessionFactory" />
</bean>

hibernate.xml containing the configuration of session factory

 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
      parent="AbstractSessionFactory" depends-on="EnvironmentHelper">
    <property name="hibernateProperties">
      <props>
         <prop key="hibernate.connection.autocommit">true</prop>
         <prop key="hibernate.connection.provider_class">com.hibernate.ConnectionPool</prop>
         <prop key="hibernate.connection.driver_class">${driverClassName}</prop>
         <prop key="hibernate.connection.url">${databaseURL}</prop>
         <prop key="hibernate.connection.username">${databaseUsername}</prop>
         <prop key="hibernate.connection.password">${databasePassword}</prop>

         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.default_schema">${databaseDefaultSchema}</prop>
      </props>
    </property>
</bean>

<!-- Use Spring transactions for Hibernate -->
<tx:annotation-driven transaction-manager="MyTxManager" mode='proxy' proxy-target-class='true'/>

<bean id="MyTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="MySessionFactory" />
</bean>    
</beans>

3 Answers 3

2

I think the problem in rolling back the transactions. See here defaultRollback method. Try like this:

@TransactionConfiguration(transactionManager="MyTxManager", defaultRollback=false)
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that MyProvider myProviderInstance = new MyProvider(); the myProviderInstance isn't maintained by Spring container, you just new it, so the Spring can't do anything about it, definitely the transaction doesn't work. You should config the bean in Spring applicationcontext.xml and load it with ApplicationtContext, you can find dozens of sample after google it. And since you use Mysql you should make sure that you use innerDB mode or the transaction isn't supported by mysql.

2 Comments

I just added more relevant code snippet for this question. I am actually using information-provider to get the provider instance (please refer the code). Also, can you please elaborate on what you mean by: "innerDB mode"?
I'll check your code latter, this link explains the innerDB: dev.mysql.com/doc/refman/5.0/en/innodb-storage-engine.html
0

add the line <tx:annotation-driven/> to your spring configuration

1 Comment

It is already there. Please take a look at the updated code snippets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.