To create and register a new database channel with a particular database context, you can use the code in “Listing 10-2.” It’s probably most appropriate to put this code in your Session class, as this listing does. The code listing also shows how to register for the notification that EODatabaseContext posts when it needs another database channel.
Listing 9-2 Creating and registering a new database channel
import com.webobjects.foundation.*; |
import com.webobjects.appserver.*; |
import com.webobjects.eocontrol.*; |
import com.webobjects.eoaccess.*; |
public class Session extends WOSession { |
private final int DatabaseChannelCountMax = 3; |
public Session() { |
super(); |
NSNotificationCenter.defaultCenter().addObserver(this, new |
NSSelector("registerNewDatabaseChannel", |
new Class[] { NSNotification.class } ), |
EODatabaseContext.DatabaseChannelNeededNotification, null); |
} |
public void registerNewDatabaseChannel(NSNotification notification) { |
EODatabaseContext databaseContext = (EODatabaseContext)notification.object(); |
if (databaseContext.registeredChannels().count() < DatabaseChannelCountMax){ |
EODatabaseChannel channel = new EODatabaseChannel(databaseContext); |
databaseContext.registerChannel(channel); |
} |
} |
} |
If you create database channels programmatically on a per-session basis, you may end up with a large number of database channels per application instance. “When Database Connections Are Closed” describes how to check for and close extraneous database connections.
Last updated: 2007-07-11