日期:2014-05-18  浏览次数:20544 次

100分求帮忙解决死锁问题
程序是这样写的:

在类article.cs中

defConnStr=数据库连接
public static SqlDataReader ArticleInner(int ArticleID)
  {
  SqlConnection con = new SqlConnection(defConnStr);
  SqlCommand cmd = new SqlCommand("SP_GetArticleDetail", con);
  cmd.CommandType = CommandType.StoredProcedure;
  //存储过程参数添加
  cmd.Parameters.Add("@ID", SqlDbType.Int);
  cmd.Parameters["@Article_ID"].Value = ArticleID;
  con.Open();
  return (SqlDataReader)cmd.ExecuteReader();
  }

显示页面中:

SqlDataReader dr;
dr = Article.ArticleInner(articleID);
if (dr.Read())
{
  ...
}
else{
  ...
}

如果是,用什么样的方法写比较好呢?请指教

------解决方案--------------------
1、不要返回DataReader,你可以返回一个实体类列表。
2、用Lock关键字锁住代码。
------解决方案--------------------
public static SqlDataReader ArticleInner(int ArticleID, Repeater RP)
{
SqlConnection con = new SqlConnection(defConnStr);
SqlCommand cmd = new SqlCommand("SP_GetArticleDetail", con);
cmd.CommandType = CommandType.StoredProcedure;
//存储过程参数添加
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@Article_ID"].Value = ArticleID;
try 
{ con.Open();
RP.DataSource=cmd.ExecuteReader(); //////////////// 这里,不要直接用Reader,使用DataAdapter 填充到DataTabl 里
RP.DataBind();

finally 

cmd.Dispose(); 
con.Close(); 
con.Dispose(); 

}

------解决方案--------------------
由于不了解你的程序是如何设计的,乱说对你也无益。指给你一个文章,希望对你有帮助

http://blog.csdn.net/octverve/archive/2007/09/08/1776867.aspx


------解决方案--------------------
Exception information:
Exception type: InvalidOperationException
Exception message: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
大概意思是:连接超时,可能原因是连接池已满.

很多使用完的连接没有关闭,建议检查所有与数据库相关的代码是否正确关闭连接,特别是使用了Reader的地方.
------解决方案--------------------
如果是连接池不够
在配置文件中增加连接池 
<add key="ccc" value="Server='.';UID=sa;password=sa;Database='Northwind';Min Pool Size=7;Max Pool Size=50" />
------解决方案--------------------
不是死锁,是连接数超出连接池最大连接数了!
原因是你没有及时关闭不需要的连接。连接要用完后立即关闭。