TDS Connection Pooling

The Connection Pooling server swims in the src/pool directory.

The FreeTDS connection pool is a server process; it emulates a SQL Server. Any program that can attach to a real SQL Server may instead elect to attach to the pool server. The pool in turn connects to the SQL Server and database you specify, and attempts to share these connections. See the src/pool/README for a more detailed description of its inner workings.

To configure the pool server, first make sure FreeTDS has a working entry for the real SQL Server by connecting to it with tsql or another program.

[Note]Note

The FreeTDS connection pool currently does not supports TDS version 5.0 (Sybase) and encrypted connections. This restriction applies to both the client-to-pool and pool-to-server connections!

After FreeTDS has been installed, you will find an executable named tdspool in the /usr/local/bin directory (or whatever directory was specified with the configure --with-prefix flag option).

Edit pool.conf in the FreeTDS's etc directory. The pool.conf file is formatted like freetds.conf, with a section name in brackets and options for each section in key/value pairs.

Just as in freetds.conf there are two types of sections, a [global] section whose options affect all pools, and a section with the name of the pool for pool-specific options. The following options are supported and may appear in either section.

Table 5.1. pool.conf settings

NamePossible ValuesDefaultMeaning
userAny valid usernoneThe username used to connect to the pool server.
passwordAnynoneThe password of the user at the pool server.
server userAny valid useruser fieldThe username used to connect to the servername.
server passwordAnypassword fieldThe password of the user at the servername.
serverAny entry in the freetds.conf filenoneThe alias from the freetds.conf file representing the servername that will be connected to.
databaseAny valid databaseUser's default databaseThe database on the servername to use.
portAny TCP portnonePort on which tdspool will listen.
min pool conn0 or morenoneMinimum number of open connections to maintain to the servername. 0 will cause pool server to not open any initial connection.
max pool conn1 or morenoneMaximum number of open connections to open against the servername.
max member age0 (no limit) or a number of seconds0Maximum age of idle members before connection is closed.


Now, let's put this into practice.

Example 5.7. pool.conf

	[global]
	min pool conn = 5
	max pool conn = 10
	max member age = 120
	
	[mypool]
	user = webuser
	password = secret
	database = ebiz
	server = fooserv
	max pool conn = 7
	port = 5000
	


The [global] section defines that we will open 5 connections against the server initially, and will increase up to 10 as demand requires. These connections will be closed after being idle for 2 minutes (120 seconds), but only until there are 5 remaining open.

The [mypool] section defines a pool named mypool that will listen on port 5000. It will login to a SQL Server named fooserv using the user webuser and the ever so clever password of secret. Once logged in, the connections will use the database ebiz instead of webuser's default database. Also, since this SQL Server has a limited number of CALs (Client Access Licenses), we are restricting the maximum number of connections to 7, which overrides the global setting of 10.

Run tdspool with the name of the pool you are serving.

	$  tdspool mypool

On Unix systems tdspool can also be started by inetd or xinetd using the -x option. The service must be configured in wait mode, so that the listening socket (not an accepted connection) is passed to tdspool as standard input. In this mode the port setting in pool.conf is ignored (but still required); the port is the one configured in xinetd. Standard output and error are redirected to /dev/null; use the -l option to specify a log file. The -d option cannot be used together with -x.

Example 5.8. xinetd service for tdspool

	service tdspool
	{
		type        = UNLISTED
		socket_type = stream
		protocol    = tcp
		port        = 5000
		wait        = yes
		user        = nobody
		server      = /usr/local/bin/tdspool
		server_args = -x -l /var/log/tdspool.log mypool
	}
	


tdspool also supports systemd socket activation; no option is needed, the socket passed by systemd is detected automatically. The socket unit must use Accept=no and a single ListenStream. As with xinetd, the port setting in pool.conf is ignored.

Example 5.9. systemd units for tdspool

	# /etc/systemd/system/tdspool.socket
	[Socket]
	ListenStream=5000
	Accept=no

	[Install]
	WantedBy=sockets.target

	# /etc/systemd/system/tdspool.service
	[Service]
	ExecStart=/usr/local/bin/tdspool mypool
	User=nobody
	


Before your clients connect to the pool, you must edit your freetds.conf to include the host and port of the pooling server, and point your clients at it.