HttpSessionListener 调用 spring 中服务类的实现方法出现session错误。

remote_roamer 2008-09-13 04:26:00
我写了一个 HttpSessionListener 。其中实现的sessionDestroyed 方法是去调用一个 service 中业务逻辑(去删除数据库中的一条记录)
代码如下
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
AlcorTUser alcorTUser = (AlcorTUser) session.getAttribute("_AlcorTUserVO");
logger.debug("Session即將失效,更新在线用户表");
WebApplicationContext springCtx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
UserService userService = (UserService)(springCtx.getBean("UserService"));
String [] names = springCtx.getBeanDefinitionNames();
for (int i = 0 ; i < names.length ;i++ )
{
logger.debug(names[i]);
}

userService.deleteOnlineUser(alcorTUser);
}

UserService 已经通过 spring注入。
现在遇到的情况是:
1. UserService 是确保可以在spring中运行。因为我通过其他的spring 或者 servlet去调用 这个userService.deleteOnlineUser(alcorTUser)这个方法完全成功。
2.在这个listener中去调用的话,控制台上显示
严重: Session event listener threw exception
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is java.lang.IllegalArgumentException: interface org.hibernate.jdbc.ConnectionWrapper is not visible from class loader
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:599)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at com.alcor.web.services.UserService$$EnhancerByCGLIB$$6656bd3d.deleteOnlineUser( <generated>)
at com.alcor.web.flex.session.SessionListenerService.sessionDestroyed(SessionListenerService.java:39)
at org.apache.catalina.session.StandardSession.expire(StandardSession.java:702)
这个错误。是说 session事务无法建立。
我为此专门升级了hibernate3.3.1.GA 和 spring2.5.5 。仍然无法解决。
请教各位高手,是否做过类似我的这种 需求实现。这个错误应该如何解决?
...全文
324 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shihuan830619 2010-10-22
  • 打赏
  • 举报
回复
import org.springframework.context.ApplicationContext;

public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
AlcorTUser alcorTUser = (AlcorTUser) session.getAttribute("_AlcorTUserVO");
logger.debug("Session即將失效,更新在线用户表");
ApplicationContext springCtx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
UserService userService = (UserService)(springCtx.getBean("UserService"));
String [] names = springCtx.getBeanDefinitionNames();
for (int i = 0 ; i < names.length ;i++ ){
logger.debug(names[i]);
}

userService.deleteOnlineUser(alcorTUser);
}

按照上述方式试试看, 反正我这里是通过的,你的那个WebApplicationContext我没试过!
MLMW_LHC 2009-05-18
  • 打赏
  • 举报
回复
lz,你这个问题解决没啊?我现在也遇到了这个问题。望指教啊……
yeah920 2008-09-26
  • 打赏
  • 举报
回复
帮你顶一下.

67,513

社区成员

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

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