black sensei
black sensei

Reputation: 6677

Spring 3.0.5 + hibernate 3.6 hibernate.show_sql not showing.Is it due to use of transaction?

i have a small maven project with

            <artifactId>spring-core</artifactId>
            <artifactId>spring-test</artifactId>
            <artifactId>spring-beans</artifactId>
            <artifactId>spring-context</artifactId>
            <artifactId>spring-aop</artifactId>
            <artifactId>spring-context-support</artifactId>
            <artifactId>spring-tx</artifactId>
            <artifactId>spring-orm</artifactId>  
            <artifactId>spring-web</artifactId>
            <artifactId>spring-webmvc</artifactId>
            <artifactId>spring-asm</artifactId>
            <artifactId>log4j</artifactId>
            <artifactId>hibernate-core</artifactId>
            <artifactId>hibernate-cglib-repack</artifactId>
            <artifactId>hsqldb</artifactId>


    <spring.version>3.0.5.RELEASE</spring.version>
    <hibernate.version>3.6.1.Final</hibernate.version>
    <hibernate-cglig-repack.version>2.1_3</hibernate-cglig-repack.version>
    <log4j.version>1.2.14</log4j.version>
    <javax-servlet-api.version>2.5</javax-servlet-api.version>
    <hsqldb.version>1.8.0.10</hsqldb.version>
    <mysql-connector.version>5.1.6</mysql-connector.version>
    <slf4j-log4j12.version>1.5.2</slf4j-log4j12.version>
    <slf4j-api.version>1.5.8</slf4j-api.version>
    <javaassist.version>3.7.ga</javaassist.version>

here is my applicationContext :

    <context:component-scan base-package="com.project.personal.admin.model"/>
<context:annotation-config />

<bean id="propertyconfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:mysql.hibernate.properties</value>
            <value>classpath:mysql.jdbc.properties</value>
        </list>
    </property>
</bean>
<context:annotation-config/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}"  />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>

            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.project.personal.admin.model.domain" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

mysql.hibernate.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create

here is a small test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/test-project-admin-config.xml"})
@TransactionConfiguration(defaultRollback=true)
@Transactional
public class UserDAOImplTest {

  @Resource(name="manager")
  Manager manager;

  @Autowired
  UserDAO userDAO;

  public UserDAOImplTest() {
  }



@Test
public void testSave() {
    User u1 = manager.CreateUser();
   //.....
    userDAO.save(u1);
    User expResult = u1;

    User result = (User)userDAO.getById(u1.getId());

    Assert.assertEquals(expResult, result);
    Assert.assertEquals(expResult.getId(), result.getId());

}

@Test
public void testUpdate(){

    User u2 = manager.CreateUser();
    //....
    u2.setPassword("mypassword");
    userDAO.save(u2);

    User fromdb =(User) userDAO.getById(u2.getId());

    fromdb.setEmail("[email protected]");
    userDAO.save(fromdb);

    User result = (User) userDAO.getById(fromdb.getId());

    Assert.assertNotNull(result);
    Assert.assertEquals(fromdb.getEmail(), result.getEmail());

}

}

So far everything runs file test are successfull, but just that there is not sql output, i'm wondering what might have caused that.Since this is the first time i'm relying on trasactionConfiguration i thought it's might be the cause of sql not showing other than that other previous project show the sql.

How can i address it as in forcing the show of the sql scripts(not using log4j yet) thanks for reading.

Upvotes: 1

Views: 8590

Answers (1)

axtavt
axtavt

Reputation: 242686

You use

@TransactionConfiguration(defaultRollback=true) 
@Transactional 

Since your transactions are rolled back by default, it's possbile that Hibernate doesn't execute any SQL statements, because session flush doesn't happen before rollback.

You can disable rollback of test transactions by setting defaultRollback = false (or @Rollback(false) at method level).

If you want Hibernate to issue real SQL statements, but don't want to commit your test transactions, you can call flush() at the end of your test methods.

Upvotes: 2

Related Questions