Java:JDBC数据库连接池 [英] Java: JDBC Database connection pool

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

问题描述

HI:我有一个多线程Java数据库应用程序,我们必须创建一个自定义的数据库池。原因是我们的一些预备陈述必须在连接中缓存。我们有一个基于一个免费hashmap的原始解决方案,另一个是锁定的hashmap。使用hashmap,我们必须使用synchronized方法,然后我们的吞吐量会受到影响。选择是使用concurrentHashMap,以便同步部分尽可能小。有一些示例代码吗?或者我应该下载apache DBCP源代码来阅读?

HI: I have a multi thread Java database application, we have to create a customized database pooling. The reason is that some of our preparedstatement has to be cached in the connection. We have our primitive solution based on one free hashmap, the other locked hashmap. With hashmap, we have to use synchronized method, then our throughput is influenced. The choice is to use concurrentHashMap so that synchronization part would be as small as possible. Are there some sample codes? Or shall I download apache DBCP source codes to read?

推荐答案

使用synchronized会花费你大约1-2微秒。如果这对您至关重要,则不应使用JDBC。恕我直言。仅通过TCP连接访问服务可能需要100微秒,而许多JDBC数据库的延迟为1-10毫秒。

Using synchronized will cost you about 1-2 microseconds. If this is critical to you, you shouldn't be using JDBC. IMHO. Just accessing a service over a TCP connection is likely to cost 100 micro-seconds and many JDBC databases have a latency of 1-10 milli-seconds.

我怀疑每次查询/更新几毫秒都适合你,在这种情况下使用同步不太重要。

I suspect that a few milliseconds per query/update is fine for you in which case using synchronization is unlikely to matter.

根据您拥有的线程数量,您可以为每个线程建立一个线程本地连接。这样可以尽可能地减少开销。

Depending on how many threads you have, you can have a thread local connection for each thread. This reduces the overhead as much as possible.

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

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