简体   繁体   English

JAVA处理大量JDBC池连接

[英]JAVA handling lots of JDBC pool connections

I've a problem when creating a batch in JAVA, this is that i might have to call a certain connection every second to process one block of rows, reaching the limit of the pool size. 在JAVA中创建批处理时,我遇到了一个问题,那就是我可能必须每秒调用某个连接来处理一个行块,达到池大小的限制。

How can i handle lots of connections without increase pool size ? 如何在不增加池大小的情况下处理大量连接?

using (var connection = ExternalDBConnectionManager.getConnection()){
        using (var prepStatement = connection.prepareStatement(queryToExecute)){
        ...
    }
}

This function is called every 100 rows or more, but for an example i'd like to run this every row (ie 500 rows, one call for each) - (this means call this function 500 times with the same connection) 该函数每100行或更多行被调用一次,但是例如,我想每行运行一次(即500行,每行调用一次)-(这意味着在相同连接下调用此函数500次)

You should read up on connection pool to get an idea of how they work. 您应该阅读连接池,以了解它们的工作原理。 There's hundreds of topics here on SO that pertain to this. SO上有数百个与此相关的主题。 A quick google search of "Understanding Java connection pools" brings up a small description of how one of the libraries might work: How Connection Pooling Works 在Google上快速搜索“ Understanding Java connection pools”,可以简要了解其中一个库的工作方式: Connection Pooling的工作方式

Some more info on this SO thread: How to establish a connection pool 有关此SO线程的更多信息: 如何建立连接池

Basically, your client requests come in, you grab a connection from the pool, you do your sql operation, and when you close the connection, it returns it back to the pool. 基本上,您的客户端请求进入,从池中获取连接,执行sql操作,然后在关闭连接时将其返回到池中。 That's the high level. 那是高水平。

If you want to combine that with batching, it gets a little trickier, since you would grab the data, insert it into the batch and then trigger the send/commit when you have some predetermined amount of data in the batch. 如果您想将其与批处理结合使用,它将变得有些棘手,因为您可以获取数据,将其插入批处理中,然后在批处理中有预定数量的数据时触发发送/提交。

It really depends on what you want your clients to see, do they need acknowledgement when the record is committed? 这确实取决于您希望客户看到的内容,提交记录时是否需要确认?

With pooling, it's less likely that you will need to use batching, you just need to adjust the connection pool size for your environment. 有了池,您就不太可能需要使用批处理,只需调整环境的连接池大小即可。

There's a number of standalone pool libraries (C3PO and DBCP), and a number of the various services (like websphere) already have pooling built in. I believe the Oracle jdbc driver as the ability to do it for you. 有许多独立的池库(C3PO和DBCP),并且已经内置了许多各种服务(如Websphere)。我相信Oracle jdbc驱动程序可以为您提供此功能。

Universal Connection Pool (UCP) is a Java connection pool which is rich in features and is tightly integrated with Oracle RAC, Oracle DG, Oracle ADG database architectures leveraging high availability, failover and performance features. 通用连接池(UCP)是Java连接池,具有丰富的功能,并与Oracle RAC,Oracle DG,Oracle ADG数据库体系结构紧密集成,从而利用了高可用性,故障转移和性能功能。

It is a standalone ucp.jar that needs to be used with JDBC driver. 它是一个独立的ucp.jar,需要与JDBC驱动程序一起使用。
There are some whitepapers on UCP and its capabilities on OTN . 关于UCP及其在OTN上的功能有一些白皮书。

You mentioned "batch". 您提到了“批处理”。 Usually batches don't use a connection pool which are reserved to OLTP applications. 通常,批处理不使用保留给OLTP应用程序的连接池。 That's because a connection pool works well if connections are borrowed and released fairly quickly. 这是因为如果相当快地借用和释放连接,则连接池会很好地工作。 Batches usually create their own connections which stay opened during the lifetime of the batch (which could be hours). 批次通常创建自己的连接,这些连接在批次的生命周期(可能是数小时)内保持打开状态。

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

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