Announcement Announcement Module
Collapse
No announcement yet.
No data inserted when I use transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No data inserted when I use transaction

    Hi everyone:

    I want to use HibernateTransaction in Spring. But it couldn't insert data into database.
    my config file is:
    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?useUnicode=true&amp;characterEncoding=gb2312</value>
            </property>
            <property name="username">
                <value>root</value>
            </property>
    		<property name="password">
                <value>root</value>
            </property>
    		
    		<property name="defaultAutoCommit"> 
                <value>false</value> 
            </property>
    		
        </bean>
    	<!--
    <bean id="MyJNDIDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <value>jdbc/mysql</value>
      </property>
    </bean>
    -->
    <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>
          </props>
       </property>
       <property name="dataSource"><ref bean="MyDataSource"/></property>
    </bean>
    
    
    
    
    <bean id="fproxy" class="lyo.hotmail.site.util.ForumProxy">
       <property name="fdao">
          <ref bean="formDao"/>
       </property>
    </bean>
    <bean id="viewSpringForumController" class="lyo.hotmail.site.action.ViewForumcontroller">
    <property name="forumProxy">
         <ref bean="fproxy"/>
    </property>
       </bean>
       <bean id="rewriteURLController" class="lyo.hotmail.site.action.DispatchForumController">
    <property name="fdao">
          <ref bean="formDao"/>
       </property>
       </bean>
    <bean id="postArticleController" class="lyo.hotmail.site.action.PostArticleController">
    <property name="fdao">
          <ref bean="formDao"/>
       </property>
    </bean>
    <bean id="viewDetailController" class="lyo.hotmail.site.action.viewDetailController">
    <property name="forumProxy">
         <ref bean="fproxy"/>
    </property>
    </bean>
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/oldForum.spring">viewSpringForumController</prop>
    				<prop key="/viewForum.spring">viewSpringForumController</prop>
    				<prop key="/detail.spring">viewDetailController</prop>
    				<prop key="/newPost.spring">rewriteURLController</prop>
    				<prop key="/post.spring">postArticleController</prop>
                </props>
            </property>
       
        </bean>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value>/WEB-INF/forum/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    <bean id="formDao" class="lyo.hotmail.site.service.ForumDAOImpl">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
       <property name="udao">
          <ref bean="userDao"/>
       </property>
    </bean> 
     <bean id="userDao" class="lyo.hotmail.site.service.UserDAOImpl">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    </bean> 
    <bean id="msgDao" class="lyo.hotmail.site.service.MessageDAOImpl">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
        <property name="messageDao">
        <ref bean="msgDao"/>
      </property>
    </bean> 
    
    <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
      <property name="sessionFactory">
        <ref bean="MySessionFactory"/>
      </property>
    </bean>
    <bean id="myTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
      <property name="transactionManager">
        <ref bean="myTransactionManager"/>
      </property>
      <property name="transactionAttributeSource">
        <value>
          lyo.hotmail.site.service.MessageDAOImpl.sendMsgTransaction=PROPAGATION_MANDATORY
       </value>
      </property>
    </bean>
    
    <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="proxyInterfaces">
        <value>lyo.hotmail.site.dao.MessageDAO</value>
      </property>
      <property name="interceptorNames">
        <value>myTransactionInterceptor,msgDao</value>
      </property>
    </bean>
    If I set :
    Code:
    <property name="defaultAutoCommit"> 
                <value>true</value> 
            </property>
    Spring will report error:
    Code:
    Can't call commit when autoCommit is true.
    So I set autoCommit to false(I don't know why ;-( ).

    My class is:

    Code:
    public class MessageDAOImpl extends HibernateDaoSupport implements MessageDAO &#123;
    ......................................................................................
    public boolean sendMsg&#40;final Message msg,final Long userID&#41; &#123;
    		HibernateTemplate hibernateTemplate = new HibernateTemplate&#40;this.getSessionFactory&#40;&#41;&#41;;
    	    User user= &#40;User&#41; hibernateTemplate.execute&#40;
    	        new HibernateCallback&#40;&#41; &#123;
    	          public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
    	          	User user=&#40;User&#41;session.get&#40;User.class,userID&#41;;
    	    		msg.setUser&#40;user&#41;;
    	    		Set msgSet=user.getMessageSet&#40;&#41;;
    	    		if&#40;null!=msgSet&#41;&#123;
    	    			msgSet.add&#40;msg&#41;;
    	    		&#125;else&#123;
    	    			msgSet=new HashSet&#40;&#41;;
    	    			msgSet.add&#40;msg&#41;;
    	    		&#125;
    	    		user.setMessageSet&#40;msgSet&#41;;
    	    		session.update&#40;user&#41;;
    	    		return null;
    	          &#125;
    	        &#125;&#41;;
    	   
    		
    		
    		return true;
    	&#125;
    	
    	/***
    	 * send message transaction                                        	 */
    	public boolean sendMsgTransaction&#40;final Message msg,final Long userID&#41;&#123;
    		this.getMessageDao&#40;&#41;.sendMsg&#40;msg,userID&#41;;
    		return true;
    	&#125;
    ...........................................................................
    MessageDao class:
    Code:
    public interface MessageDAO &#123;
    	..................................................................................	public boolean sendMsg&#40;Message msg,Long userID&#41;;
    	public boolean sendMsg&#40;Message msg,String userName&#41;;
    	public boolean sendMsgTransaction&#40;final Message msg,final Long userID&#41;;
    
    &#125;
    When I run it in Tomcat ,console output :
    Code:
    .......................................................................
    00&#58;08&#58;36,313  INFO SettingsFactory&#58;103 - Use scrollable result sets&#58; true
    00&#58;08&#58;36,323  INFO SettingsFactory&#58;106 - Use JDBC3 getGeneratedKeys&#40;&#41;&#58; true
    00&#58;08&#58;36,323  INFO SettingsFactory&#58;109 - Optimize cache for minimal puts&#58; false
    00&#58;08&#58;36,323  INFO SettingsFactory&#58;115 - echoing all SQL to stdout
    00&#58;08&#58;36,323  INFO SettingsFactory&#58;118 - Query language substitutions&#58; &#123;no='N',
    true=1, yes='Y', false=0&#125;
    00&#58;08&#58;36,323  INFO SettingsFactory&#58;129 - cache provider&#58; net.sf.hibernate.cache.
    EhCacheProvider
    00&#58;08&#58;36,323  INFO Configuration&#58;1116 - instantiating and configuring caches
    00&#58;08&#58;36,323  INFO SessionFactoryImpl&#58;118 - building session factory
    00&#58;08&#58;36,403  INFO SessionFactoryObjectFactory&#58;82 - Not binding factory to JNDI,
     no JNDI name configured
    00&#58;08&#58;46,587  INFO PropertyMessageResources&#58;127 - Initializing, config='org.apac
    he.struts.actions.LocalStrings', returnNull=true
    00&#58;08&#58;46,607  INFO SQLErrorCodesFactory&#58;197 - Looking up default SQLErrorCodes f
    or DataSource
    00&#58;08&#58;46,607  INFO SQLErrorCodesFactory&#58;202 - Database product name found in cac
    he for DataSource &#91;org.apache.commons.dbcp.BasicDataSource@130c132&#93;. Name is 'My
    SQL'.
    Hibernate&#58; select user0_.USER_ID as USER_ID0_, user0_.USER_GRADE as USER_GRADE0_
    , user0_.USER_NAME as USER_NAME0_, user0_.USER_PASSWORD as USER_PAS4_0_ from use
    r user0_ where user0_.USER_ID=?
    Hibernate&#58; select messageset0_.USER_ID as USER_ID__, messageset0_.MESSAGE_ID as
    MESSAGE_ID__, messageset0_.MESSAGE_ID as MESSAGE_ID0_, messageset0_.MESSAGE_CONT
    ENT as MESSAGE_2_0_, messageset0_.MESSAGE_TITLE as MESSAGE_3_0_, messageset0_.SE
    ND_TIME as SEND_TIME0_, messageset0_.SENDER as SENDER0_, messageset0_.USER_ID as
     USER_ID0_ from message messageset0_ where messageset0_.USER_ID=?
    Hibernate&#58; insert into message &#40;MESSAGE_CONTENT, MESSAGE_TITLE, SEND_TIME, SENDE
    R, USER_ID, MESSAGE_ID&#41; values &#40;?, ?, ?, ?, ?, ?&#41;
    Not any exception happen,But no data inserted ! Why spring couldn't insert data into database? Thks

  • #2
    <bean id="myProductService" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>lyo.hotmail.site.dao.MessageDAO</value>
    </property>
    <property name="interceptorNames">
    <value>myTransactionInterceptor,msgDao</value>
    </property>
    </bean>
    This declaration apply transaction demarcation to bean msgDao . However, I do not see where ou are using bean myProductService!!
    <bean id="msgDao" class="lyo.hotmail.site.service.MessageDAOImpl">
    <property name="sessionFactory">
    <ref bean="MySessionFactory"/>
    </property>
    <property name="messageDao">
    <ref bean="msgDao"/>
    </property>
    </bean>
    Are you sure this declaration is correct?
    HTH

    Comment


    • #3


      Thank you for your reply.
      I change the config file to the following,but it also can't insert data to database.

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
      <beans>
      <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?useUnicode=true&amp;characterEncoding=gb2312</value>
              </property>
              <property name="username">
                  <value>root</value>
              </property>
      		<property name="password">
                  <value>root</value>
              </property>
      		
      		<property name="defaultAutoCommit"> 
                  <value>false</value> 
              </property>
      		
          </bean>
      	<!--
      <bean id="MyJNDIDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
          <value>jdbc/mysql</value>
        </property>
      </bean>
      -->
      <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>
            </props>
         </property>
         <property name="dataSource"><ref bean="MyDataSource"/></property>
      </bean>
      
      
      
      
      <bean id="fproxy" class="lyo.hotmail.site.util.ForumProxy">
         <property name="fdao">
            <ref bean="formDao"/>
         </property>
      </bean>
      <bean id="viewSpringForumController" class="lyo.hotmail.site.action.ViewForumcontroller">
      <property name="forumProxy">
           <ref bean="fproxy"/>
      </property>
         </bean>
         <bean id="rewriteURLController" class="lyo.hotmail.site.action.DispatchForumController">
      <property name="fdao">
            <ref bean="formDao"/>
         </property>
         </bean>
      <bean id="postArticleController" class="lyo.hotmail.site.action.PostArticleController">
      <property name="fdao">
            <ref bean="formDao"/>
         </property>
      </bean>
      <bean id="viewDetailController" class="lyo.hotmail.site.action.viewDetailController">
      <property name="forumProxy">
           <ref bean="fproxy"/>
      </property>
      </bean>
          <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              <property name="mappings">
                  <props>
                      <prop key="/oldForum.spring">viewSpringForumController</prop>
      				<prop key="/viewForum.spring">viewSpringForumController</prop>
      				<prop key="/detail.spring">viewDetailController</prop>
      				<prop key="/newPost.spring">rewriteURLController</prop>
      				<prop key="/post.spring">postArticleController</prop>
                  </props>
              </property>
         
          </bean>
      
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
              <property name="prefix"><value>/WEB-INF/forum/</value></property>
              <property name="suffix"><value>.jsp</value></property>
          </bean>
      <bean id="formDao" class="lyo.hotmail.site.service.ForumDAOImpl">
         <property name="sessionFactory">
            <ref bean="MySessionFactory"/>
         </property>
         <property name="udao">
            <ref bean="userDao"/>
         </property>
      </bean> 
       <bean id="userDao" class="lyo.hotmail.site.service.UserDAOImpl">
         <property name="sessionFactory">
            <ref bean="MySessionFactory"/>
         </property>
      </bean> 
      <bean id="msgDao" class="lyo.hotmail.site.service.MessageDAOImpl">
         <property name="sessionFactory">
            <ref bean="MySessionFactory"/>
         </property>
         
      </bean> 
      
      <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
        </property>
      </bean>
      <bean id="proxyService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
        <property name="transactionManager">
          <ref bean="myTransactionManager"/>
        </property>
        <property name="target"> 
                      <ref local="msgDao"/> 
              </property> 
        <property name="transactionAttributes">
         
         <props> 
                       
                      <prop key="sendMsg">PROPAGATION_REQUIRED</prop> 
                      <prop key="update*">PROPAGATION_REQUIRED</prop> 
                      <prop key="delete*">PROPAGATION_REQUIRED</prop> 
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> 
                  </props> 
      
        </property>
      </bean>
      
      
      
      
      
      </beans>
      Why?

      Comment


      • #4
        The controllers reference base dao classes. These daos do not use transactions, so no data will be persisted into the database since autocommit is set to false.
        You need to reference a bean that is wrapped within / uses transactions. For example:
        Code:
        <bean id="viewDetailController" class="lyo.hotmail.site.action.viewDetailController"> 
        <property name="proxyService"> 
             <ref bean="proxyService"/> 
        </property> 
        </bean>
        For more informations on using declarative transaction demarcation with Spring, please take a look at Chapter 7. Transaction management.
        HTH.

        Comment


        • #5
          &amp;#65306;&amp;#65288;

          Originally posted by irbouho
          The controllers reference base dao classes. These daos do not use transactions, so no data will be persisted into the database since autocommit is set to false.
          You need to reference a bean that is wrapped within / uses transactions. For example:
          Code:
          <bean id="viewDetailController" class="lyo.hotmail.site.action.viewDetailController"> 
          <property name="proxyService"> 
               <ref bean="proxyService"/> 
          </property> 
          </bean>
          For more informations on using declarative transaction demarcation with Spring, please take a look at Chapter 7. Transaction management.
          HTH.
          But I don't use this dao in viewDetailController servlet. I use it in my struts Action. This Action is :
          Code:
          public final class DoMsgAction extends DispatchActionSupport &#123;
          
          	/* send message                                                         	 * @see org.apache.struts.action.Action#execute&#40;org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse&#41;
          	 */
          	public ActionForward send&#40;ActionMapping arg0, ActionForm arg1,
          			HttpServletRequest arg2, HttpServletResponse arg3&#41; throws Exception &#123;
          		Message msg=new Message&#40;&#41;;
          		BeanUtils.copyProperties&#40;msg,arg1&#41;;
          		msg.setSendTime&#40;new java.util.Date&#40;&#41;&#41;;
          		HttpSession s=arg2.getSession&#40;&#41;;
          		User user=&#40;User&#41;s.getAttribute&#40;"user"&#41;;
          		String username=user.getUserName&#40;&#41;;
          		msg.setSender&#40;username&#41;;
          		String senderID=arg2.getParameter&#40;"senderID"&#41;;
          		MessageDAO msgDao=&#40;MessageDAO&#41;this.getWebApplicationContext&#40;&#41;.getBean&#40;"msgDao"&#41;;
          		msgDao.sendMsg&#40;msg,new Long&#40;senderID&#41;&#41;;
          		return arg0.findForward&#40;"AfterSend"&#41;;
          	&#125;
          .............................................................................
          &#125;
          The DispatchActionSupport is in the
          Code:
          org.springframework.web.struts.DispatchActionSupport
          How should I  do?

          Comment


          • #6
            need have to use Transaction?

            I want to know whether I need have to use Spring Transaction if I "update" or "save" an Object.

            I change my code to this. It also don't work
            Code:
            public class ForumDAOImpl extends HibernateDaoSupport implements ForumDAO &#123;
            ...........................................................................................
            public boolean insertArticle&#40;final Article a,final User u&#41; &#123;
            		
            		boolean  obj_id = false;
            			User user=null;
            			Object o=null;	
            HibernateTemplate hibernateTemplate = new HibernateTemplate&#40;this.getSessionFactory&#40;&#41;&#41;;
            		   Boolean isup= &#40;Boolean&#41; hibernateTemplate.execute&#40;
            		        new HibernateCallback&#40;&#41; &#123;
            		          public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
            		          	Object o=null;
            		          	User user=null;
            		          	o = session.createQuery&#40;
            							"from User u where u.userName='" + u.getUserName&#40;&#41; + "'"&#41;.uniqueResult&#40;&#41;;
            		          	if&#40;null!=o&#41;&#123;
            						log.info&#40;"Get o&#58; "+o&#41;;
            						user=&#40;User&#41;o;
            					&#125;
            					a.setUser&#40;user&#41;;
            					Set articleSet=user.getArticleSet&#40;&#41;;
            					
            					if&#40;null!=articleSet&#41;&#123;
            						articleSet.add&#40;a&#41;;
            					&#125;else&#123;
            						articleSet=new HashSet&#40;&#41;;
            						articleSet.add&#40;a&#41;;
            					&#125;
            					user.setArticleSet&#40;articleSet&#41;;
            						
            						session.update&#40;user&#41;;
            						
            		            return new Boolean&#40;true&#41;;
            		          &#125;
            		        &#125;&#41;;
            			
            		
            		return true;
            
            	&#125;
            .....................  other methods  ...................................
            &#125;
            Config file change to this:
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
            <beans>
            <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?useUnicode=true&amp;characterEncoding=gb2312</value>
                    </property>
                    <property name="username">
                        <value>root</value>
                    </property>
            		<property name="password">
                        <value>root</value>
                    </property>
            		
            		<property name="defaultAutoCommit"> 
                        <value>false</value> 
                    </property>
            		
                </bean>
            	<!--
            <bean id="MyJNDIDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
              <property name="jndiName">
                <value>jdbc/mysql</value>
              </property>
            </bean>
            -->
            <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>
                  </props>
               </property>
               <property name="dataSource"><ref bean="MyDataSource"/></property>
            </bean>
            
            
            
            
            <bean id="fproxy" class="lyo.hotmail.site.util.ForumProxy">
               <property name="fdao">
                  <ref bean="formDao"/>
               </property>
            </bean>
            <bean id="viewSpringForumController" class="lyo.hotmail.site.action.ViewForumcontroller">
            <property name="forumProxy">
                 <ref bean="fproxy"/>
            </property>
               </bean>
               <bean id="rewriteURLController" class="lyo.hotmail.site.action.DispatchForumController">
            <property name="fdao">
                  <ref bean="formDao"/>
               </property>
               </bean>
            <bean id="postArticleController" class="lyo.hotmail.site.action.PostArticleController">
            <property name="fdao">
                  <ref bean="formDao"/>
               </property>
            </bean>
            <bean id="viewDetailController" class="lyo.hotmail.site.action.viewDetailController">
            <property name="forumProxy">
                 <ref bean="fproxy"/>
            </property>
            </bean>
                <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                    <property name="mappings">
                        <props>
                            <prop key="/oldForum.spring">viewSpringForumController</prop>
            				<prop key="/viewForum.spring">viewSpringForumController</prop>
            				<prop key="/detail.spring">viewDetailController</prop>
            				<prop key="/newPost.spring">rewriteURLController</prop>
            				<prop key="/post.spring">postArticleController</prop>
                        </props>
                    </property>
               
                </bean>
            
            <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
                    <property name="prefix"><value>/WEB-INF/forum/</value></property>
                    <property name="suffix"><value>.jsp</value></property>
                </bean>
            <bean id="formDao" class="lyo.hotmail.site.service.ForumDAOImpl">
               <property name="sessionFactory">
                  <ref bean="MySessionFactory"/>
               </property>
               <property name="udao">
                  <ref bean="userDao"/>
               </property>
            </bean> 
             <bean id="userDao" class="lyo.hotmail.site.service.UserDAOImpl">
               <property name="sessionFactory">
                  <ref bean="MySessionFactory"/>
               </property>
            </bean> 
            <bean id="msgDao" class="lyo.hotmail.site.service.MessageDAOImpl">
               <property name="sessionFactory">
                  <ref bean="MySessionFactory"/>
               </property>
               
            </bean> 
            
            <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
              <property name="sessionFactory">
                <ref bean="MySessionFactory"/>
              </property>
            </bean>
            <bean id="proxyService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
              <property name="transactionManager">
                <ref bean="myTransactionManager"/>
              </property>
              <property name="target"> 
                            <ref local="msgDao"/> 
                    </property> 
            		<property name="target"> 
                            <ref local="formDao"/> 
                    </property> 
              <property name="transactionAttributes">
               
               <props> 
                             
                            <prop key="send*">PROPAGATION_REQUIRED</prop> 
            	<prop key="insert*">PROPAGATION_REQUIRED</prop>
                        </props> 
            
              </property>
            </bean>
            </beans>
            Tomcat output:
            Code:
            11&#58;07&#58;46,442  INFO SQLErrorCodesFactory&#58;197 - Looking up default SQLErrorCodes f
            or DataSource
            11&#58;07&#58;46,893  INFO SQLErrorCodesFactory&#58;202 - Database product name found in cac
            he for DataSource &#91;org.apache.commons.dbcp.BasicDataSource@129645a&#93;. Name is 'My
            SQL'.
            Hibernate&#58; select user0_.USER_ID as USER_ID, user0_.USER_GRADE as USER_GRADE, us
            er0_.USER_NAME as USER_NAME, user0_.USER_PASSWORD as USER_PAS4_ from user user0_
             where &#40;user0_.USER_NAME='li' &#41;
            11&#58;07&#58;47,474  INFO ForumDAOImpl&#58;175 - Get o&#58; lyo.hotmail.site.bean.User@104a681&#91;
            id=622594&#93;
            Hibernate&#58; select articleset0_.USER_ID as USER_ID__, articleset0_.ARTICLE_ID as
            ARTICLE_ID__, articleset0_.ARTICLE_ID as ARTICLE_ID0_, articleset0_.ARTICLE_ICON
             as ARTICLE_2_0_, articleset0_.ARTICLE_TITLE as ARTICLE_3_0_, articleset0_.ARTIC
            LE_TIME as ARTICLE_4_0_, articleset0_.ARTICLE_CONTENT as ARTICLE_5_0_, articlese
            t0_.ARTICLE_VIEWTIMES as ARTICLE_6_0_, articleset0_.USER_ID as USER_ID0_, articl
            eset0_.ART_ARTICLE_ID as ART_ARTI8_0_, articleset0_.FORUM_ID as FORUM_ID0_ from
            article articleset0_ where articleset0_.USER_ID=?
            Hibernate&#58; select forum0_.FORUM_ID as FORUM_ID0_, forum0_.FORUM_NAME as FORUM_NA
            ME0_, forum0_.FORUM_DESC as FORUM_DESC0_, forum0_.CREATE_DATE as CREATE_D4_0_ fr
            om forum forum0_ where forum0_.FORUM_ID=?
            Hibernate&#58; insert into article &#40;ARTICLE_TITLE, ARTICLE_TIME, ARTICLE_CONTENT, AR
            TICLE_VIEWTIMES, USER_ID, ART_ARTICLE_ID, FORUM_ID, ARTICLE_ID&#41; values &#40;?, ?, ?,
             ?, ?, ?, ?, ?&#41;
            11&#58;07&#58;48,705  INFO PostArticleController&#58;72 - Saving article &#58; true
            My action:
            Code:
            public class PostArticleController implements Controller &#123;
            	private Log log=LogFactory.getLog&#40;PostArticleController.class&#41;;
            
            	/**
            	 * @return Returns the fdao.
            	 */
            	public ForumDAO getFdao&#40;&#41; &#123;
            		return fdao;
            	&#125;
            	/**
            	 * @param fdao The fdao to set.
            	 */
            	public void setFdao&#40;ForumDAO fdao&#41; &#123;
            		this.fdao = fdao;
            	&#125;
            	private ForumDAO fdao;
            	public ModelAndView handleRequest&#40;HttpServletRequest arg0,
            			HttpServletResponse arg1&#41; throws Exception &#123;
            		String title=arg0.getParameter&#40;"title"&#41;;
            		String content=arg0.getParameter&#40;"EditorDefault"&#41;;
            		String parentID=arg0.getParameter&#40;"parentID"&#41;;
            		log.info&#40;"get title&#58; "+title&#41;;
            		log.info&#40;"get content&#58;"+content&#41;;
            		Article article=new Article&#40;&#41;;
            		String fid=arg0.getParameter&#40;"fid"&#41;;
            		Forum f=&#40;Forum&#41;this.getFdao&#40;&#41;.loadObjectByID&#40;Forum.class,Long.valueOf&#40;fid&#41;&#41;;
            		article.setForum&#40;f&#41;;
            		article.setArticleTitle&#40;title&#41;;
            		if&#40;null!=parentID&&!"".equals&#40;parentID.trim&#40;&#41;&#41;&&!"null".equals&#40;parentID.trim&#40;&#41;&#41;&#41;&#123;
            			log.info&#40;"get parentID >>> "+parentID&#41;;
            			Article p_article=&#40;Article&#41;this.getFdao&#40;&#41;.loadObjectByID&#40;Article.class,Long.valueOf&#40;parentID&#41;&#41;;
            			article.setArtArticle&#40;p_article&#41;;
            		&#125;
            		article.setArticleContent&#40;content&#41;;
            		article.setArticleTime&#40;new Date&#40;&#41;&#41;;
            		HttpSession session=arg0.getSession&#40;&#41;; 
            		User user=&#40;User&#41;session.getAttribute&#40;"user"&#41;;
            		boolean article_id=fdao.insertArticle&#40;article,user&#41;;
            		log.info&#40;"Saving article &#58; "+article_id&#41;;
            		return new ModelAndView&#40;"wait","fid",fid&#41;;
            	&#125;
            
            &#125;
            But it couldn't insert data to database.Is there any mistake in my config file? [/code]

            Comment


            • #7


              What's meaning of the following statement?
              Code:
              <property name="defaultAutoCommit"> 
                          <value>false</value> 
                      </property>
              When should I set the value to true? THks

              Comment

              Working...
              X