Announcement Announcement Module
Collapse
No announcement yet.
Data does not commit. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data does not commit.

    Hi ,

    My app uses Struts(1.4) + Spring(1.1.1) + Hibernate(2.1)
    I have noticed that whenever any insert or update is done ,
    the data does not get commited ,

    following is the configuration details

    1. I use contextLoaderPlugin to integrate Struts and Spring

    <plug-in className="org.springframework.web.struts.ContextL oaderPlugIn">
    <set-property property="contextConfigLocation"
    value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml"/>
    </plug-in>

    2. I have following configuration for hibernate and Spring
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    <list>
    <!--<value>org/med/common/User.hbm.xml</value> -->
    <value>org/med/common/Responsibilities.hbm.xml</value>
    <value>org/med/common/core/model/Product.hbm.xml</value>
    <value>org/med/common/core/model/Order.hbm.xml</value>
    <value>org/med/common/core/model/OrderItem.hbm.xml</value>
    </list>
    </property>

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.O racleDialect</prop>
    <prop key="hibernate.connection.driver_class">oracle.jdb c.driver.OracleDriver</prop>
    <prop key="hibernate.connection.username">user</prop>
    <prop key="hibernate.connection.password">password</prop>
    <prop key="hibernate.connection.url">jdbc:oracle:thin:@l ocalhost:1521:sid</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="myTransactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory"><ref local="mySessionFactory"/></property>
    </bean>

    <!-- ***** ORDER SERVICE *****-->
    <bean id="orderService" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="myTransactionManager"/></property>
    <property name="target"><ref local="orderDAO"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly,-OrderException</prop>
    <prop key="save*">PROPAGATION_REQUIRED,-OrderException,-OrderMinimumAmountException</prop>
    <prop key="create*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <!-- Order DAO object: Hibernate implementation -->
    <bean id="orderDAO" class="org.med.common.core.dao.OrderDAO" singleton="false">
    <property name="sessionFactory"><ref local="mySessionFactory"/></property>
    </bean>

    3. In my hibernate DAO I do followng thing


    public void createOrder(Order order){
    getHibernateTemplate().saveOrUpdate(order);
    }



    When I run my application , I see following sql's being generate

    Hibernate: insert into beta_orders (order_date, price_total, id) values (?, ?, ?)
    Hibernate: insert into beta_order_items (amount, price, order_id, product_id, id) values (?, ?, ?, ?, ?)
    Hibernate: update beta_products set name=?, price=?, amount=? where id=?


    The problem is the data gets reflected in my DB once I shutdown the server,
    I guess Iam missing out some basic parameter in my configuration files like auto commit.

    Thanks
    -Hari

  • #2
    If your data is being commited to the database when the server is shutdown it would appear everything is okay and you just need a getHibernateTemplate().flush(); after the getHibernateTemplate().saveOrUpdate(order); statement to force hibernate to commit the data.

    Comment


    • #3
      but there isn't any
      getHibernateTemplate().flush();
      method..

      Comment


      • #4
        Hari,

        getHibernateTemplate().flush() does exist in Spring since 1.1. Are you sure you are using version 1.1.1?

        Comment


        • #5
          Omar,

          yes you were right , I had reference to old spring jars in eclipse , now I have set it to 1.1.

          I have added following line

          getHibernateTemplate().saveOrUpdate(order);
          >getHibernateTemplate.flush();

          but still the data does not commit,

          I guess we need not even add those lines , most of the sample applciations that I have seen , I don't see flush being called at all.

          any clues...

          -Hari

          Comment


          • #6
            Hari,

            Indeed, you do not need to use flush in this case.
            can you show the piece of configuration where you connect your DAO to your Service?

            Comment


            • #7
              here is the complete DAO code

              public class OrderDAO extends HibernateDaoSupport {
              public Product findProductByName(String productID){
              return (Product) getHibernateTemplate().get(Product.class, productID);
              }

              public void createOrder(Order order){
              getHibernateTemplate().saveOrUpdate(order);
              //getHibernateTemplate().flush();
              }
              }

              I have added all the configuration details in my initial post,

              -Hari

              Comment


              • #8
                I am sorry, I didn't read your initial post carrefuly.
                I can see that you configure your DataSource using Hibernate
                Code:
                <prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop> 
                <prop key="hibernate.connection.username">user</prop> 
                <prop key="hibernate.connection.password">password</prop> 
                <prop key="hibernate.connection.url">jdbc&#58;oracle&#58;thin&#58;@localhost&#58;1521&#58;sid</prop>
                Could you use DBCP to configure the DataSource and inject it inside your sessionFactory?

                Comment


                • #9
                  ok , it works now!!

                  I have injected datasources properties like this

                  <property name="dataSource"><ref local="dataSource"/></property>

                  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
                  <property name="driverClassName"><value>oracle.jdbc.driver.O racleDriver</value></property>
                  <property name="url"><value>jdbc:oracle:thin:@localhost:1521 :dmc</value></property>
                  <property name="username"><value>medicom</value></property>
                  <property name="password"><value>medicom</value></property>
                  </bean>



                  now the data gets commited,

                  but what's wrong with the way I was doing things ...

                  Comment

                  Working...
                  X