关于SSH中对于action的监听问题(关于系统计数)

09-04-27 liweitsky

最近想做一个系统计数功能,网上看了一下资料,用的是session监听,每当一个session创建或失效,count自动增减1,监听类如下

public class CounterAction extends DispatchAction implements HttpSessionListener {

private static int activeSessions = 0;

private CounterService counterService;

public CounterService getCounterService()

{

return counterService;

}

public void setCounterService(CounterService counterService)

{

this.counterService = counterService;

}

public ActionForward updatepv(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

counterService = new CounterServiceImpl();

Integer time = new Integer(Integer.parseInt("20090403"));

Counter counter = this.counterService.findCounterByTime(time);

this.counterService.updateCounterPv(counter, activeSessions);

request.getSession(true).setAttribute("counter", counter);

return null;

}

public ActionForward showpv(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

Integer time = new Integer(20090403);

Counter counter = this.counterService.findCounterByTime(time);

this.counterService.updateCounterPv(counter, activeSessions);

return mapping.findForward("success");

}

public void sessionCreated(HttpSessionEvent se) {

activeSessions++;

Integer time = new Integer(20090403);

String[] locations = {"applicationContext-common.xml","applicationContext-counter.xml"};

ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);

CounterDAO counterdao = (CounterDAOImpl) ctx.getBean("counterDAO");

List list=(List<Counter>) counterdao.findCounterByTime(time);

Counter counter = (Counter) list.get(0);

Iterator counterIterator = list.iterator();

while(counterIterator.hasNext()){

counter = (Counter)counterIterator.next();

}

counter.setPv(activeSessions);

counterdao.updateCounter(counter);

}

public void sessionDestroyed(HttpSessionEvent se) {

if(activeSessions > 0)

activeSessions--;

}

public static int getActiveSessions() {

return activeSessions;

}

}

这个类运行也不报错,数据库操作也能通过,问题是似乎数据库并不commit,我在数据库中用的是spring getHibernate()的相关方法,我还特意做了一个action,如果人为的去触发这个action,数据库一切按照原计划正常运行(可以更新,查询),也就是说这个类应该没错,可是一旦用了监听session后,就不更新数据库,也不报错,各位大侠帮忙看看