Blog Home  Home Feed your aggregator (RSS 2.0)  
Implements IVillage - Monday, April 09, 2007
It takes a village to keep up with .Net
 
 Monday, April 09, 2007

I was implementing some data access code for a personal project using the Enterprise Library's Data Access Application Block (DAAB).  I was running some code that checked for the existence of a row prior to issuing an insert.  Here is the original code:

SqlDatabase db = DatabaseFactory.CreateDatabase("mainDB") as SqlDatabase;

string sqlCommand = "ar_GetArrestReportItemByCaseNumber";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(dbCommand, "reportId", DbType.Int32, reportID);
db.AddInParameter(dbCommand, "caseNumber", DbType.String, caseNumber);

IDataReader dataReader = db.ExecuteReader(dbCommand);

exists = dataReader.Read();

After running through on some test data, I gave it a run against some real world data.  And it worked fine until the data's batches exceeded 100 rows.  Then I got the error 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.

I checked SQL Manager and saw 100 connections / processes accessing the table in question.  I am using the DAAB.  The Database class is supposed to take care of this sort of thing for me.  There was a bug in VS 2003, but this was fixed in VS 2005.  Where is the connection being left open... the DataReader.  I forgot to close the DataReader.  I promptly re-educated myself on some ADO and IDisposable.  Whenever an object implements IDisposable, there is probably a good reason for it... like say... releasing a connection object back to the connection pool.  Corrected code and much better performance:

SqlDatabase db = DatabaseFactory.CreateDatabase("mainDB") as SqlDatabase;

string sqlCommand = "ar_GetArrestReportItemByCaseNumber";
using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
{
   db.AddInParameter(dbCommand, "reportId", DbType.Int32, reportID);
   db.AddInParameter(dbCommand, "caseNumber", DbType.String, caseNumber);

   using (IDataReader dataReader = db.ExecuteReader(dbCommand))
   {
      exists = dataReader.Read();
   }
}

Moral of the story... if IDisposable is implemented, chances are you should call it.

ADDENDUM
----------
For those who are not familiar, IDisposable is an interface for the implementation of the Dispose Pattern.  This is very useful in programming languages that have Garbage Collection (ie. .Net).  When you have Garbage Collection, you have what is called a non-deterministic destructor.  In other words, when you are done using an object, you have no idea when it's resources will be freed.  Garbage Collection occurs at an interval that is determined by the system.  By implementing IDisposable, you ad a Dispose method to your class where high contention resources can be freed (ie. File Handles, Database Connections, etc.).  The implementing of IDisposable signals the user that there is something valuable to be cleaned up ASAP that cannot wait for Garbage Collection.  So, even if Dispose is called, the Garbage Collector will still clean up the object's resources eventually.  But in the mean time, all of the high value resources have been released.

Monday, April 09, 2007 10:49:49 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]    | 
 Wednesday, April 04, 2007

I wanted to share this bit of SQL wisdom I picked up a few months ago on the ASP.Net forums (http://forums.asp.net). Let's say you have an orders table and you have an orderDate column that contains the Date & Time of the order.  If you wanted to create a query to select all orders made yesterday at any time, I cameup with this working (if not long winded) method:

 Where orderDate = CAST(MONTH(DATEADD(day, - 1, GETDATE())) AS varchar) + '/' + CAST(DAY(DATEADD(day, - 1, GETDATE())) AS varchar) + '/' + CAST(YEAR(DATEADD(day, - 1, GETDATE())) AS varchar))

I was treated to an extreme stream lining of this on the forums.  The resulting query was:

Where orderDate >= convert(Varchar, Getdate() -1, 101) And orderDate < convert(Varchar, Getdate() , 101)
 
The use of convert with the right format number negates a lot of work and is much easier to read.
Wednesday, April 04, 2007 9:32:33 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [1]    | 
 Tuesday, March 27, 2007

Wow.  The General Manager of the Microsoft Developer Division in Melbourne, FL.  It was quite an enjoyable presentation.  Scott typed away at the demo like he still was slugging code with the rest of us in the trenches.  Everybody really appreciated Scott taking the time out of his schedule to come over from DevConnections in Orlando.  Scott arrived just in time because somebody told him would only take 45 minuets to drive the Melbourne-Orlando gauntlet in rush hour.  I wasn't paying attention, but I think he implicated one of the local evangelists.

The presentation covered all things Orcas.  Scott covered lots of topics in a marathon 2 hour presentation.  He enthusiastically  made LINQ dance to the oohs and ahhs of the assembled crowd.  Another big pleaser was a simple demo of the IIS configuration items that moved into the web.config file.  The presentation had a couple of the prerequisite CTP crashes for which Scott took a little good natured ribbing.  When an ASP.Net error page popped up in his browser mid demo, he was jeered - "Hey, I have that page in my program too!"  When demonstrating the ability of IIS7 to track long running/hanging pages, Scott said this might be useful if we knew anybody who wrote software with bugs in it (wink wink). 

It was a record crowd for the user group with strong attendance from ONetUG members.  Looking forward to a good year of speakers.

 

Wednesday, March 28, 2007 5:35:23 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]    | 

Quick tip for those putting alternate user names and passwords.  When you export your bindings file, BizTalk 2006 replaces the password for the user credentials with a VT_NULL inside the <TransportTypeData> element:

&lt;Password vt="1" /&gt;

Be careful, that vt="1" mean NULL, or in other words - no password.  So when you do this:

&lt;Password vt="1"&gt;mysecret&lt;/Password&gt;

You might as well do nothing.  When the file receive adapter imports these bindings, it will not set a password.  However, the send adapter doe snot seem to care and will set the password fine.  Regardless, the proper form is to specify VT_BSTR attribute in the Password element:

&lt;Password vt="8"&gt;mysecret&lt;/Password&gt;

You can find this documented in the Biztalk binding file documentation:

Configuration Property Variable Types

File Adapter Configuration Properties

Tuesday, March 27, 2007 8:25:34 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]    | 
 Monday, March 26, 2007

It was wort the early morning drive to Seminole Community College from Melbourne.  The directions provided on the flyer made it slightly challenging to find but all went well.  The key note from Carl Franklin was a humorous retrospective on his career and the progression of Microsoft Technologies from earlier days.  However due to technical difficulties with his guitar, we were not treated to a live performance (nor was there a live performance from old man Paul later that evening either).

I attended several good talks.  My favorites:

  • Wes Dumey - Great primer on EDW and SSIS.  Looking forward to spending some time with SSIS, this was a good start.
  • Richard Campbell - SQL Tips presentation.  I understand this is one of his regular presentations and I understand why.  Lot's of practical advice and an intro to some of SQL 2005's new features.  Very Technical and very humorous... this is one session that went by in a flash and left me wanting another hour. 
  • Miguel Castro - Great Great presentation on extensibility patterns.  Lots of practical information with backing code samples.  I have never seen Miguel in person and was not disappointed.  Top notch speaker.

Overall, it was a great way to spend a Saturday.  Got to catchup with some developer friends I have not seen in a while.  I also got to make some new friends in the 'There's no more pizza, just wait for the next delivery' forced networking session with my starving co-attendees.  Lot's of thanks to ONetUg for a job well done!

Monday, March 26, 2007 4:17:43 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]    |  |   | 
 Friday, March 23, 2007

Busy next couple of days.  Saturday is a double header of Orlando Code Camp then a birthday party for a co-worker (I am 3 years behind ya Paul!).  Later in the week on Tuesday is Scott Guthrie at Space Coast .Net User Group.

Friday, March 23, 2007 6:50:52 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]    | 
Copyright © 2008 Christian M Loris. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.