Database connection management EntLib 5.0

Topics: Data Access Application Block
Oct 25, 2012 at 8:24 AM

Below is a code example of how we create our database connections. Every now and then we get max pool size reached error. Is it somehow possible that in some cases database connection doesn't close properly though we use using block in all our db connections.

Code example:

public class InternalDB
{
    private static readonly SqlDatabase database;

	static InternalDB()
	{
		try
		{
			if (database == null)
			{
				database = EnterpriseLibraryContainer.Current.GetInstance<Database>(DefaultDatabaseName) as SqlDatabase;
			}
	.
	.
	.
      
    private static EntityRelationshipStorage GetRelationships()
    {
		using (DbCommand aDbCommand = database.GetStoredProcCommand("dbo.GetRelationships"))
		{
			using (IDataReader aDataReader = database.ExecuteReader(aDbCommand))
			{
				aResult = new EntityRelationshipStorage();

				// read data from DB
				while (aDataReader.Read())
				{
					var aSID = (int) aDataReader["s_id"];
					var aPID = (int) aDataReader["p_id"];

					aResult.AddRelationship(aSID, aPID);
				}

				cache.Add(EntityRelationshipStorageCacheKey, aResult, CacheItemPriority.Normal, null,
						  new AbsoluteTime(DateTime.Now.AddMinutes(Parameters.CacheItemExpirationTime)));
			}
		}
	.
	.
	.

-Jukka

 

Oct 26, 2012 at 6:23 AM

The posted code looks OK to me.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com