wondering if re-using adapter is right thing to do?

Posts   
 
    
pkellner avatar
pkellner
User
Posts: 19
Joined: 23-Aug-2009
# Posted on: 27-Aug-2009 04:22:02   

I'm wondering if it makes sense to try and open just one DataAccessAdapter per thread. That is, I create my own data manager classes that each are executed by using the standard singleton pattern. That is, I execute these static classes like:

CodeTestManager.I.Get(...);

They are all [ThreadStatic] so I'm confident I won't have any threading issues since a new instance is created on each thread automatically for me.

So my question is, if I create one adapter (rather than using the "using" syntax, is that a recommended way to go? Or, is creating an adapter on every page invocation of a manager class OK. My code is below.

    public abstract class ManagerBase<TManager, TResult, TEntity> where TEntity : class, new()
        where TManager : class, new()
        where TResult : ResultBase
    {
        protected static LinqMetaData _meta;

        private static readonly object SingletonLock = new object();
        [ThreadStatic]
        private static TManager _instance;
        public static TManager I
        {
            get
            {
                if (_instance == null)
                {
                    lock (SingletonLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new TManager();

                            //using (var adapter = new DataAccessAdapter())
                            {
                                var adapter = new DataAccessAdapter();
                                adapter.ConnectionString = "Data Source=.;Initial Catalog=3plogic ;Persist Security Info=True;User ID=3plogic ;Password=xx";
                                _meta = new LinqMetaData(adapter);
                            }
                        }
                    }
                }
                return _instance;
            }
        }

I know .dispose is never called, but the idea is this adapter lives for the life of the applicaiton.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Aug-2009 06:24:08   

You can use both ways. I prefer the "using (..." approach. These are related threads about that subject: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=14695 http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7331

David Elizondo | LLBLGen Support Team
pkellner avatar
pkellner
User
Posts: 19
Joined: 23-Aug-2009
# Posted on: 27-Aug-2009 06:31:58   

Thanks, I don't see anything about perf implications on those threads. is it a worry to keep opening and closing? Does it cause any database interaction to do that?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-Aug-2009 11:06:22   

is it a worry to keep opening and closing? Does it cause any database interaction to do that?

When you open a connection you actually grab a connection from the connection pool. Not much effect on the performance, although it's generally recommended to use the same connection for multiple consecutive transaction as much as you can.