Documentation Archive Developer
Search
PATH  WebObjects 4.0 Documentation > EOF Developer's Guide

Table of Contents Previous Section

Limiting the Number of Database Connections

By default, an Enterprise Objects Framework application uses one connection to the database-that is, all of an application's display groups, EODatabaseDataSources, EOEditingContexts, and EODatabaseDataSources share the same database connections. However, if an application accesses multiple databases (an Oracle database and a Sybase database, for example), Enterprise Objects Framework establishes one database connection for each database, and these connections are shared as shown in Figure 46.

Figure 46. Sharing Database Connections

Because Enterprise Objects Framework uses the minimum number of connections by default, you don't need to do anything to limit the number of connections an application uses. However, you can close connections when they aren't in use.

Closing Database Connections

Enterprise Objects Framework doesn't close database connections. If many copies of an application are likely to be running at the same time, you may run out of database connections. You can reduce the likelihood of running out if you close connections when they aren't in use. A good time to close an EODatabaseChannel is after a specified period of inactivity. The following method demonstrates the process:

In Java:

public void closeChannels() {
int i, contextCount, j, channelCount;
NSArray contexts;
EOObjectStoreCoordinator coordinator;

coordinator = (EOObjectStoreCoordinator)EOObjectStoreCoordinator.defau ltCoordinator();

contexts = coordinator.cooperatingObjectStores();
contextCount = contexts.count();
for (i = 0; i < contextCount; i++) {
NSArray channels = ((EODatabaseContext)contexts.objectAtIndex(i)).registere dChannels();
channelCount = channels.count();
for (j = 0; j < channelCount; j++) {
((EODatabaseChannel)channels.objectAtIndex(j)).adaptorCh annel().closeChannel();
}
}
}
In Objective-C:

- (void)closeChannels
{
int i, contextCount, j, channelCount;
NSArray *contexts;
EOObjectStoreCoordinator *coordinator;

coordinator = [EOObjectStoreCoordinator defaultCoordinator];

contexts = [coordinator cooperatingObjectStores];
contextCount = [contexts count];
for (i = 0; i < contextCount; i++) {
NSArray *channels = [(EODatabaseContext *)
[contexts objectAtIndex:i] registeredChannels];
channelCount = [channels count];

for (j = 0; j < channelCount; j++) {
[[(EODatabaseChannel *)
[channels objectAtIndex:j] adaptorChannel]
closeChannel];
}
}
}
The closeChannels method gets the EODatabaseContexts from the default EOObjectStoreCoordinator. Then it gets the EODatabaseChannels registered with each EODatabaseContext. To close the database connection managed by an EODatabaseChannel, closeChannels sends the channel's EOAdaptorChannel a closeChannel message.

The next time a channel is needed, its EODatabaseContext reopens it automatically.

The closeChannels method above assumes that the application only has one EOObjectStoreCoordinator. If your application has multiple coordinators, you would repeat the process for each coordinator. It also assumes that all of the EOCooperatingObjectStores managed by the coordinator are EODatabaseContexts, which is nearly always the case. An EOObjectStoreCoordinator uses only EODatabaseContexts unless you substitute your own EOCooperatingObjectStore subclass. (For more information about EOCooperatingObjectStores, see the chapter "Application Configurations".)

Table of Contents Next Section