JDBC:我可以在多线程应用程序中共享连接并享受良好的交易吗? [英] JDBC: Can I share a connection in a multithreading app, and enjoy nice transactions?

查看:84
本文介绍了JDBC:我可以在多线程应用程序中共享连接并享受良好的交易吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎用JDBC处理事务的经典方法是将auto-commit设置为false.这将创建一个新事务,并且每次提交的调用都将标记下一个事务的开始. 在多线程应用程序上,我了解为每个线程打开一个新连接是一种常见的做法.

It seems like the classical way to handle transactions with JDBC is to set auto-commit to false. This creates a new transaction, and each call to commit marks the beginning the next transactions. On multithreading app, I understand that it is common practice to open a new connection for each thread.

我正在编写一个基于RMI的多客户端服务器应用程序,因此基本上我的服务器为每个新连接无缝地产生了一个线程. 为了正确处理事务,我应该为每个线程创建一个新的连接吗? 这样的架构的成本不高吗?

I am writing a RMI based multi-client server application, so that basically my server is seamlessly spawning one thread for each new connection. To handle transactions correctly should I go and create a new connection for each of those thread ? Isn't the cost of such an architecture prohibitive?

推荐答案

是的,通常需要为每个线程创建一个新的连接.您无法控制操作系统如何对线程的执行进行时间片划分(尽管定义了自己的关键部分),因此您可能会无意中使多个线程试图沿着那个管道发送数据.

Yes, in general you need to create a new connection for each thread. You don't have control over how the operating system timeslices execution of threads (notwithstanding defining your own critical sections), so you could inadvertently have multiple threads trying to send data down that one pipe.

请注意,这同样适用于任何网络通信.例如,如果您有两个线程试图通过HTTP连接共享一个套接字.

Note the same applies to any network communications. If you had two threads trying to share one socket with an HTTP connection, for instance.

  • 线程1发出请求
  • 线程2发出请求
  • 线程1从套接字读取字节,不知不觉地从线程2的请求读取响应

如果将所有事务包装在关键部分中,并因此在整个开始/提交周期中锁定所有其他线程,则可以在线程之间共享数据库连接.但是,除非您真的具有JDBC协议的先天知识,否则我什至不会这样做.

If you wrapped all your transactions in critical sections, and therefore lock out any other threads for an entire begin/commit cycle, then you might be able to share a database connection between threads. But I wouldn't do that even then, unless you really have innate knowledge of the JDBC protocol.

如果大多数线程很少需要数据库连接(或根本不需要),则可以指定一个线程来完成数据库工作,并让其他线程将他们的请求排队到该线程.这将减少许多连接的开销.但是您必须弄清楚如何管理环境中每个线程的连接(或在StackOverflow上询问有关此问题的另一个特定问题).

If most of your threads have infrequent need for database connections (or no need at all), you might be able to designate one thread to do your database work, and have other threads queue their requests to that one thread. That would reduce the overhead of so many connections. But you'll have to figure out how to manage connections per thread in your environment (or ask another specific question about that on StackOverflow).

更新:要在评论中回答您的问题,大多数数据库品牌不支持单个连接上的多个并发事务(InterBase/Firebird是我所知道的唯一例外).

update: To answer your question in the comment, most database brands don't support multiple concurrent transactions on a single connection (InterBase/Firebird is the only exception I know of).

最好有一个单独的事务对象,并且每个连接能够启动和提交多个事务.但是供应商根本不支持它.

It'd be nice to have a separate transaction object, and to be able to start and commit multiple transactions per connection. But vendors simply don't support it.

同样,JDBC和ODBC等标准的独立于供应商的API做出相同的假设,即事务状态仅仅是连接对象的属性.

Likewise, standard vendor-independent APIs like JDBC and ODBC make the same assumption, that transaction state is merely a property of the connection object.

这篇关于JDBC:我可以在多线程应用程序中共享连接并享受良好的交易吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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