程序運行起來,執行一個頁面後返回一個新頁面的數據是上一個頁面的。這就痛苦了。控制檯提示以下java
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:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
at com.damu.dao.UsersDAO.findById(UsersDAO.java:37)
at com.damu.servlet.UserFindByIdServlet.doPost(UserFindByIdServlet.java:19)
at com.damu.servlet.UserFindByIdServlet.doGet(UserFindByIdServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
複製代碼
在查找衆多資料中,發現sessio在查詢數據所有的時候,已經關閉了,代碼以下:sql
public List<Users> findAll() {
try {
list = sqlSession .selectList("findAll");
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return list;
}
/*
* 查詢單個用戶*/
public Users findById(Integer id) {
try {
users = sqlSession.selectOne("findById", id);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return users;
}
複製代碼
在執行第一session的時候已經關閉了,修代碼以下apache
sqlSession = SqlSessionFactoryUtils.getsqlSessionFactory().openSession();
return sqlSession;
}
複製代碼
把要用的session抽象到一方法裏面,一個方法只是關閉一次。 總結:bash