Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2007, 07:46 AM
Registered User
 
Join Date: Oct 2007
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default provider class singleton not thread safe

The singleton pattern is used in the provider classes to return an instance of the class.

This implementation of singleton does not seem to be thread safe. Is this taken care of in another way by ASP.NET or is it just not thread safe?


For an example of a thread safe singleton in C# see http://www.yoda.arachsys.com/csharp/singleton.html

 
Old October 2nd, 2007, 03:00 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

trystanc - that's a crackin good read. i really will take this to heart and try to benchmark/expose it's use within tbh.

verging on excellent 'foresight'....

jimi

http://www.originaltalent.com
 
Old October 3rd, 2007, 11:39 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

trystanc - had a play (haven't measured anything yet). here's how my ForumsProvider 'intro' looks now:

namespace MB.TheBeerHouse.DAL
{
    public abstract class ForumsProvider : DataAccess
    {
        static private readonly ForumsProvider _instance = (ForumsProvider)Activator.CreateInstance(
                    Type.GetType(Globals.Settings.Forums.ProviderType) );

        static ForumsProvider() { }

        static public ForumsProvider Instance
        {
            get
            {
                return _instance;
            }
        }

        public ForumsProvider()
        {
            this.ConnectionString = Globals.Settings.Forums.ConnectionString;
            this.EnableCaching = Globals.Settings.Forums.EnableCaching;
            this.CacheDuration = Globals.Settings.Forums.CacheDuration;
        }
        // other code below unchanged

certainly appears to follow the pattern from the article you quoted. any comments re the code above??

jimi

http://www.originaltalent.com
 
Old October 4th, 2007, 03:49 AM
Registered User
 
Join Date: Oct 2007
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi jimi,

That looks like it will do the trick.

The thing about thread safety is that 99.9% of the time the unsafe code will work fine as the chances of two threads colliding are very small.

Basically two page requests would have to come in at exactly the same time for the same resource (and be the first request for the resource since the app restarted so the instance variable is not set) for threads to collide. This is very unlikely, especially if the site doesn't have high traffic.

But the risk is always there...

 
Old October 17th, 2007, 08:01 PM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I understand the risk, but choosing between grab the instance or creating a new instance everytime... it's hard

The jimi version minimize the risk but is it faster than the older one?

I think that processor burn more resources if he create a new activor.createinstance everytime the client call ForumsProvider Class, or not?



 
Old October 18th, 2007, 03:52 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

max,

as it's defined as static, it'll only be initilised on the 1st invocation, so won't create a new resource each time. i wondered this too, so implemented the change debugged the code and found that it only get's created the 1st time around.

so my 'take' on this is that this new method is both 100% safer and doesn't suffer any decrease in efficiency or performance.

jimi

http://www.originaltalent.com
 
Old October 18th, 2007, 05:05 AM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ummmm OK!

Then I guess i need to implement that! :)

 
Old October 25th, 2007, 12:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Atlanta, Georgia, USA.
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've used code like this in some applications, got it from the same web page.

I only found one case where it couldn't work and I didn't take the time to figure out why. I just used code like Marco's with a lock verb in that case. That was a pretty deep architecture that was heavily interface driven. The code wasn't unreliable, it just plain didn't work in that case.

Eric





Similar Threads
Thread Thread Starter Forum Replies Last Post
Share a non-remotable singleton object lok_tan .NET Framework 2.0 4 March 17th, 2008 04:00 AM
md5() safe??? foddie PHP How-To 0 December 30th, 2005 10:25 PM
Safe For Scripting interrupt VBScript 0 September 2nd, 2004 07:25 AM
Singleton Usage in Clustering and EJB argupta BOOK: Expert One-on-One J2EE Design and Development 0 December 17th, 2003 12:44 AM
Thread safe ejbs Indian Ocean J2EE 0 September 20th, 2003 03:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.