简体   繁体   English

将基本数据源与连接池数据源混合:何时调用close()?

[英]mixing basic DataSource with connection pooling DataSource: when to call close()?

I'm in the process of adding connection pooling to our java app. 我正在将连接池添加到我们的Java应用程序中。

The app can work with different rdbmses, and both as a desktop app and as a headless webservice. 该应用程序可以与不同的rdbmses一起使用,既可以作为桌面应用程序使用,也可以作为无头Web服务使用。 The basic implementation works fine in desktop mode (rdbms = derby). 基本实现在桌面模式下运行良好(rdbms = derby)。 When running as a webservice (rdbms = mysql), we see that connection pooling is needed to get good concurrent behaviour. 当作为Web服务运行(rdbms = mysql)时,我们看到需要使用连接池来获得良好的并发行为。

My initial approach was to use dependency injection to decide between a basic DataSource or a connection pooling DataSource at startup time. 我最初的方法是使用依赖项注入在启动时决定是基本数据源还是连接池数据源。

The problem in this scenario is that I don't know when to call connection.close(). 在这种情况下的问题是,我不知道何时调用connection.close()。 My understanding is that when using connection pooling, you should close() after each query so the connection object can be recycled. 我的理解是,使用连接池时,应在每个查询之后关闭(),以便可以回收连接对象。 But the current non-pooling implementation tries to hang on to the Connection object as long as possible. 但是当前的非池化实现尝试尽可能长时间地挂接到Connection对象。

Is there a solution to this problem? 有解决这个问题的方法吗? Is there a way to recycle a basic connection object? 有没有办法回收基本的连接对象? What happens if I don't call connection.close() with a thread pooled connection object? 如果我不使用线程池连接对象调用connection.close()会发生什么?

Or is it simply a bad design to mix these two connection strategies? 还是将这两种连接策略混合在一起只是一个糟糕的设计?

If I understand you correctly, essentially you are doing your own pooling implementation by holding on to the connection as long as possible. 如果我对您的理解正确,那么实际上您是在通过尽可能长时间地保持连接来进行自己的池实现。 If this strategy is successful (that is the program is behaving as you describe it), then you have your own pool already. 如果此策略成功(即程序按照您描述的那样运行),则您已经拥有了自己的池。 The only thing adding a pool will gain you is to improve the response time when you make a new connection (as you won't really be making one, you will be getting it from the pool), which is apparently not happening all that often. 添加池唯一可以使您受益的是改善建立新连接时的响应时间(因为您实际上不会建立连接,因此将从池中获得连接),这显然并不会经常发生。

So, I would cycle back to the assumption underlying this question: Is it in fact the case that your concurrent performance problems are related to database pooling? 因此,我将回到这个问题的基础假设:实际上,您的并发性能问题与数据库池有关吗? If you are using transactions in the MySQL and not the Derby, that could be a big cause of concurrency issues, as an example of a different potential cause. 如果您在MySQL中而不是在Derby中使用事务,那么这可能是导致并发问题的主要原因,例如,另一个潜在原因。

To answer your question directly, use a database pool all the time. 要直接回答您的问题,请始终使用数据库池。 They are very little overhead, and of course change the code to release connections quickly (that is, when the request is done, not as long as the user has a screen open, for example) rather than holding on to them forever. 它们的开销很小,并且当然可以更改代码以快速释放连接(即,在完成请求时,例如,只要用户没有打开屏幕,就可以释放连接),而不是永远保持连接。

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

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