JDBC/Connectorj:了解连接池 [英] JDBC/Connectorj: Understanding connection pooling

查看:182
本文介绍了JDBC/Connectorj:了解连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我需要更好地了解连接池的概念.我正在使用ConnectorJ在Java中工作,并将servlet部署在Apache Tomcat服务器上.我一直在关注该文档,因此我的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>

然后使用推荐的方式从数据源获得连接:

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
}

我的问题是:为什么我必须在context.xml中为我的数据源指定用户和密码.如果我错了,请纠正我,但我认为连接池的重点是重用拥有相同连接字符串的连接吗?

我希望能够处理多种不同的登录(例如,servlet通过HTTP接收要使用的数据库凭据),但是如果我必须为每个可能的连接定义不同的数据源,那是否不可行连接池的重点?

解决方案

使用DriverManager.getConnection直接打开与数据库的连接时,您将提供用户名和密码以在该调用中登录数据库. /p>

使用连接池时,不是直接自己打开连接;而是直接打开连接.相反,该池将打开并为您管理连接.当然,在这种情况下,池需要知道用户名和密码才能登录数据库.

通常,在Java Web应用程序中,您不会为应用程序的每个用户使用不同的数据库登录凭据.对于使用Web应用程序的任何人,您只有一个应用程序使用的用户名和密码.如果Web应用程序的不同用户具有不同的权限,则可以通过使用该应用程序本身的登录系统进行设置,但是用于该应用程序的用户名和密码与用于登录的用户名和密码不同进入数据库.

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. I've been following the doc so my Tomcat context.xml looks like this:

<?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. 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 ?

解决方案

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.

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. You'd just have one username and password that the application uses, for anybody who uses the web application. 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.

这篇关于JDBC/Connectorj:了解连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆