Question

I have simple Spring MVC - Hibernate application, that worked fine (inserts & reads data) when connected to MySQL 5.5 database. On connecting the same application to Oracle 10g, it read records from database, but was not able to insert the records.

On debugging, I found that flushMode of the session was MANUAL. I explicitly wrote session.flush() after saving the record, and the code started to work correctly in both the databases.

My problem - As I understand from the documentation of Hibernate, the default flush mode is AUTO. I have not set it to manual in my code, but somehow it is getting set. How to control this?

I am using Spring 3.1.1 and Hibernate 3.5.6. Below are my root-context.xml and servlet-context.xml files.

servlet-context.xml:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<context:component-scan base-package="com.home.cfs"/>

<mvc:annotation-driven/>

root-context.xml:

<bean id="databaseDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:testdb"/>
    <property name="username" value="test_usr"/>
    <property name="password" value="test_usr"/>
     -->

    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/person"/>
    <property name="username" value="root"/>
    <property name="password" value="admin"/> 

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="databaseDataSource"/>
    <property name="packagesToScan" value="com.home.cfs"/>
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
    <property name="hibernateProperties">
        <props>
            <!-- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

Please let me know if more details are needed.

Thanks.

Was it helpful?

Solution

check whether you have turned on OpenSessionInViewFilter filter. That seems to be a likely culprit for this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top