Best practices

Hi,

I was wondering I am writing this Windows Service Class. My class has a Timer and when ever the Elapsed Event is raised I want to go to a Database and see if there are any new records for me to process. Now what I started to think about is which is more "Correct" way of doing things.

I have an OleDbConnection called myConnection.

Is is "correct" to have one global object "myConnection" and just make sure the State is Closed when ever I exit my function or should I create a new instance of the OleDbConnection class everytime the event is raised

Then I ask you would the same pattern apply to a Web Service class where many people can be accessing the Web Service at the same time Or is it "more correct" then to create a new instance of my OleDbConnection object when ever a Web Service Function is accessed

Thank you for your time.


Answer this question

Best practices

  • Paolo Pialorsi

    Connection instances cannot be "shared". Each manages its own state and requires its own dedicated channel to the server (which maintains state for each connection). Sure, it's correct to create a new instance for each invocation of the service. Be sure to open the connection just in time and close the connection when it's not being used.

    hth



  • Best practices