| 1 |
TDS Connection Pooling v0.01 |
| 2 |
|
| 3 |
Introduction |
| 4 |
------------ |
| 5 |
The purpose of the TDS Connection Pool is to share a limited number of actual |
| 6 |
database connections among a larger group of virtual database clients. |
| 7 |
|
| 8 |
The advantages are twofold. First, a dataserver with a limited number of |
| 9 |
licenses can be used with the tdspool program to support a large number of |
| 10 |
client connections. For instance if you have a database with a 10 concurrent |
| 11 |
license limit and 35 Apache/PHP children, which access the database |
| 12 |
infrequently, you could share those 10 real connections among all the apache |
| 13 |
children. |
| 14 |
|
| 15 |
Secondly, TDS Connection Pool acts as a sort of persistant connect for it's |
| 16 |
clients. This works because logins to the connection pool are very quick in |
| 17 |
comparision to logins into the dataserver. Connections to the dataserver are |
| 18 |
maintained for the life of the tdspool program. |
| 19 |
|
| 20 |
Terminology |
| 21 |
----------- |
| 22 |
Pool Server, the tdspool program. |
| 23 |
|
| 24 |
Client, the application using TDS (generally through freetds dblib or ctlib) to |
| 25 |
connect to the Connection Pooling Server. |
| 26 |
|
| 27 |
DataServer, the Sybase or MS SQL Server database. |
| 28 |
|
| 29 |
Pool User, the connection from the client to the Pool Server. |
| 30 |
|
| 31 |
Pool Member, a connection to the dataserver maintained by the tdspool program. |
| 32 |
|
| 33 |
How It Works |
| 34 |
------------ |
| 35 |
|
| 36 |
On start up the Pool Server opens a predefined number of connections (Members) to the DataServer. |
| 37 |
|
| 38 |
It then opens a listening socket on the configured port and goes into its main |
| 39 |
loop. |
| 40 |
|
| 41 |
When a Client connects, the Pool Server accepts the connection, creates a User |
| 42 |
and sets its state to TDS_SRV_LOGIN, and returns to the main loop. |
| 43 |
|
| 44 |
When a Client in the TDS_SRV_LOGIN state sends a login packet, the packet is |
| 45 |
parsed and if the login information matches, a TDS login acknowledgement stream |
| 46 |
is forged by the Pool Server and sent to the Client. The User structure then |
| 47 |
goes to the TDS_SRV_IDLE state. The Pool Server then returns to the main loop. |
| 48 |
|
| 49 |
When the Client in the TDS_SRV_IDLE state sends a query, the member list is |
| 50 |
searched for a Member with its TDS socket in the TDS_COMPLETED state. This |
| 51 |
Member is then allocated to the User. The User is then switched to the |
| 52 |
TDS_SRV_QUERY state. If no free Member is found, the User is placed in the |
| 53 |
TDS_SRV_WAIT state and the search will be reattempted the next time the server |
| 54 |
is woken up by activity (such as a Member completing). When a User is placed |
| 55 |
in a wait, a message is logged to let the administrator know that the number of |
| 56 |
Members may need to be adjusted upwards. |
| 57 |
|
| 58 |
When results are recieved from the DataServer, they are forwarded to the User |
| 59 |
that currently has that Member allocated. The Pool Server processes through the |
| 60 |
results prior to forwarding, looking for the end token. When it is found the |
| 61 |
Member is deallocated from the User. The Member returns to the idle list and |
| 62 |
the User is returned to the TDS_SRV_IDLE state. |
| 63 |
|
| 64 |
One caveat to this is that querys that contain stateful information, such as |
| 65 |
the need to query @@identity after inserting, will not work. I am currently, |
| 66 |
thinking of adding a "lock connection" command that will be intercepted by the |
| 67 |
Pool Server and will cause it to allocate a Member to that User until an |
| 68 |
"unlock connection" command is received. |