Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Two insertions per one save hibernateTemplate

  1. #1

    Default Two insertions per one save hibernateTemplate

    hi i m facing a strange problem... when i persists an object in database using hibernateTemplate of spring with hibernate3 two records are being inserted into the database table...

    the log generated by the spring is as
    Code:
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping] - Looking up handler for [/miQuotation.htm]
    
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler map [org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping@56d149] in DispatcherServlet with name 'dispatcher'
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@1c634b9] in DispatcherServlet with name 'dispatcher'
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping] - Looking up handler for [/miQuotation.htm]
    
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - Looking up handler for [/miQuotation.htm]
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@1c634b9] in DispatcherServlet with name 'dispatcher'
    2007-06-20 17:29:55,000 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@1af7c57]
    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] - Looking up handler for [/miQuotation.htm]
    
    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1d86a73]
    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@1af7c57]
    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver] - Determined handler method 'processQuotation' based on value of request parameter 'action'
    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1d86a73]
    2007-06-20 17:29:55,109 DEBUG [org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver] - Determined handler method 'processQuotation' based on value of request parameter 'action'
    2007-06-20 17:29:55,109 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
    2007-06-20 17:29:55,109 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
    2007-06-20 17:29:55,109 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC Connection to [jdbc:mysql://localhost:3306/cosmos]
    Hibernate: select max(quotation_id) from quotation
    2007-06-20 17:29:55,218 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
    2007-06-20 17:29:55,234 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
    Hibernate: insert into quotation (agent_code, agent_name, sum_insd, seating, year, ac_code, customer_name, phone, business, age, licence_no, ncd_year, pab, duration, oman, rate1, grs_premium1, rate2, grs_premium2, rate3, grs_premium3, vehicle_type_id, vehicle_make_id, country_id, category_id, cover_type_id, company1_id, company2_id, company3_id, quotation_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    2007-06-20 17:29:55,234 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC Connection to [jdbc:mysql://localhost:3306/cosmos]
    Hibernate: insert into quotation (agent_code, agent_name, sum_insd, seating, year, ac_code, customer_name, phone, business, age, licence_no, ncd_year, pab, duration, oman, rate1, grs_premium1, rate2, grs_premium2, rate3, grs_premium3, vehicle_type_id, vehicle_make_id, country_id, category_id, cover_type_id, company1_id, company2_id, company3_id, quotation_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    2007-06-20 17:29:55,328 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
    2007-06-20 17:29:55,406 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
    2007-06-20 17:29:55,406 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
    2007-06-20 17:29:55,406 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
    2007-06-20 17:29:55,406 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'quotation-view'
    2007-06-20 17:29:55,437 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
    2007-06-20 17:29:55,500 DEBUG [org.springframework.web.servlet.view.XmlViewResolver] - Cached view [quotation-view]
    2007-06-20 17:29:55,500 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
    2007-06-20 17:29:55,500 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
    2007-06-20 17:29:55,500 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
    and my dao entries are
    Code:
    private HibernateTemplate hibernateTemplate;
    private PlatformTransactionManager hbmTxManager;
    private DefaultTransactionDefinition txDefination;
    private TransactionStatus status = null;
    these are being set in dispacher-sevlet.xml
    try {
               txDefination.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
     setStatus(hbmTxManager.getTransaction(txDefination));
                
       hibernateTemplate.save(quotation);
    hbmTxManager.commit(status);
     hibernateTemplate.clear();    
      }
        catch(Exception exp){}
    plz reply ...
    thanks in advance

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    Can you post your configuration file(s), if possible ?

  3. #3
    Join Date
    Jan 2007
    Location
    Kuala Lumpur, Malaysia
    Posts
    138

    Default

    any errors being thrown because of primary key violation?

    looks like your log4j setting causing the log message to be displayed twice
    perhaps you could paste your log4j setting?

    probably should put additivity=false some in there

  4. #4

    Default

    Code:
     <bean id="miQuotationCntlr" class="com.aedsys.motorins.controller.QuotationCntlr">
            <property name="dao" ref="quotationDAO" />
            <property name="methodNameResolver" ref="methodNameResolver" />
        </bean>
         <bean id="quotationDAO" class="com.aedsys.motorins.dao.QuotationDAOImpl">
            <property name="hibernateTemplate" ref="hibernateTemplate" />
            <property name="txDefination" ref="txDefination" />
            <property name="hbmTxManager" ref="hbmTxManager" />
        </bean>   
     <bean id="hbmTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
            <property name="sessionFactory"><ref bean="sessionFactory"/></property>   
        </bean>
        
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory"><ref bean="sessionFactory"/></property>
        </bean>
    
        <bean id="txDefination" class="org.springframework.transaction.support.DefaultTransactionDefinition"/>
     <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.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource"/></property>  
            <property name="mappingResources">  
                <list>  
                    <value>com/aedsys/hbm/agent.hbm.xml</value> 
                    <value>com/aedsys/hbm/agentType.hbm.xml</value> 
    .....
    </list>
    
     <property name="hibernateProperties"> 
                <props> 
                    <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>  
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.dbcp.ps.whenExhaustedAction">1</prop>
                    <prop key="hibernate.dbcp.ps.maxIdle">100</prop>
                    <prop key="hibernate.dbcp.whenExhaustedAction">1</prop>
                    <prop key="hibernate.dbcp.ps.maxWait">120000</prop>
                    <prop key="hibernate.dbcp.maxIdle">10</prop>
                    <prop key="hibernate.dbcp.maxWait">120000</prop>
                    <prop key="hibernate.dbcp.maxActive">100</prop>
                    <prop key="hibernate.dbcp.ps.maxActive">100</prop>
                </props>   
            </property> 
     <property name="hibernateProperties"> 
                <props> 
                    <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>  
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.dbcp.ps.whenExhaustedAction">1</prop>
                    <prop key="hibernate.dbcp.ps.maxIdle">100</prop>
                    <prop key="hibernate.dbcp.whenExhaustedAction">1</prop>
                    <prop key="hibernate.dbcp.ps.maxWait">120000</prop>
                    <prop key="hibernate.dbcp.maxIdle">10</prop>
                    <prop key="hibernate.dbcp.maxWait">120000</prop>
                    <prop key="hibernate.dbcp.maxActive">100</prop>
                    <prop key="hibernate.dbcp.ps.maxActive">100</prop>
                </props>   
            </property>

  5. #5
    Join Date
    Jan 2007
    Location
    Kuala Lumpur, Malaysia
    Posts
    138

    Default

    looks good..
    what about your log4j.xml
    or log4j.properties file?

  6. #6

    Default

    hmmm sir i cant understand what u want to know .... i think i have mentioned that there are two insertions in database on a single save using hibernatetemplate.... so there is no use of log4j enteries or etc....


    and this is unpridictable behavoiure.. means it insert t

  7. #7

    Default

    hmmm sir i cant understand what u want to know .... i think i have mentioned that there are two insertions in database on a single save using hibernatetemplate.... so there is no use of log4j enteries or etc....


    and this is unpridictable behavoiure.. means it insert two records and some time it inserts only one record....

    ....
    ..
    .

  8. #8
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    If you look carefully on your log you would see that many entries there are duplicated, so it is quite feasible assumption that you really do not have to DB inserts, but 2 entries in the log file for the same single insert.

    Duplication of log entries is usually caused by misconfiguration of log4j, this is why its configuration was requsted.

    Have yoo take a look into DB? Are records really duplicated, or, if primary or unique constraints exist, have you received appropriate exceptions?

    Regards,
    Oleksandr

  9. #9

    Default

    yes ofcouse there are two recodes in the table. i have a primary key that is autoincremented but these two insertions have different keys......

    i also debug it but when the hibernatetemplate.commit() is executed then the two insertions are being made.....

    so what should i do

    n thanks for reply

  10. #10
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by CuriousHARD View Post
    yes ofcouse there are two recodes in the table. i have a primary key that is autoincremented but these two insertions have different keys......

    i also debug it but when the hibernatetemplate.commit() is executed then the two insertions are being made.....

    so what should i do

    n thanks for reply
    It looks as if problem is not telated to Hibernate, but to your handlers. On the more close look on your log-file, it comes to mind that request handler is called twice via to different mappings. I do not know how this result is reached - there are not enough data, but take a look on the following quotation from your log file (especially, on red-marked parts):

    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.mvc.multiaction.Pa rameterMethodNameResolver] - Determined handler method 'processQuotation' based on value of request parameter 'action'
    2007-06-20 17:29:55,093 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter@1d86a73]
    2007-06-20 17:29:55,109 DEBUG [org.springframework.web.servlet.mvc.multiaction.Pa rameterMethodNameResolver] - Determined handler method 'processQuotation' based on value of request parameter 'action'
    2007-06-20 17:29:55,109 DEBUG [org.springframework.orm.hibernate3.SessionFactoryU tils] - Opening Hibernate Session
    2007-06-20 17:29:55,109 DEBUG [org.springframework.orm.hibernate3.SessionFactoryU tils] - Opening Hibernate Session
    One more remark - if you use autoincrement primary key it is usually a good idea to have an unique constraint on the "business" key (if latter exists).

    Regards,

    Oleksandr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •