---
title: "SQLServerException: DBComms.receive产生的原因"
author: "peter.kong"
date: "2007-01-08T10:46:51Z"
tags: []
slug: "sqlserverexception-dbcomms-receive"
draft: false
lastmod: "2007-01-08T10:46:51Z"
id: 44328
url: "https://peter-kong.iteye.com/blog/44328"
---

SQLServerException: DBComms.receive产生的原因

原文：http://www.javatang.com/archives/2006/04/28/053226.html

Filed under: SQL Server — Jet @ 12:05 am

使用SQLServer2005的JDBC进行SQL Server2000的操作, 程序中使用了多线程的”Worker Thread”模式, 但是在运行的过程中偶尔会产生这样的SQLServerException:

    使用 DBComms.receive 方法期间发生异常。操作:socket closed。上下文:(9) , IO:897a
    9, Dbc:null]。PktNum:0。TotalReceived:0。PktSize:4,096。

后来查了一些有限的资料, 终于知道了原因所在. 原来是因为程序中的代码是非线程安全的, JDBC对象是不能被多个线程共用的. 产生这个异常的原因可能是一个线程中Connection正在关闭或Statement正在被重新执行的时候另一个线程正在使用.

    Hi. I suspect it’s your application code (including the dbcp stuff) that may not be threadsafe. Your JDBC objects can’t be shared by multiple threads. I suspect a connection is being closed, or a statement is being re-executed by one thread while another is still using it.
    Joe Weinstein at BEA Systems