C # Connection Pool development, multi-connection efficient application development, multi-connection automatic maintenance management.

Source: Internet
Author: User
Tags connection pooling server port

This article will use a GitHub open Source component library technology to implement connection pooling and apply to frequent network connection operations in some cases.

GitHub Address: Https://github.com/dathlin/HslCommunication If you like star or fork, you can also reward support, please look for the source code project.

This project currently supports the C # language and Java language, the C # language is more complete, the Java version of the library is still in development and improvement.

NuGet Address: https://www.nuget.org/packages/HslCommunication/

GitHub Address: Https://github.com/dathlin/HslCommunication If you like star or fork, you can also enjoy support.

Technical Support QQ Group: 592132877 (the component version update details will be released in the group first) Last modified: June 13, 2018 08:28:34

Inside all kinds of small partners, for you to answer data interaction, programming skills, if you have any questions about the API provided by this interface, you can add group consultation, if there are better suggestions, welcome to put forward.

The complete information and API description of the component refer to: http://www.cnblogs.com/dathlin/p/7703805.html component usage limits, update logs, all within this page.

Why connection pooling is required

First of all, we need to solve this problem, why we need to connect pool, the ultimate goal is to efficient data access, but not all the circumstances need to connect pool building, to mention a few?? It

Case one: You have a background thread every 1s clock reading the data of the PLC, you and the PLC interaction in this thread, then completely do not need to connect pool.

Case two: You will be in the program's multiple threads (including the thread pool inside) and the PLC for data interaction, frequent read and write operations, if the performance of a common connection is not up to the requirements how to do (usually under the wireless network, a data interaction between 30ms-100ms, network fluctuations)? , if each operation is to create a connection, the end of the operation is closed, this can also achieve the function, but the loss performance is more serious, whether it is the PLC or the machine, communication efficiency is not ideal, because each operation is reconnected and closed, the PLC can actually replace the database, Redis, Any other data communication.

When you cannot connect to a pool

Not all cases can use the connection pool, there is a huge limit, if the server does not support multi-connection is very troublesome, such as the Mitsubishi PLC server port, of course, the general database, Redis, special server are supported. Of course, some special servers, the number of supported connections is capped, nothing, this component can also configure the maximum number of connections.

Feature Support

At the beginning of the design of this functional class, it is the flexibility, in order to support all other different types of data communication, using interface + generic to achieve, first build a communication package class, then create a connection pool manager, and then call to use.

    1. Support for configuring the maximum number of connections
    2. Support Setting connection Expiration time
    3. Supports any other type of connection object into a connection pool

Practical examples

Here is an example of the Siemens PLC, so the first step is to create a connected encapsulation class, in addition to implementing the interface, you need to define the actual connection object.

    public class SiemensConnector:HslCommunication.Algorithms.ConnectPool.IConnector {#region construction method Pub  Lic Siemensconnector (String ipAddress) {siemens = new HslCommunication.Profinet.Siemens.SiemensS7Net (        HslCommunication.Profinet.Siemens.SiemensPLCS.S1200, ipAddress); } #endregion #region Iconnector implementation///<summary>//Indicates whether the current connection is in use///</summa        Ry> public bool Isconnectusing {get; set;}        <summary>////Unique GUID code///</summary> public string Guidtoken {get; set;}        <summary>////Last used time///</summary> public DateTime lastusetime {get; set;} <summary>///Open connection///</summary> public void Open () {//Set up regular connections Pick up. If it is Redis, you can connect to the server and the database is the same as Siemens.        ConnectServer (); }//<summary>//Close and release//&LT;/SUmmary> public void Close () {//close connection Siemens.        Connectclose (); } #endregion #region Public Member//<summary>///////////To get current connection objects for easy data interaction//</ summary>//<returns></returns> public HslCommunication.Profinet.Siemens.SiemensS7Net Getsie        Mens () {return Siemens;        } #endregion #region Private Member private HslCommunication.Profinet.Siemens.SiemensS7Net Siemens; Connection Object #endregion}

Once defined, you can create a real connection pool object.

Private hslcommunication.algorithms.connectpool.connectpool<siemensconnector> siemensConnect = null;           Connection pool for Siemens objects

  

Then initialize the variable

Siemensconnect = new Hslcommunication.algorithms.connectpool.connectpool<siemensconnector> (() = {return New Siemensconnector ("192.168.1.195"); } );            Siemensconnect.maxconnector = ten;         The maximum number of simultaneous connections            siemensconnect.conectionexpiretime =;  How long the connection does not have to be automatically recovered, per second

When initializing, there is a place to be aware that the connection pool needs to know a message, how to create a new connection object, where it is possible to create a default Siemesconnector class, but the connection pool of the management object is likely to be an interface, You will need to manually indicate how to instantiate a new object at this time. This is the case when I use the dapper ORM, I have encountered the support of MySQL and SQL Server two kinds of data.

Call the Connection object class, and the following code can appear in any of the background threads:

            The code here has little effect in the case of single-threaded programs, but can significantly improve performance in multithreaded situations.            ///For example, here to access a PLC data            Siemensconnector connector = Siemensconnect.getavailableconnector ();            Short m100 = connector. Getsiemens (). ReadInt16 ("M100"). Content;            Return the connection            Siemensconnect.returnconnector (connector) after the use is complete;

  

Be sure to return the connection object after use, if you want to get the number of connections that have been activated in the connection pool

int online = Siemensconnect.usedconnector;

  

Also, it is important that you do not throw an exception after getting the connection object,

C # Connection Pool development, multi-connection efficient application development, multi-connection automatic maintenance management.

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.