Using DatabaseFactory.CreateDatabase()

Topics: Data Access Application Block
Feb 17, 2009 at 12:31 AM
I'd like to ensure that I'm using CreateDatabase correctly - how should this call be used in a regular application?
i.e. I have a number of forms, each calling their own DatabaseFactory.CreateDatabase(), being run by multiple users - does this result in one or many connections being opened? Should I be calling CreateDatabase ONCE when the app starts and then caching the returned "Database" object and supplying that to the forms when they require it?

Feb 17, 2009 at 1:13 AM
Connections are not being opened when you call DatabaseFactory.CreateDatabase().  When using DAAB, connections can be automatically created and opened for you even if you did not explicitly create codes for it.  For example, calling on Database.ExecuteDataset(.....) automatically handles the association of an existing connection or creating a new one. 

"By design, most of the Database class methods handle the opening and closing of connections to the database on each call. Therefore, the application code does not need to include code for managing connections. (By default, and for performance reasons, ADO.NET returns connections to the connection pool without closing them. Therefore, you do not need to cache your Database objects.)" - taken from the documentation, Data Access Application Block -> Developing Applications Using the Data Access Application Block -> Managing Connections.

You might also ask the same question along the way as with the guys in this thread -

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.