# Poco::Data

# class SessionPool

Library: Data
Package: SessionPooling
Header: Poco/Data/SessionPool.h

## Description

This class implements session pooling for POCO Data.

Creating a connection to a database is often a time consuming operation. Therefore it makes sense to reuse a session object once it is no longer needed.

A SessionPool manages a collection of SessionImpl objects (decorated with a PooledSessionImpl).

When a SessionImpl object is requested, the SessionPool first looks in its set of already initialized SessionImpl for an available object. If one is found, it is returned to the client and marked as "in-use". If no SessionImpl is available, the SessionPool attempts to create a new one for the client. To avoid excessive creation of SessionImpl objects, a limit can be set on the maximum number of objects. Sessions found not to be connected to the database are purged from the pool whenever one of the following events occurs:

- JanitorTimer event
- get() request
- putBack() request

Not connected idle sessions can not exist.

Usage example:

    SessionPool pool("ODBC", "...");
    ...
    Session sess(pool.get());
    ...

## Member Summary

Member Functions: allocated, available, capacity, customizeSession, dead, deadImpl, get, idle, onJanitorTimer, purgeDeadSessions, putBack, used

## Types

### PooledSessionHolderPtr

typedef Poco::AutoPtr < PooledSessionHolder > PooledSessionHolderPtr;

### PooledSessionImplPtr

typedef Poco::AutoPtr < PooledSessionImpl > PooledSessionImplPtr;

### SessionList

typedef std::list < PooledSessionHolderPtr > SessionList;

## Constructors

### SessionPool

SessionPool(
const std::string & sessionKey,
const std::string & connectionString,
int minSessions = 1,
int maxSessions = 32,
int idleTime = 60
);

Creates the SessionPool for sessions with the given sessionKey and connectionString.

The pool allows for at most maxSessions sessions to be created. If a session has been idle for more than idleTime seconds, and more than minSessions sessions are in the pool, the session is automatically destroyed.

If idleTime is 0, automatic cleanup of unused sessions is disabled.

## Destructor

### ~SessionPool

virtual ~SessionPool();

Destroys the SessionPool.

## Member Functions

### allocated

int allocated() const;

Returns the number of allocated sessions.

### available

int available() const;

Returns the number of available (idle + remaining capacity) sessions.

### capacity

int capacity() const;

Returns the maximum number of sessions the SessionPool will manage.

### dead

int dead();

Returns the number of not connected active sessions.

### get

Session get();

Returns a Session.

If there are unused sessions available, one of the unused sessions is recycled. Otherwise, a new session is created.

If the maximum number of sessions for this pool has already been created, a SessionPoolExhaustedException is thrown.

### idle

int idle() const;

Returns the number of idle sessions.

### used

int used() const;

Returns the number of sessions currently in use.

### customizeSession

virtual void customizeSession(
Session & session
);

Can be overridden by subclass to perform custom initialization of a newly created database session.

The default implementation does nothing.

### deadImpl

int deadImpl(
SessionList & rSessions
);

### onJanitorTimer

void onJanitorTimer(
Poco::Timer & param41
);

### purgeDeadSessions

void purgeDeadSessions();

### putBack

void putBack(
PooledSessionHolderPtr pHolder
);