publicclass ConnectionManager
{
private Hashtable _cache = new Hashtable();
privatestatic ConnectionManager _instance = null;
private ConnectionManager()
{
}
publicstatic ConnectionManager Instance
{
get
{
if( _instance == null )
_instance = new ConnectionManager();
return _instance;
}
}
public IDbConnection GetConnection()
{
IDbConnection cnn = null;
if(_cache.Contains(Thread.CurrentThread))
{
cnn = _cache[Thread.CurrentThread] as IDbConnection;
}
else
{
cnn = CreateConnection();
}
_cache[Thread.CurrentThread] = cnn;
if( cnn.State == ConnectionState.Open)
cnn.Open();
return cnn;
}
publicvoid FreeConnection()
{
IDbConnection cnn = _cache[Thread.CurrentThread] as IDbConnection;
if(cnn != null)
{
cnn.Close();
// if neeeded
//_cache.Remove(Thread.CurrentThread);
//cnn.Dispose();
}
}
private IDbConnection CreateConnection()
{
//return new SqlConnection("");
returnnull;
}
}