weixin_45965756
Aias_小枫
采纳率100%
2019-12-04 00:37

javaweb小项目,在写完登录功能时设置断点查看程序运行状况时得到的结果和直接运行得到的结果不一样???

初学者今天打算自己写一下小项目,写完登录功能后运行了没什么问题。
但是设置断点之后打算一步步看运行过程,发生了诡异的事情,因为之前用断点查看运行过程没出现过这样的事,首先正常启动得到正常的结果,加了断点走却得不到一样的结果。
程序正常运行大概流程就是在login.html填上用户名和密码发送请求给LoginServlet调用service,service调dao执行sql查询是否有这个用户。
事务处理用了动态代理,dao层的实现类也依靠mybatis的动态代理,只写sql语句。
一般我发现问题后都会上网查,但是这个加不加断点影响结果的事情查了一下午都没有找到解决办法,我设置了两个点,34行(断点1)和46行(断点2)。

我发现把 断点1 设置在获取动态代理对象agent(图里第35行代码)那一句代码之前,直走断点1不跳过,那么37行得到的emp对象会是null,也就是找不到这个用户。

明明输入了正确的用户名和密码,而且不走断点直接运行是可以验证通过的。

接下来更诡异的是,我选择走断点的方式,但是我走到断点1的时候,选择跳过断点1,居然得到了emp这个对象,然后验证成功。

这到底咋回事阿????
下面第一张图是跳过断点1
第二张图是走断点1
这是跳过断点1
这是走断点1

试了一下跟着断点走到动态代理类的时候报了反射异常InvocationTargetException,应该是动态代理类写错了?
图片说明

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cdx.psms.utils.TransactionInvocationHandler.invoke(TransactionInvocationHandler.java:29)
    at com.sun.proxy.$Proxy6.login(Unknown Source)
    at com.cdx.psms.controller.LoginServlet.doPost(LoginServlet.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
### Cause: org.apache.ibatis.executor.ExecutorException: Executor was closed.
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
    at com.sun.proxy.$Proxy5.login(Unknown Source)
    at com.cdx.psms.service.EmpServiceImpl.login(EmpServiceImpl.java:17)
    ... 31 more
Caused by: org.apache.ibatis.executor.ExecutorException: Executor was closed.
    at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:197)
    at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
    ... 37 more

public class EmpServiceImpl implements EmpService {

private EmpDao empDao = SqlSessionUtil.getSession().getMapper(EmpDao.class);

@Override
public Emp login(String username,String password) {

    Emp emp = new Emp();
    emp.setUsername(username);
    emp.setPassword(password);

    emp = empDao.login(emp);

    return emp;
}

}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • guangcaiwudong Kevin.Y.K 2年前

    把项目重新编译,再运行

    点赞 评论 复制链接分享
  • hj_java1 无悔_人生 2年前

    在TransactionInvocationHandler的invoke方法里打个断点,走到27行看下obj和args的值是否正确,另外看下EmpServiceImpl的login方法17行两次运行有什么不一样,逐一排查

    点赞 评论 复制链接分享