每当我需要使用 c3Po 池获取连接时,是否有必要在每个 class 中调用 getConnection() ?

is it necessary to call getConnection() in every class whenever I need to get the connection using c3Po pooling?

我有一个 jsp 页面 LoginCheck.jsp,我想在其中调用数据库来验证登录详细信息,还有一个 java class CmDatabaseConnection,我在其中创建连接池。

LoginCheck.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import ="java.sql.*" %>
<%@ page import ="java.util.Date" %>
<%@ page import ="com.wipro.clk.CmDatabaseConnection" %>
<%@ page import ="com.mchange.v2.c3p0.ComboPooledDataSource" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String userid = request.getParameter("username");
System.out.println("entered username is " +userid);
String pwd=request.getParameter("password"); 
System.out.println("entered password is " +pwd);

ComboPooledDataSource dataSource = CmDatabaseConnection.makeConnectionPool();

Connection con=dataSource.getConnection();


/* Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/CCB25", "CISADM", "CISADM"); */

//
// USE BINDS ! to avoid sql injection
// push the userid and passwd comparison to the db 
// no need to get the password and compare locally
// 
String query = "select password from cmlogin where userid=? and password=?";

PreparedStatement st = con.prepareStatement(query);

st.setString(1,userid);
st.setString(2,pwd);

ResultSet rs  = st.executeQuery(); 
System.out.println("Size is  "+st.getFetchSize());
String pass1;

//  .next will advance if the query has any results
// 

if (rs.next()) {
pass1 = rs.getString("password");
System.out.println("value of result pass1" +pass1);

String name =request.getParameter("username");
System.out.println(name);
session.setAttribute("nam",name);
%> 
<jsp:forward page="admin.jsp"></jsp:forward>
<%
} else {
String msg="Username or password failed";
%>
<center> <p style="font-family:verdana;color:red;"> <%=msg %>
<jsp:include page="Login.jsp"></jsp:include>
<%  } 
con.close();
%>
</body>

CmDatabaseConnection.java

package com.wipro.clk;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class CmDatabaseConnection {



public static ComboPooledDataSource  makeConnectionPool()
{
ComboPooledDataSource cpds = new ComboPooledDataSource();
try
{

cpds.setDriverClass("oracle.jdbc.driver.OracleDriver");
cpds.setJdbcUrl("jdbc:oracle:thin:@localhost:1521/CCB25");
cpds.setUser("CISADM");
cpds.setPassword("CISADM");
cpds.setMaxPoolSize(100);
cpds.setMinPoolSize(5);
cpds.setMaxStatements(180);  
cpds.setAcquireIncrement(20);


} 
catch (Exception ex) 
{

ex.printStackTrace();
}
return cpds;

}


}

现在我的疑问是,每当我想调用数据库时是否需要调用下面的行?

ComboPooledDataSource dataSource = CmDatabaseConnection.makeConnectionPool();
Connection con=dataSource.getConnection();

或者是否可以获取一次数据库连接并在每个 java/jsp 文件中使用相同的连接?如果我们在每个文件中都使用相同的文件,那么是否不需要关闭连接?

您仍然需要 getclose 连接。 但是,连接池解决方案(例如 C3P0)正在关注连接重用和其他优化。

绝对不应该在每次调用时都创建数据源,它应该创建一次。很可能最好考虑您的应用程序服务器(例如 Tomcat)获取数据源(例如通过 JNDI)的能力。

有必要在任何需要连接的地方使用 pool.getConnection(),在不再需要连接时使用 connection.close()

这就是连接池的工作原理。

您创建一个连接池,在需要时从池中获取一个 (pool.getConnection()),并在不再需要时 return 将其放入池中 (connection.close())

使用连接池时,它会根据池实现创建一个(new/set 个到数据库的连接(在 go/at 上同时)。

当您调用时,connection.close()它return将连接返回到池而不是终止连接(您的数据库池实现会处理这个)。

这在功能上与您想要的相似。

我建议 Java Hikari Pool,因为它很快。