database connection, to close or not to do

H

Han

Hello

I have been using database in server script for years without problem.

connection.open();
do something...
connection.close();

All is fine. Sometimes,

connection.open();
xmlreader xrd = command.executeXmlreader();
connection.close();
return xrd;

All is fine. But not always!
Sometimes I see packet size 4096 error in above scenario. I hope you know
the error. The error comes when result stream size is bigger than default
4096 packet size. You can set the size in connection string. e.g.,

string strcon = "Server=.\\sqlExpress; Database=db1; Integrated
Security=SSPI; Packet Size=32767"

But setting packet size is not ideal way as I heard. Another workaround
suppress the packet size error is let database connection open. e.g.,

xmlreader func() {
connection.open();
xmlreader xrd = command.executeXmlreader();
//connection.close();
return xrd;
}

It works when I do without connection.close(). Now my question comes.

1. Why does above procedure work without close(), and not with close()?

Anyway all is fine if it works. But not that.

When I call the procedure several times I see now connection pool error. I
thought Dotnet database connection is closed automatically when container
procedure ends. My second question is,

2. Is an opened connection not closed even after the container procedure
ends? Should you always close explicitly?

Thanks in advance
 
H

Han

Resolved.

Don't bother with the question. All the problem comes from returning
xmlreader as is. Loading onto Xelement solved all the issues.

xelement func() {
connection.open();
xmlreader xrd = command.executeXmlreader();
xelement1 = xelement.load(xrd);
connection.close();
return xelement1;
}

Thanks for reading anyway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top