我正在使用 Java 在 Web 项目中创建数据库连接。然而,有一件事让我感到困惑;
- 创建数据库连接和关闭连接的最佳方式是什么?使用
Filter
或ServletContextListener
? - 一旦我在 servlet 中执行数据库操作,JDBC 连接将如何关闭?我需要手动关闭连接吗?我应该在哪里关闭连接?我确实添加了
contextDestroyed
关闭连接的方法,但我认为它只有在 Tomcat 服务器停止时才有效。 - 或者,我需要打开和关闭
Filter
中的连接吗? ?
这是我工作的步骤
- 创建
DataSource
在 Tomcat 服务器的Context.xml
使用<resource>
文件标签 - 引用
web.xml
中的资源通过使用<resource-ref>
标签 - 在
contextInitialized
中创建连接的ServletContextListener
- 在servlet的
doPost
中进行数据库操作或doGet
方法
contextInitialized
方法:
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("START CONNECTION");
try {
Context contextEnvironment = (Context) new InitialContext().lookup("java:comp/env/");
DataSource ds = (DataSource) contextEnvironment.lookup("jdbc/lunaruniversity");
try {
Connection con = ds.getConnection();
ServletContext context = event.getServletContext();
context.setAttribute("dbConnection", con);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (NamingException e) {
e.printStackTrace();
}
}
servlet 的 doGet
方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con = (Connection) getServletContext().getAttribute("dbConnection");
String saveStudent = request.getParameter("saveStudent");
String insertSQL = "INSERT INTO STUDENT (FNAME, LNAME, EMAIL, PHONE, STATE, ZIPCODE) VALUES(?,?,?,?,?,?)";
String fName = request.getParameter("fName");
String lName = request.getParameter("lName");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String state = request.getParameter("state");
String zipCode = request.getParameter("zipCode");
try {
java.sql.PreparedStatement ps = con.prepareStatement(insertSQL);
ps.setString(1, fName);
ps.setString(2, lName);
ps.setString(3, email);
ps.setString(4, phone);
ps.setString(5, state);
ps.setString(6, zipCode);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
contextDestroyed
方法:
@Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println("CLOSE CONNECTION");
DataSource ds = (DataSource) event.getServletContext().getAttribute("dbConnection");
try {
if (!ds.getConnection().isClosed()) {
System.out.println("CONNECTION IS CLOSED");
ds.getConnection().close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
最佳答案
1、2、3:以上都不是。
当您需要开始交易时,您会从 DataSource 获得一个连接。您在参与该事务的所有方法调用中传递该连接。然后提交并关闭连接。使用 try-with-resources 确保它始终关闭,即使在出现异常的情况下也是如此:
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try (Connection connnection = dataSource.getConnection()) {
// use the connection
connection.commit();
}
}
关于java - 在 ServletContextListener 与 Filter 中创建和关闭数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46226903/