我有一个到数据库的静态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.
更多推荐
发布评论