關於SSH中對於action的監聽問題(關於系統計數)

liweitsky發表於2009-04-27
最近想做一個系統計數功能,網上看了一下資料,用的是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後,就不更新資料庫,也不報錯,各位大俠幫忙看看

相關文章