为什么java连接不能createstatement()而executeQuery()正在处理中?(why java connection cannot createstatement() while

编程入门 行业动态 更新时间:2024-10-28 02:34:04
为什么java连接不能createstatement()而executeQuery()正在处理中?(why java connection cannot createstatement() while an executeQuery() is in the middle of processing ?)

我有一个到数据库的静态Java连接。

第一线程

我的静态连接我正在创建一个语句并调用executeQuery()方法。 此时我正在等待,因为查询结果非常大,我必须等待超过15分钟。

第二线程

我使用相同的静态连接尝试调用createStatement()方法。 这里第二个线程被阻止了。

有很多方法可以解决这个问题,但我找不到为什么这个方法不起作用。 有任何想法吗?

I have a static java connection to a databases.

1st Thread

with my static connection I am creating a statement and call to executeQuery() method. at this point I'm waiting because the query results is very big and I have to wait more than 15 minutes.

2nd Thread

with the same static connection I am trying to make a call to createStatement() method. And here the 2nd thread is blocked.

There are many ways to solve this issue, but I don't find anywhere why this aproach should not working. Any ideas?

最满意答案

通常,通过Sockets机制实现对数据库的单个Connection 。 更好的说,你的单个connection是某种管道,它用于具体查询,每次执行一次。

很难想象这样的最佳和高性能代码实现,尽管一个套接字多个查询及其结果是并行发送的。 这样的实现可能导致与数据包的混淆,并且至少会丢失它们。

正如karmanaut在评论中提到的那样,您需要实现数据库池,即连接池,它将同时用于每个语句。

In a common way, the single Connection to the database is implemented through Sockets mechanism. Better to say, that your single connection is some kind of pipe, which is using for concrete query which is executing one per time.

It is hard to imagine such optimal and high-perfomance code implementation, where though one socket multiple queries and their results are sent in parallel. Such implementation could cause the confusion with data-packets and at least their lost.

As karmanaut mentioned above in comments, you need to implement DB pool, i.e. pool of connections, which will be used for each statement concurrently.

更多推荐

本文发布于:2023-08-07 15:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:executeQuery   createstatement   java   processing   middle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!