XpoDefault.GetConnectionPoolString(String, Int32) Method
Gets a connection pool’s connection string based on a specified standard connection string and pool capacity.
A String value which specifies the parameters required to establish a connection.
A connection pool’s capacity (the number of connections to be made available within the pool).
A String value which specifies the parameters needed to establish a connection pool’s connection.
A regular Data Access Layer (DAL) configuration shares a single IDataStore instance between all Session and UnitOfWork objects. When several threads simultaneously execute a read or write operation on Data Access Layer, the first thread executes a query immediately, while the remaining threads have to wait. In high-load applications, such as web sites and web services, use DataStorePool to reduce the waiting time.
Unlike a regular data store, DataStorePool maintains a set of initialized, ready-to-use IDataStore objects. This configuration can serve concurrent read/write operations without delays.
If an application creates a new DAL for every user, they do not have to wait for each other. Note that this approach has a drawback: in a simple configuration, a data layer acquires a database connection and keeps it open, and other threads cannot use this connection object until the application releases it when the user’s session expires. To save SQL server resources and untie database connections from user sessions, change the data layer configuration so that all IDataLayer objects use the same DataStorePool instance.
The GetConnectionPoolString method returns a connection string with a set of options for DataStorePool. Use this connection string with the GetDataLayer or GetConnectionProvider methods to activate the DataStorePool component.
DataStorePool has the following configuration options:
- Specifies how many IDataStore instances to keep in a pool. The number of IDataStore instances in use may exceed the poolSize. In this case, DataStorePool destroys redundant instances when clients release them. The poolSize value should be nearly the same as the expected number of simultaneous requests. When the poolSize is small, DataStorePool acquires new connections at the moments of high user activity and releases unused connections when the number of active users is low. Note that the IDataStore instantiation takes some time. You may want to increase the poolSize value to minimize time to instantiate additional IDataStore objects. The default poolSize value is 8. You do not need to change this value in most cases.
- Specifies the maximum number of IDataStore instances in use. If the number of IDataStore instances reaches the maxConnections value, DataStorePool suspends subsequent requests and resumes their execution as soon as previous requests release IDataStore instances. The default maxConnections value (-1) allows the DataStorePool to create up to Int32.MaxValue IDataStore instances. You can change the maxConnections value to improve the database performance with many concurrent users.
string connectionString = "Your connection string"; // Here 2 is the number of connections in a pool; 1 connection is not pooled. // Total is 3 - the maximum number of connections to be established. string connectionPoolString = XpoDefault.GetConnectionPoolString(connectionString, 2, 3); IDataStore dataStore = XpoDefault.GetConnectionProvider(connectionPoolString, AutoCreateOption.DatabaseAndSchema); XPDictionary dictionary = new ReflectionDictionary(); // ... XpoDefault.DataLayer = new ThreadSafeDataLayer(dictionary, dataStore); XpoDefault.Session = null;