Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,I saw an article in the http://www.yoda.arachsys.com/csharp/singleton.html, called Implementing the Singleton Pattern in C #, I write a DEMO use 5th version, code as follows:
public sealed class Singleton
{
    Singleton()
    {
       InitSessionFactory();
    }

    private void InitSessionFactory()
    {
       //connection to DB...
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }

    public ISession GetSession()
    {
        return sessionFactory.OpenSession();
    }

    private ISessionFactory sessionFactory;
}


Usually it works well, but I found a problem when Instance occurs an exception, such as network disconnection, the result is exception when reading Instance, however,when the network reconnected, read the Instance again, the result is still exception , How can I recovery Instance?

Significance of this problem was, when the network happen connection problems, I need close program then restart or no.

Thanks.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Thanks Jimmanuel.

if network disconnection at programming start, the programming exception is :
'Instance' threw an exception of type 'System.TypeInitializationException'

{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}


then, network is connection, the exception still:
'Instance' threw an exception of type 'System.TypeInitializationException'

{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}


if network disconnection at programming already running, the programming exception is:
A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - Remote host forced to close an existing connection。)


even network is reconnection.
Posted
Updated 6-May-10 22:46pm
v2
Comments
Jimmanuel 6-May-10 14:11pm    
Edditting your post to include what kind of exception is being thrown might help get an answer to your question.

I'm guessing that you're being bitten by the constructor throwing an exception when the network connection is down on startup (google TypeInitializationException). If new Singleton() throws then what does static readonly Singleton instance get set to? Is the object in a valid state or not? Maybe an expert here can chime in with what exactly happens in that case.

As a shot in the dark solution, I'd try moving the call to InitSessionFactory(); into a separate method that isn't called via the class' constructor. Then I'd be prepared for an exception when performing any method call from the one instance, and when that happens maybe attempt to re-initialize the object.
 
Share this answer
 
Comments
yuzifu 7-May-10 15:04pm    
Thank you, I think I understand a little bit, I would carefully consider.
yuzifu 7-May-10 23:02pm    
Yes, a part of exception has fix, when i moving InitSessionFactory() into GetSession().
Next, I test the remaining exception.
 
Share this answer
 
Comments
yuzifu 7-May-10 11:06am    
Thank you for your reply, But you do not understand what I mean.

when network is reconnection, Instance Should not be exception, As I re-start programming, it's work well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900