TWO open Sessions

zl283936851 2010-05-12 12:16:07
在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>

...全文
71 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl283936851 2010-07-28
  • 打赏
  • 举报
回复
对不起啊~最近回了下 学校~·没什么时间上 现在补上~见谅
Octer 2010-05-12
  • 打赏
  • 举报
回复
感觉像 你的事务是控制在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>

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧