Please also have a look in our OnlineHelp for further information.
In some situations it might be useful, to use a so called connection pool. This means that several processes or threads are using a set of SAP connections together, e.g. in a web application: 30 users work with an application but there are only 10 concurrent connections to SAP. Everytime an application process needs a connection a free connection is allocated by the pool. After having used the connection it is freed by the process and can now be used by another one.
Before we start
Before we start with the pool class we need to extend the R3Connection class by inheriting it.
We will need the two new properties LastUsage and IsInUse later on.
The class ConnectionPool
The following text describes the methods of the connection pool class.
In the constructor of the class a timer is initialized. The timer is responsible for closing connections that are not used for a certain period of time.
The generic list MyConnectionList holds all active connections. When the last usage was more than 60 seconds ago and it is not currently in use, the connection is closed and removed from the list.
The two internal private functions AllocConnection() and FreeConnection() are for allocating and deallocating connections. If there is no free connection available a new connection is created and added to the connection list.
Without the pool you would call CreateFunction() directly (e.g. MyConnection.CreateFunction(..)). If you are using the new pool class the CreateFunction() method is called by the pool after having allocated a connection dynamically. The RFCFunction object is cashed with the help of XML serialization and deserialization. This avoids retrieving the function’s meta data from SAP every time CreateFunction() is called.
The execution of the function uses the same principle as CreateFunction(): Allocate connection, execut e, deallocate.
Test the connection pool
The following console program shows how to test and apply the connection pool class.
First 3 separate threads are started. After pressing Enter 3 more threads are startetd. The timer shows the current number of active connections.
Depending on how many threads have already finished after the new ones have been started, the connections are recycled or newly connected.
The output shows:
3 threads are started
2 have finished
3 news threads are started
Result: 4 active SAP connections, because after 2 have finished only 1 more is needed.