0

I have a solution in which an UpdateController class manages the logic for updating data. This controller calls various classes for managing data (ClaimData, StatementData, etc.). What's the best way to share connection across these data handlers--use a singleton, or create another class for managing the connection and passing it to each data handler? What if the application is multithreaded?

Thanks in advance.

areyling
  • 2,181
  • 2
  • 20
  • 25

1 Answers1

1

you could use dependency injection to provide each of these with a connection...

another way is to use an Oracle provider with internal Connection pooling (for example Devart dotconnect, I am only a customer)... then you only share the connection string via dependency injection or configuration file... every class instantiates/releases the connection on its own... the central connection pooling takes care of the rest (reusing connections etc)... this way you don't have to worry about any threading issues regarding connections...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Yes most Data Providers like this or ODAC or built-in SQLServer provider are pooling the connection inside so you should not share the connection. The same as when you are using threads implicitly in C# - the framework takes care. Open, use, close the connection - this is the pattern - inside the framework gets to decide what is really happenning. If youre not using such a framework, passing the connection as a a parameter - dependency injection - is the way to go. Singletons are bad code. – Hanan Jul 21 '11 at 15:17
  • Thanks for the response. I would agree that singletons can be a bad idea, and that's what was suggested to me here to use. Pooling works for an app that uses multithreading, but if you don't need to be thread-safe isn't it more efficient to share the connection? Sadly, I'm not familiar with Dependency Injection--know of any good resources for getting started? Thanks again. – areyling Jul 21 '11 at 15:24
  • if at any time in the future you need threads then a singleton will be a major point to change... as for the efficiency the pooling approach is a bit less efficient in the current scenario but definitely not measurably... so: use the the pooling approach... – Yahia Jul 21 '11 at 15:31
  • as for dependency injection a good starting point is http://stackoverflow.com/questions/21288/which-net-dependency-injection-frameworks-are-worth-looking-into/21348#21348 – Yahia Jul 21 '11 at 15:32