在这段代码中,我需要关闭dao。调用daoClose()方法。但我什么时候可以打电话到哪里?这是最有效的。 我需要确定哪个连接已挂起然后关闭它。怎么抓住这个时刻?
public class UserController extends HttpServlet {
private static final long serialVersionUID = 7589862078860584987L;
private static String INSERT_OR_EDIT = "/user.jsp";
private static String LIST_USER = "/listUser.jsp";
private UserDao dao;
private long begin = System.nanoTime ();
public UserController() {
super();
dao = new UserDao();
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
int userId = Integer.parseInt(request.getParameter("userId"));
dao.deleteUser(userId);
request.setAttribute("users", dao.getAllUsers());
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
int userId = Integer.parseInt(request.getParameter("userId"));
User user = dao.getUserById(userId);
request.setAttribute("user", user);
}
private void listUsers(HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("users", dao.getAllUsers());
}
private void common (HttpServletRequest request, HttpServletResponse response) {
String forward="";
String action = request.getParameter("action");
if (action.equalsIgnoreCase("delete")){
delete(request, response);
forward = LIST_USER;
} else if (action.equalsIgnoreCase("edit")){
forward = INSERT_OR_EDIT;
edit(request, response);
} else if (action.equalsIgnoreCase("listUser")){
forward = LIST_USER;
listUsers(request, response);
} else {
forward = INSERT_OR_EDIT;
}
RequestDispatcher view = request.getRequestDispatcher(forward);
try {
view.forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
在这段代码中,我需要关闭dao。调用daoClose()方法。但我什么时候可以打电话到哪里?这是最有效的。 我需要确定哪个连接已挂起然后关闭它。怎么抓住这个时刻?
private void commonSec( HttpServletRequest request, HttpServletResponse response) {
User user = new User();
user.setFirstName(request.getParameter("firstName"));
user.setLastName(request.getParameter("lastName"));
try {
Date dob = new SimpleDateFormat("MM/dd/yyyy").parse(request.getParameter("dob"));
user.setDob(dob);
} catch (ParseException e) {
e.printStackTrace();
}
user.setEmail(request.getParameter("email"));
String userid = request.getParameter("userid");
if(userid == null || userid.isEmpty())
{
dao.addUser(user);
}
else
{
user.setUserid(Integer.parseInt(userid));
dao.updateUser(user);
}
RequestDispatcher views = request.getRequestDispatcher(LIST_USER);
listUsers(request, response);
try {
views.forward(request, response);
} catch (ServletException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
common(request, response);
System.out.println(delta);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
commonSec(request, response);
System.out.println(delta);
}
private void daoClose() {
dao.closeCn();
System.out.println("run");
}
private long end = System.nanoTime ();
private long delta = end - begin;
}
答案 0 :(得分:0)
为确保dao在成功/失败时关闭,您可以使用finally
块。
https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
例如
try {
// use your dao
} catch (Exception e) {
// handle error
} finally {
daoClose();
}
finally块中的任何代码都保证运行,因此无论是否发生错误,您都可以确保dao在结束时关闭
答案 1 :(得分:0)
最好使用try(UserDao dao = new UserDao())
,不要将它作为类的成员,它不是线程安全的。我建议你多读一些关于交易的内容,也要按照常规的方式开始学习。