在action里面调用了两次Service的方法,导致了 two open Session 这是为什么呢 
一下是代码:
1.action里面的方法: public String addPrSubtype() throws Exception{
DgProductType instance=this.iDgProductTypeService.findById(this.dgProductType.getPrTyId());
...
this.iDgProductTypeService.attachDirty(instance);
return "success";
}2.service里面的方法public class DgProductTypeService implements IDgProductTypeService {
...
public void attachDirty(DgProductType instance) {
// TODO Auto-generated method stub
this.iDgProductTypeDAO.attachDirty(instance);
}
         
public DgProductType findById(Integer id) {
// TODO Auto-generated method stub
DgProductType dgProductType =this.iDgProductTypeDAO.findById(id);
return dgProductType;
}
}
3.事务管理器
<!-- 事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="transactionInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor"
    >
    <property name="transactionManager">
     <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributes"> 
     <props>
     <prop key="find*">PROPAGATION_REQUIRED,-Exception</prop>
     <prop key="*">PROPAGATION_REQUIRED</prop>
      <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
     </props>
    </property>
</bean>

     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">     
         <!--  指定对满足哪些bean name的bean自动生成业务代理 -->     
         <property name="beanNames">     
             <!--  下面是所有需要自动创建事务代理的bean-->     
             <list>     
               <value>productTypeService</value>
                 <value>dgUserService</value>
                 <value>dgUserTypeService</value>  
                   
            </list>     
             <!--  此处可增加其他需要自动创建事务代理的bean-->     
         </property>     
         <!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器-->     
         <property name="interceptorNames">     
             <list>     
                 <!-- 此处可增加其他新的Interceptor -->     
                 <value>transactionInterceptor</value>       
             </list>     
         </property>     
     </bean>4.web.xml   <!--*********** Filter************* -->
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   <init-param>  
   <param-name>config </param-name>  
   <param-value>struts-default.xml,struts-plugin.xml,struts-user.xml,struts-product.xml</param-value>  
</init-param> 
  </filter>
  <filter>  
        <filter-name>OpenSessionInViewFilter</filter-name>  
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
        <init-param>   
            <param-name>singleSession</param-name>   
            <param-value>false</param-value>   
        </init-param>  
    </filter>
    <!-- zh-cn encoding -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
           com.SetCharacterEncodingFilter
        </filter-class>
        <init-param>
         <param-name>encoding</param-name>
         <param-value>UTF-8</param-value>
        </init-param>
    </filter> 
        <!--<filter>
     <filter-name>loginFilter</filter-name>
     <filter-class>com.LoginFilter</filter-class>
    </filter>   
    
    -->
      <filter-mapping>  
        <filter-name>OpenSessionInViewFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
  </filter-mapping>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

解决方案 »

  1.   

    感觉像 你的事务是控制在service层
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">     
             <!--  指定对满足哪些bean name的bean自动生成业务代理 -->     
             <property name="beanNames">     
                 <!--  下面是所有需要自动创建事务代理的bean-->     
                 <list>     
                      <value>productTypeService</value>
                     <value>dgUserService</value>
                     <value>dgUserTypeService</value>  

                       
                </list>     
                 <!--  此处可增加其他需要自动创建事务代理的bean-->     
             </property>     
             <!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器-->     
             <property name="interceptorNames">     
                 <list>     
                     <!-- 此处可增加其他新的Interceptor -->     
                     <value>transactionInterceptor</value>       
                 </list>     
             </property>     
         </bean>