且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据库连接关闭不够快?

更新时间:2023-10-13 17:51:40

一种不完整的代码片段,但在你处理它之前如何关闭你的连接




-


Andrew Robinson
www.binaryocean .com
www.bellinghamdotnet.org

布伦特 <"" b b i g l e r \@ y a h o o。 c o m>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
我快跑了超出可用的数据库连接与下面的代码。我想,当我继续关注时,我想要关闭所有内容,但是当方法getIDs发挥作用时,我会立即看到
连接。
有没有人看到任何明显的东西?这几乎就像我的代码不能像打开它一样快地关闭数据库连接......

感谢您的帮助!

--Brent

================================
代码片段
================================
//创建数组字符串[] id后

for(int i = 0; i< 50; i ++)
{
string theseids = getIDs(Int32.Parse(ids [i]));

if(theseids!=" 0")
{
Response.Write(cusips.ToString());
}

}

公共字符串getIDs(int id)
{String> StringBuilder ids = new StringBuilder();

//设置sql连接& sql语句

myConn.Open(); //打开连接
//设置datareader并循环遍历行,
//附加到每个
//循环的StringBuilder

如果(ids.ToString()。长度> 0)
{
返回ids.ToString();
}
其他
{
返回" 0"
}
如果(myConn!= null){myConn.Dispose();}

}



嗯...尝试过,没有运气......


完整的代码,不是更多,在这里:


======================================= =========== =

public void writeIDs()

{


StringBuilder sbsql = new StringBuilder();


//获取我们想要查看的数组。

string [] filingIDs = getFilingIDs();


for(int i = 0; i< 50; i ++)

{

string theseids = getIDs(Int32.Parse(filingIDs [i ]));

if(theseids!=" 0")

{

output(theseids.ToString());

}


}


}

公共字符串getIDs(int filingid)

{

StringBuilder idlist = new StringBuilder();

string sql =" select thisid from holdings where thisid =" + filingid;


MySqlConnection myConn;

myConn = new

MySqlConnection(ConfigurationSettings.AppSettings [" connString"]);


MySqlCommand myCommand = new MySqlCommand(sql,myConn);

myConn.Open();


MySqlDataReader dr = myCommand.ExecuteReader();


if(dr.HasRows)

{

while(dr.Read())

{

if(dr [" thisid"]。ToString()。Length> 0)

{

idlist.Append("''" + dr [" thisid"]。ToString()+"'',") ; $ / b $ b $

}

int thislength = idlist.ToString()。长度;

if(thislength> 0)

{

返回idlist.ToString()。子串(0,thislength);

}

其他

{

返回" 0&quot ;;

}

if(myConn!= null){myConn.Close(); myConn.Dispose();}


}


public string [] getFilingIDs()

{

StringBuilder ID = new StringBuilder();

string sql =" select filingid from filings where filing_type = 1 and

parseid> 0 limit 100&quot ;;

MySqlConnection myConn = new

MySqlConnection(ConfigurationSettings.AppSettings [" connString"]);

MySqlCommand myCommand =新的MySqlCommand(sql,myConn);


myConn.Open();


MySqlDataReader dr = myCommand.ExecuteReader();


if(dr.HasRows)

{

while(dr.Read())

{

IDs.Append(dr [" filingid"]。ToString()+",");


}

}

int filingIDLength = filingIDs.ToString()。Length-1;

返回filingIDs.ToString()。子串(0,filingIDLength).S plit('' ,'');

if(myConn!= null){myConn.Close(); myConn.Dispose();}

}

===========================

Andrew Robinson写道:
种类一个不完整的代码片段,但是在你处理它之前关闭你的连接怎么样?



Mayb这些联系正在汇集。


" Brent" <"" b b i g l e r \@ y a h o o。 c o m>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
我快跑了超出可用的数据库连接与下面的代码。我想,当我继续关注时,我想要关闭所有内容,但是当方法getIDs发挥作用时,我会立即看到
连接。
有没有人看到任何明显的东西?这几乎就像我的代码不能像打开它一样快地关闭数据库连接......

感谢您的帮助!

--Brent

================================
代码片段
================================
//创建数组字符串[] id后

for(int i = 0; i< 50; i ++)
{
string theseids = getIDs(Int32.Parse(ids [i]));

if(theseids!=" 0")
{
Response.Write(cusips.ToString());
}

}

公共字符串getIDs(int id)
{String> StringBuilder ids = new StringBuilder();

//设置sql连接& sql语句

myConn.Open(); //打开连接
//设置datareader并循环遍历行,
//附加到每个
//循环的StringBuilder

如果(ids.ToString()。长度> 0)
{
返回ids.ToString();
}
其他
{
返回" 0"
}
如果(myConn!= null){myConn.Dispose();}

}



I''m quickly running out of available db connections with the code below.
I like to think I''m closing everything as I go along, but I watch the
connections shoot up as soon as the the method getIDs comes into play.
Does anyone see anything obvious? It''s almost as if my code can''t close
a DB connection as fast as it can open it...

Thanks for any help!

--Brent
================================
Code Fragment
================================
//after creating array string[] ids

for(int i = 0;i < 50;i++)
{
string theseids = getIDs(Int32.Parse(ids[i]));

if (theseids != "0")
{
Response.Write(cusips.ToString());
}

}

public string getIDs(int id)
{
StringBuilder ids = new StringBuilder();

//Set up sql connection & sql statement

myConn.Open(); //open connection

//set up datareader and loop thru rows,
//appending to the StringBuilder at each
//loop

if (ids.ToString().Length > 0)
{
return ids.ToString();
}
else
{
return "0";
}

if(myConn != null){myConn.Dispose();}

}

Kind of an incomplete code snippet, but how about closing your connection
before you dispose it?

--

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org
"Brent" <""b b i g l e r \"@ y a h o o . c o m"> wrote in message
news:11*************@corp.supernews.com...
I''m quickly running out of available db connections with the code below. I
like to think I''m closing everything as I go along, but I watch the
connections shoot up as soon as the the method getIDs comes into play.
Does anyone see anything obvious? It''s almost as if my code can''t close a
DB connection as fast as it can open it...

Thanks for any help!

--Brent
================================
Code Fragment
================================
//after creating array string[] ids

for(int i = 0;i < 50;i++)
{
string theseids = getIDs(Int32.Parse(ids[i]));

if (theseids != "0")
{
Response.Write(cusips.ToString());
}

}

public string getIDs(int id)
{
StringBuilder ids = new StringBuilder();

//Set up sql connection & sql statement

myConn.Open(); //open connection

//set up datareader and loop thru rows,
//appending to the StringBuilder at each
//loop

if (ids.ToString().Length > 0)
{
return ids.ToString();
}
else
{
return "0";
}

if(myConn != null){myConn.Dispose();}

}



Hmmm...tried that, with no luck...

The full code, which isn''t much more, is here:

================================================== =
public void writeIDs()
{

StringBuilder sbsql = new StringBuilder();

//get array of ids we want to look at.
string[] filingIDs = getFilingIDs();

for(int i = 0;i < 50;i++)
{
string theseids = getIDs(Int32.Parse(filingIDs[i]));
if (theseids!= "0")
{
output(theseids.ToString());
}

}

}
public string getIDs(int filingid)
{
StringBuilder idlist = new StringBuilder();
string sql = "select thisid from holdings where thisid = "+ filingid;

MySqlConnection myConn;
myConn = new
MySqlConnection(ConfigurationSettings.AppSettings["connString"]);

MySqlCommand myCommand = new MySqlCommand(sql, myConn);

myConn.Open();

MySqlDataReader dr = myCommand.ExecuteReader();

if (dr.HasRows)
{
while (dr.Read())
{
if (dr["thisid"].ToString().Length > 0)
{
idlist.Append("''" + dr["thisid"].ToString()+"'',");
}
}
}
int thislength= idlist.ToString().Length;
if (thislength> 0)
{
return idlist.ToString().Substring(0,thislength);
}
else
{
return "0";
}
if(myConn != null){myConn.Close();myConn.Dispose();}

}

public string[] getFilingIDs()
{
StringBuilder IDs = new StringBuilder();
string sql = "select filingid from filings where filing_type = 1 and
parseid > 0 limit 100 ";
MySqlConnection myConn = new
MySqlConnection(ConfigurationSettings.AppSettings["connString"]);
MySqlCommand myCommand = new MySqlCommand(sql, myConn);

myConn.Open();

MySqlDataReader dr = myCommand.ExecuteReader();

if (dr.HasRows)
{
while (dr.Read())
{
IDs.Append(dr["filingid"].ToString()+",");

}
}
int filingIDLength = filingIDs.ToString().Length-1;
return filingIDs.ToString().Substring(0,filingIDLength).S plit('','');
if(myConn != null){myConn.Close();myConn.Dispose();}
}
===========================
Andrew Robinson wrote:
Kind of an incomplete code snippet, but how about closing your connection
before you dispose it?



Maybe the connections are pooling.

"Brent" <""b b i g l e r \"@ y a h o o . c o m"> wrote in message
news:11*************@corp.supernews.com...
I''m quickly running out of available db connections with the code below. I
like to think I''m closing everything as I go along, but I watch the
connections shoot up as soon as the the method getIDs comes into play.
Does anyone see anything obvious? It''s almost as if my code can''t close a
DB connection as fast as it can open it...

Thanks for any help!

--Brent
================================
Code Fragment
================================
//after creating array string[] ids

for(int i = 0;i < 50;i++)
{
string theseids = getIDs(Int32.Parse(ids[i]));

if (theseids != "0")
{
Response.Write(cusips.ToString());
}

}

public string getIDs(int id)
{
StringBuilder ids = new StringBuilder();

//Set up sql connection & sql statement

myConn.Open(); //open connection

//set up datareader and loop thru rows,
//appending to the StringBuilder at each
//loop

if (ids.ToString().Length > 0)
{
return ids.ToString();
}
else
{
return "0";
}

if(myConn != null){myConn.Dispose();}

}