Announcement Announcement Module
Collapse
No announcement yet.
Can't insert data using HibernateTemplate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't insert data using HibernateTemplate?

    Hi everyone:

    I come across a odd problem. I want to save a record in databse using this code:
    Code:
    public String insertObject(Object o){
        	return (String)this.getHibernateTemplate().save(o);
        	
        }
    Spring report error:
    Code:
    org.springframework.jdbc.UncategorizedSQLException: (Hibernate operation): encountered SQLException [Can't call commit when autocommit=true]; nested exception is java.sql.SQLException: Can't call commit when autocommit=true
    	org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:94)
    	org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:281)
    	org.springframework.orm.hibernate.HibernateAccessor.convertJdbcAccessException(HibernateAccessor.java:249)
    	org.springframework.orm.hibernate.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:231)
    	org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:181)
    	org.springframework.orm.hibernate.HibernateTemplate.save(HibernateTemplate.java:298)
    	lyo.hotmail.site.service.ForumDAOImpl.insertObject(ForumDAOImpl.java:57)
    	lyo.hotmail.site.action.PostArticleController.handleRequest(PostArticleController.java:57)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:495)
    	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:321)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    I use MySQL4.01. InnoDB.
    Why this exception happen? Thks!

  • #2


    If I change code to this:
    Code:
     public String insertObject(Object o){
        	//don't work why? :(
        	//return (String)this.getHibernateTemplate().save(o);
        	Session s=SessionFactoryUtils.getSession(this.getSessionFactory(),false);
        	String obj_id=null;
        	try{
        		obj_id=(String)s.save(o);
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	return obj_id;
        }
    It report exception:
    Code:
    java.lang.IllegalStateException: No Hibernate session bound to thread, and configuration does not allow creation of new one here
    	org.springframework.orm.hibernate.SessionFactoryUtils.getSession(SessionFactoryUtils.java:320)
    	org.springframework.orm.hibernate.SessionFactoryUtils.getSession(SessionFactoryUtils.java:182)
    	lyo.hotmail.site.service.ForumDAOImpl.insertObject(ForumDAOImpl.java:61)
    	lyo.hotmail.site.action.PostArticleController.handleRequest(PostArticleController.java:59)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:495)
    	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:321)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    Why there isn't a session in current thread?

    Comment


    • #3
      Code:
      org.springframework.jdbc.UncategorizedSQLException: (Hibernate operation): encountered SQLException [Can't call commit when autocommit=true]; nested exception is java.sql.SQLException: Can't call commit when autocommit=true
      try to turn autoCommit off in your datasource configuration.

      HTH

      Comment


      • #4


        Originally posted by irbouho
        Code:
        org.springframework.jdbc.UncategorizedSQLException: (Hibernate operation): encountered SQLException [Can't call commit when autocommit=true]; nested exception is java.sql.SQLException: Can't call commit when autocommit=true
        try to turn autoCommit off in your datasource configuration.

        HTH
        Thks for your reply.But it seems don't work too .

        My config file change to this:
        Code:
        <bean id="MySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
           <property name="mappingResources">
              <list>
                <value>lyo/hotmail/site/bean/Article.hbm.xml</value>
                <value>lyo/hotmail/site/bean/Forum.hbm.xml</value>
                <value>lyo/hotmail/site/bean/Message.hbm.xml</value>
                <value>lyo/hotmail/site/bean/User.hbm.xml</value>
                <value>lyo/hotmail/site/bean/UserDetail.hbm.xml</value>
              </list>
           </property>
           <property name="hibernateProperties">
              <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.query.substitutions">true=1 false=0</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_outer_join">false</prop>
        		<prop key="hibernate.autocommit">false</prop>
              </props>
           </property>
           <property name="dataSource"><ref bean="MyDataSource"/></property>
        </bean>
        It also report the same exception. why?

        Comment


        • #5
          liren,

          Could you set autoCommit to false at your dataSource bean definition instead of HibernateLocalSessionDefinition?

          Comment


          • #6


            I try it . But it don't work:
            Code:
            <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                    <!-- results in a setDriverClassName&#40;String&#41; call -->
                    <property name="driverClassName">
                        <value>com.mysql.jdbc.Driver</value>
                    </property>
                    <property name="url">
                        <value>jdbc&#58;mysql&#58;//127.0.0.1&#58;3306/Hibernate</value>
                    </property>
                    <property name="username">
                        <value>root</value>
                    </property>
            		<property name="password">
                        <value></value>
                    </property>
            		<property name="autoCommit">
                        <value>false</value>
                    </property>
            		
                </bean>
            Spring report error:
            Code:
            07&#58;26&#58;53,981  INFO DefaultListableBeanFactory&#58;409 - Destroying singletons in fac
            tory &#123;org.springframework.beans.factory.support.DefaultListableBeanFactory defin
            ing beans &#91;MyDataSource,MySessionFactory,fproxy,viewSpringForumController,rewrit
            eURLController,postArticleController,urlMapping,viewResolver,formDao&#93;; Root of B
            eanFactory hierarchy&#125;
            07&#58;26&#58;53,981 ERROR DispatcherServlet&#58;208 - Context initialization failed
            org.springframework.beans.factory.BeanCreationException&#58; Error creating bean wit
            h name 'MyDataSource' defined in resource &#91;/WEB-INF/springmodal-servlet.xml&#93; of
            ServletContext&#58; Error setting property values; nested exception is org.springfra
            mework.beans.NotWritablePropertyException&#58; Invalid property 'autoCommit' of bean
             class &#91;org.apache.commons.dbcp.BasicDataSource&#93;&#58; Property 'autoCommit' is not w
            ritable
            org.springframework.beans.NotWritablePropertyException&#58; Invalid property 'autoCo
            mmit' of bean class &#91;org.apache.commons.dbcp.BasicDataSource&#93;&#58; Property 'autoCom
            mit' is not writable
                    at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrappe
            rImpl.java&#58;666&#41;
                    at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrappe
            rImpl.java&#58;588&#41;
                    at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrappe
            rImpl.java&#58;720&#41;
                    at org.springframework.beans.BeanWrapperImpl.setPropertyValues&#40;BeanWrapp
            .....................................................................................
            ...............................................................
            This bean don't has this option?

            Comment


            • #7
              I am sorry for the confusion
              Code:
              <property name="defaultAutoCommit">
                <value>false</value>
              </property>
              I can see from the log that you did not configure any transaction management for your DAOs, did you? basically, HibernateTransactionManager should set autoCommit to false.

              Comment


              • #8


                Thks. It would work if I change my code to this:

                Code:
                public Object insertObject&#40;Object o&#41;&#123;
                                
                    	return this.getHibernateTemplate&#40;&#41;.save&#40;o&#41;;		
                    &#125;
                It doesn't work!
                It means that should I have to use the more complex code in MySQL database?

                How to make this code "return this.getHibernateTemplate().save(o);" work property? I mean that if I want to use the later code how to do it?

                Comment

                Working...
                X