Unclosed connection in pqsql

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
1/28/2008 8:33:03 AM
Gravatar
Total Posts 488

Unclosed connection in pqsql

 Joe,

my custom code failed, so I debugged the problem.

There is the problem with this function in NpgsqlHelper.cs:

public static NpgsqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params NpgsqlParameter[] commandParameters)

It opens connection, bu does not close it. So when called many times the connection limit is exceeded.

(ExecuteReader does not close the connection as it did not open it).
 

1/28/2008 8:40:20 AM
Gravatar
Total Posts 18439

Re: Unclosed connection in pqsql

No you must close the connection on the reader by using reader.Close(); after you use it. This will close the connection.

Joe

1/28/2008 9:37:24 AM
Gravatar
Total Posts 488

Re: Unclosed connection in pqsql

Ok, ExecuteReader opens the connection, but does not close it. I suppose this normal as it returns the reader.

Are you sure that reader.Close() closes the connection?

I have written some "run once" custom code that adds about 20000 users to the database, but it throws an exception after about 1000 are added. Reconfiguring postgres for more connections solved the problem, so I suppose some connections not to be closed in time.

 

 

1/28/2008 9:52:33 AM
Gravatar
Total Posts 18439

Re: Unclosed connection in pqsql

Yes, the connection must be open for reader to read. If you use the overlaod of ExecuteReader that takes a connection string then all you have to do is reader.Close(); and it will close the connection.

If you use the overload that takes a connection, then it is up to you to call connection.Close() after calling reader.Close() since you created the connection yourself.

So I almost always use the connectionstring overload as this is the safest easiest to use, all I have to remember is to close the reader.

The only time to use the overlaod that takes a connection is if you want to use the same connection for several tasks in a row before closing it.

Joe

1/28/2008 10:06:54 AM
Gravatar
Total Posts 488

Re: Unclosed connection in pqsql

Something strange with it.

Everything seems to be ok with the connections, but it still failed.

Anyway, thanks for your help.

1/28/2008 10:18:16 AM
Gravatar
Total Posts 18439

Re: Unclosed connection in pqsql

How are you creating the users? Are they mojoportal SiteUser objects being created in rapid succession or some custom objects?

It might be better to call the data code directly for this run once code and that would use ExecuteNonQuery instead of ExecuteReader. The only thing I can think of if you are using ExecuteReader is must be creating a lot of objects like SiteUser that use reader internally to populate the instance. If any of those are failing to close the reader it can leave the connection open. Other than that maybe there is some slight delay between reader.Close() and the time when the conneciton becomes available again in the connection pool. If it fails at 1000 records maybe let the thread sleep a few seconds every 500 iteration or so to allow a little time for the connection pool to recover resources.

Hope it helps,

Joe

1/28/2008 11:09:08 AM
Gravatar
Total Posts 488

Re: Unclosed connection in pqsql

For users creation I use the code just copied from the page that creates new users in the portal itself, so it uses api functions (SiteUser object).

I have tried to add sleep(), if id will not success I will use the data layer directly. Thanks.

You must sign in to post in the forums. This thread is closed to new posts.