Translation: Why singletons are destroyed after five minutes

Source: Internet
Author: User

 The reason why the remote object created in singletons mode will be recycled in 5 minutes

Original article address:Http://www.thinktecture.com/Resources/RemotingFAQ/SINGLETON_IS_DYING.html

Q:

I have a C # class that is being exposed via remoting as a singleton server activated object.

Activated by the serverSingletonType

 

However, sometimes this object just disappears and a new one is created. Shouldn't only a single instance ever exist for a Singleton?

However, sometimes remote objects are destroyed and a new remote object is created. Not to mentionSingletonIs the remote object created only for a single instance?

A:

Remoting uses a so called "lease based" Lifetime services. when you create an object it has a certain time-to-live (normally 5 minutes) upon every methodcall you place from your client onto the object an additional time ("renewoncalltime ", usually 2 minutes) will be added to the object's life time.

RemotingUse the so-called lease-based life cycle management. After a remote object is created, it is scheduled5Default lease period in minutes. This lease period is increased every time a method call from the client is made.2Minutes

 

This is also valid for singletons-the only difference is that the server employs a guarantee, that there either exists exactly one instance or none at all.

When you don't call the server for about 5 minutes, the object will be garbage collected.

SingletonIn this way, the remote object is rented by the server, and only one instance or one does not exist. In5When there is no other access request to the remote object within minutes, the object will beGCReclaim

 

 You can work around this using two schemes:

There are two workarounds::

 

First, you can provide different leases than the default:

First, you can customize the lease period:

 

 

Public   Class Foo: financialbyrefobject
{
Public   Override Object initializelifetimeservice ()
{
Ilease lease = (Ilease) Base . Initializelifetimeservice ();
If (Lease. currentstate = Leasestate. Initial)
{< br> lease. initialleasetime = timespan. fromminutes ( 100 );
lease. sponsorshiptimeout = timespan. fromminutes ( 2 );
lease. renewoncalltime = timespan. fromseconds ( 100 );
}
Return Lease;
}
}

 

This piece of code will ensure that the specified albyrefobject will live for at least 100 minutes.

After the remote object is created100The default period of minutes. (Of course, this lease period will increase with the method call requests during this period .)

 

If you want your Singleton to live forever, you can simply return NULL:

Method 2: LetSingletonRemote Object created in

 

 

Public   Class Foo: financialbyrefobject
{< br> Public override Object initializelifetimeservice ()
{< br> return null ;
}
}

 

 

The second way is to use different pointer sor-object [on the client, the server or somewhere else]. upon running out of lease time. net-remoting framework will call the specified sor of the object an ask it, if it wants to renew or increase the lease time. the specified sor now has 2 minutes (ilease sort sorshiptimeout) to answer this request, else the object will time-out.

In this way, the user (Extends SOR)Object. When the rented remote object expires,RemotingThe infrastructure will ask the leasing user whether to re-lease or increase the lease term. The user has2Response in minutes. After this period, the object cannot be accessed.

 

(in Singleton method, this tenant will be removed from the list of remote object tenants established in the Lease Management period of the server, and the objects may not be recycled.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.