4 Multi-thread application: Database connection Pool __ Database

Source: Internet
Author: User
Tags garbage collection time interval
4 Multi-thread application: Database connection PoolCategory: Reading notes 2009-11-09 15:46 747 people read Comments (0) Favorites Report database connection pool Multithreading object Database exception string

First of all: this example is from the "C # Thread reference manual," a reference to an example, in this I just want to study her design principles.

The specific use is not good, because no specific project testing, so remind everyone attention.

1 Design ideas:

1.1 Establish a database connection pool object in the program's global use of a single case pattern.

1.2 Get the database connection method Borrowdbconnection ().

The function of this method is as follows:

If the DB connection object in the pool to be cleaned is not turned off, it is added to DB connection pool for continued use.

If the DB connection object in the pending pool is turned off, the DB connection is removed from the pool to be cleaned and then closed.

If a DB connection object is not present in the pool to be cleaned, after the DB connection object is created, attach to the DB connection pool, and then use her.

1.3 Returns the database connection to the pool to be cleaned by Returnobjecttopool ().

DB connection pool deletes the DB connection object.

The pool to be cleaned appends the DB connection object.

1.4 Garbage collection events that delay DB connection objects

The timer's elapsed event is used to implement shutdown processing of the DB connection object in the pool to be cleaned.

2 UML Legend:

Here to explain, why to inherit the Objectpool class.

Because the structure of the Objectpool class is common to all objects that use pools, which can manually release resources.

3 The specific code is as follows:

3.1 Objectpool class (really generic a class that is well designed and feels like it can inherit her to do anything)

[C-sharp]  View plain copy using system;   using system.collections;   using  system.timers;   using system.text;      namespace sqlpool   {        public abstract class ObjectPool        {           /// <summary>           /// //The current date time scale value, the user determines whether the connection timed out             /// </summary>            private long _lastCheckOut;           ///  <summary>           ///  using object pooling            /// </summary>            private static hashtable locked;             /// <summary>           ///   Cleanup Object Pool            /// </summary>           private static Hashtable unlocked;           internal static long garbage_interval =  5 * 1000;              /// < summary>           ///  Constructor 1, which shows that two pools are synchronized.            /// </summary>            static objectpool ()             {                locked = hashtable.synchronized ( New hashtable ());                Unlocked = hashtable.synchronized (New hashtable ());            }              /// < summary>           ///  initialization interval, time interval triggering event, freeing object pool cleanup objects            /// </summary>            internal objectpool ()             {               _lastcheckout  = DateTime.Now.Ticks;                system.timeRs. Timer atimer = new system.timers.timer ();  

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.