Enterprise Data Access Application Block - Help/Advice request

M

maxxx

I'm using SQL server and the Enterprise Library Data Access
Application Block using C# in a multi-tier environment.

I understand I need to have connections closed so that the connection
pool can reuse them - but my question is:

Should I (or should I not) keep a reference to the Database object
returned from the DatabaseFactory.CreateDatabase() method?

My database classes are static, and my intention was to have a private
static variable of type
Microsoft.Practices.EnterpriseLibrary.Data.Database, with a static
property which instantiates the Database object when needed.

private static Database _database;

private static Database DB
{
Get
{
If (_database == null)
{
_database = DatabaseFactory.reateDatabase("myconnection");
}
Return _database;
}
}


This would, I thought, improve performance as the object does not need
to be instantiated each time it is used.

What I do not know, though, is if the Database object needs to be
destroyed in order for it to close its connection, or if the
connection will be closed regardless (e.g. after closing a datareader
or getting a dataset).

So,,, is my idea a good one or a bad one- and importantly why?

Thanks for any advice.

..\\axxx
 
N

Nicholas Paldino [.NET/C# MVP]

maxxx,

It doesn't really matter. The Database class doesn't keep a connection
open across operations. It basically opens the connections for you,
performs the operations, and then closes them.

It ^is^ useful to keep a single instance of the Database class around
though, as it will cache stored procedure parameters. If you created a new
Database instance every time, and you used the stored procedure methods
which performed parameter discovery from the server, it would make that call
every time.

Of course, the enterprise libraries come with the source, so you should
feel free to look around in the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top