Reputation: 2009
I'm working with Spring 3 , and I use Hibernate3 as the O/R mapper.While testing my DAO classes I intended to declare my test classes as @Transactional, so that I could rollback the stuff in my DB. DAO classes work fine, however the rollback never happens. this is how my test class looks like:
@BeforeTransaction
public void createTestData() {
// instantiate User objects
}
@Before
public void insertTestData() {
jdbcTemplate.update("INSERT INTO USER VALUES" + user1);
jdbcTemplate.update("INSERT INTO USER VALUES" + user2);
jdbcTemplate.update("INSERT INTO USER VALUES" + user3);
}
@Test
@Rollback(true)
public void testCheckAvailability() {
boolean trueResult = userDao.checkAvailability("shaaadi");
boolean falseResult = userDao.checkAvailability("elthefar");
assertEquals("Invalid output", true, trueResult);
assertEquals("Invalid output", false, falseResult);
}
}
not to forget about the xml file contents:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="annotatedClasses">
<list>
<value>ir.cms.domain.user.User</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<bean id="userDao"
class="ir.cms.dao.user.impl.UserDaoImpl"
p:sessionFactory-ref="sessionFactory" />
</beans>
Finally , I should add that DAO classes are not transactional , and are only annotated as @Repository. I appreciate any suggestion :)
Upvotes: 1
Views: 490
Reputation: 2009
My Problem is finally solved. I was using MySQL, and InnoDB mode was off.
Upvotes: 1