简体   繁体   English

JDBC/Connectorj:了解连接池

[英]JDBC/Connectorj: Understanding connection pooling

I think I need to understand the concept of connection pool a bit better.我想我需要更好地理解连接池的概念。 I'm working in java with ConnectorJ and I deploy my servlet on a Apache Tomcat server.我正在使用 ConnectorJ 在 java 中工作,并将我的 servlet 部署在 Apache Tomcat 服务器上。 I've been following the doc so my Tomcat context.xml looks like this:我一直在关注文档,所以我的 Tomcat context.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="">
  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
        maxActive="-1" maxIdle="30"
        maxWait="10000" minEvictableIdleTimeMillis="1200000" name="jdbc/MySQLDB"
        removeAbandoned="true" removeAbandonedTimeout="1200"  timeBetweenEvictionRunsMillis="60000"
        type="javax.sql.DataSource" url="jdbc:mysql://my_host"
        username="my_username" password="my_password"
        factory="org.apache.commons.dbcp.BasicDataSourceFactory" /> 
</Context>

And I get a connection from a datasource using the recommanded way:我使用推荐的方式从数据源获得连接:

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
Connection conn = null;

try {
        conn = ds.getConnection();

        // Do query etc.
        // Close connection, statement and result set if applicable
}
catch (SQLException){
    // Handle exception here
}

My question is: why do I have to specify a user and password for my datasource in context.xml.我的问题是:为什么我必须在 context.xml 中为我的数据源指定用户和密码。 Correct me if I am wrong, but I thought the point of a connection pool was to reuse the connections that possesses the same connection string?如果我错了,请纠正我,但我认为连接池的目的是重用拥有相同连接字符串的连接?

I want to be able deal with multiple different login (let's say the servlet receive the DB credentials to use via HTTP), but if I have to define a different datasource for each of the possible connection, doesn't it go against the point of connection pooling?我希望能够处理多个不同的登录(假设 servlet 接收要通过 HTTP 使用的数据库凭据),但是如果我必须为每个可能的连接定义不同的数据源,不是 go连接池?

When you open a connection to the database directly, by using DriverManager.getConnection , you supply the username and password to log on to the database in that call.当您使用DriverManager.getConnection直接打开与数据库的连接时,您提供用户名和密码以在该调用中登录数据库。

When you use a connection pool, you are not opening the connection yourself directly;当你使用连接池时,你并不是直接自己打开连接; instead, the pool opens and manages the connections for you.相反,池会为您打开并管理连接。 Ofcourse, the pool needs to know the username and password to be able to log on to the database in that case.当然,在这种情况下,池需要知道用户名和密码才能登录到数据库。

Normally, in a Java web application, you would not use different database login credentials for every user of your application.通常,在 Java web 应用程序中,您不会为应用程序的每个用户使用不同的数据库登录凭据。 You'd just have one username and password that the application uses, for anybody who uses the web application.对于使用 web 应用程序的任何人,您只需要一个应用程序使用的用户名和密码。 If different users of the web application have different rights, you'd set that up by having a login system for the application itself, but the usernames and passwords that you use for the application are not the same as what you'd use to log on to the database.如果 web 应用程序的不同用户具有不同的权限,您可以通过为应用程序本身设置登录系统来进行设置,但您用于应用程序的用户名和密码与您用于登录的用户名和密码不同到数据库。

Connections to the database are usually controlled by different username and password than what your users will use for authentication on the website.与数据库的连接通常由不同的用户名和密码控制,而不是用户在网站上用于身份验证的用户名和密码。 Typically it's only one user (often being a schema name).通常它只有一个用户(通常是模式名称)。 Connection pool will create required number of connections to the database with this user name and it will be your application responsibility to take incoming credentials from the web user and verify them against stored in the database.连接池将使用此用户名创建所需数量的数据库连接,您的应用程序将负责从 web 用户获取传入凭据,并根据存储在数据库中验证它们。

Several things:几件事:

  1. DataSource API does provide a getConnection(username, password) method. DataSource API 确实提供了一个getConnection(username, password)方法。 For whatever reason, Apache DBCP that Tomcat uses for itself doesn't implement that method.无论出于何种原因,Tomcat 自己使用的 Apache DBCP 都没有实现该方法。 You might want to look into alternative implementations if DBCP doesn't meet your need.如果 DBCP 不能满足您的需要,您可能需要研究替代实现。
  2. As others stated, changing database credentials per request is a pretty obscure usecase.正如其他人所说,根据请求更改数据库凭据是一个非常模糊的用例。 Maybe you should reconsider your design, so that your user authentication is decoupled from your database access.也许您应该重新考虑您的设计,以便您的用户身份验证与您的数据库访问分离。
  3. More generally, pooling is a technique for reusing a number of things that are sufficiently alike that they can be reused multiple times.更一般地说,池化是一种重用许多足够相似的事物的技术,它们可以被多次重用。 Clearly, DBCP has decided that "sufficiently alike" doesn't include being reusable across different database credentials.显然,DBCP 已决定“足够相似”不包括可跨不同数据库凭据重用。 I don't think it's right or wrong on principle.我认为原则上没有对错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM