用户的每个请求都调用一个新的getConnection()? [英] Each request from user call a new getConnection()?

查看:304
本文介绍了用户的每个请求都调用一个新的getConnection()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库连接如下所示

public Connection getDBConection(){  
   Context context = new InitialContext();  
   DataSource dataSource = (javax.sql.DataSource) context.lookup("java:myDataSource");  
   Connection conn = dataSource.getConnection();  
}  

对于userA,每个数据库请求都应调用 getDBConnection()一次;但无需控制所有请求使用同一连接?

For a userA, is each database request should call getDBConnection() once; but no need to control all request use the same connection?

也就是说,如果userA有三个数据库请求,则userA应该调用 getDBConnection()三次,然后调用在每个请求中使用 Connection.closed()之后?

That is, if userA has three database request, then userA should call getDBConnection() three times, and call Connection.closed() after used in each request?

如果userA调用 getDBConnection()三次(即,调用 dataSource。 getConnection()三次),是否创建了三个连接?还是未知的并由weblogic控制?

If the userA call getDBConnection() three times (that is, call dataSource.getConnection() three times), is three connection created? Or it is unknown and controlled by weblogic?

我感到非常混乱,对一个数据库请求应该有一个新的连接吗?或仅针对每个数据库请求调用 DataSource.getConnection(),新创建的连接数由Web服务器控制,无需考虑实际创建了多少个连接。 / p>

I feel very chaos, is it true that there should be one new connection for one database request? or just call DataSource.getConnection() for each database request and the number of new connection created is controlled by web server, no need to think how many connection is actually created.

推荐答案

每次调用 DataSource.getConnection 时,数据源都会检索为您建立联系。应该确实返回的连接没有被其他任何人积极使用,但是它不一定是全新的连接。

Every time you call DataSource.getConnection, the data source will retrieve a connection for you. It should be true that the returned connection is not being actively used by anyone else, but it is not necessarily a brand-new connection.

例如,如果您使用a连接池,这是很常见的做法,然后当您调用 Connection.close 时,该连接实际上并未关闭,而是返回到可用连接池中。然后,当您调用 DataSource.getConnection 时,连接池将查看它是否还有尚未分发的备用连接。如果是这样,通常会测试它们是否过时(通常通过对虚拟表执行非常快速的查询)。如果没有,它将把现有的连接返回给呼叫者。但是,如果连接陈旧,则连接池将从底层数据库驱动程序检索一个真正新的连接,并返回该连接。

For example, if you use a connection pool, which is a very common practice, then when you call Connection.close, the connection is not actually closed, but instead returns to a pool of available connections. Then, when you call DataSource.getConnection, the connection pool will see if it has any spare connections lying around that it hasn't already handed out. If so, it will typically test that they haven't gone stale (usually by executing a very quick query against a dummy table). If not, it will return the existing connection to the caller. But if the connection is stale, then the connection pool will retrieve a truly new connection from the underlying database driver, and return that instead.

通常,连接池具有最大数量他们一次保持的实际连接数(例如50)。如果您的应用程序尝试请求50个以上的同时连接,则 DataSource.getConnection 将引发异常。或在某些实现中,它将阻塞一会儿直到可用,然后在该时间到期后引发异常。对于示例实现,请查看 Apache Commons DBCP

Typically, connection pools have a maximum number of real connections that they will keep at any one time (say, 50). If your application tries to request more than 50 simultaneous connections, DataSource.getConnection will throw an exception. Or in some implementations, it will block for a while until one becomes available, and then throw an exception after that time expires. For a sample implementation, have a look at Apache Commons DBCP.

希望能回答您的问题!

这篇关于用户的每个请求都调用一个新的getConnection()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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