Pitfalls of sharing a connection among threads

本文探讨了在多线程环境中共享单一数据库连接的潜在陷阱,包括事务管理、资源关闭和线程间交互的问题。建议为每个线程分配独立的连接,避免意外关闭其他线程的资源,并提出了一种策略,即让一个线程负责数据库访问,其他线程从该线程获取信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here is a review of the potential pitfalls of sharing a single Connection among multiple threads.

  1. Committing or rolling back a transaction closes all open ResultSet objects and currently executing Statements, unless you are using held cursors.

  2. If one thread commits, it closes the Statements and ResultSets of all other threads using the same connection.

  3. Executing a Statement automatically closes any existing open ResultSet generated by an earlier execution of that Statement.
    If threads share Statements, one thread could close another’s ResultSet.

In many cases, it is easier to assign each thread to a distinct Connection. If thread A does database work that is not transactionally related to thread B, assign them to different Connections. For example, if thread A is associated with a user input window that allows users to delete hotels and thread B is associated with a user window that allows users to view city information, assign those threads to different Connections. That way, when thread A commits, it does not affect any ResultSets or Statements of thread B.

Another strategy is to have one thread do queries and another thread do updates. Queries hold shared locks until the transaction commits in SERIALIZABLE isolation mode; use READ_COMMITTED instead.

Yet another strategy is to have only one thread do database access. Have other threads get information from the database access thread.

Multiple threads are permitted to share a Connection, Statement, or ResultSet. However, the application programmer must ensure that one thread does not affect the behavior of the others.

Recommended Practices

Here are some tips for avoiding unexpected behavior:

  1. Avoid sharing Statements (and their ResultSets) among threads.
    Each time a thread executes a Statement, it should process the results before relinquishing the Connection.
  2. Each time a thread accesses the Connection, it should consistently commit or not, depending on application protocol.
  3. Have one thread be the “managing” database Connection thread that should handle the higher-level tasks, such as establishing the Connection, committing, rolling back, changing Connection properties such as auto-commit, closing the Connection, shutting down the database (in an embedded environment), and so on.
  4. Close ResultSets and Statements that are no longer needed in order to release resources.
    Parent topic:
    Working with multiple threads sharing a single connection

Related concepts

Multi-thread programming tips

Related reference

Example of threads sharing a statement

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值