Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Closing Database Channels

The code in “Listing 10-3” demonstrates how to close an application’s database connections at a recurring interval. The closeDatabaseChannels method assumes that the application has only one object store coordinator (per-session object store coordinators are not used) and that all of the cooperating object stores managed by the coordinator are database contexts, which is the usual case.

Listing 9-3  Close database channels at a specified interval

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
import java.util.*;
 
public class Application extends WOApplication {
 
    public static void main(String argv[]) {
        WOApplication.main(argv, Application.class);
    }
 
    public Application() {
        super();
        System.out.println("Welcome to " + this.name() + "!");
        setupDatabaseChannelCloserTimer();
    }
 
    public void setupDatabaseChannelCloserTimer() {
        Timer timer = new Timer(true);
        //Close open database connections every four hours.
        timer.scheduleAtFixedRate(new DBChannelCloserTask(), new Date(), 14400);
    }
 
    class DBChannelCloserTask extends TimerTask {
 
     public DBChannelCloserTask() {
      super();
     }
 
      public void run() {
       closeDatabaseChannels();
       NSLog.out.appendln("running timer");
      }
 
      public void closeDatabaseChannels() {
       int i, contextCount, j, channelCount;
       NSArray databaseContexts;
       EOObjectStoreCoordinator coordinator;
 
       coordinator =
       (EOObjectStoreCoordinator)EOObjectStoreCoordinator.defaultCoordinator();
       databaseContexts = coordinator.cooperatingObjectStores();
       contextCount = databaseContexts.count();
 
       //Iterate through all an app’s cooperating object stores (database contexts).
       for (i = 0; i < contextCount; i++) {
        NSArray channels =
         ((EODatabaseContext)databaseContexts.objectAtIndex(i)).registeredChannels();
        channelCount = channels.count();
 
          for (j = 0; j < channelCount; j++) {
          //Make sure the channel you're trying to close isn't performing a transaction.
          if (!((EODatabaseChannel)channels.objectAtIndex(j)).adaptorChannel().
            adaptorContext().hasOpenTransaction()) {
             ((EODatabaseChannel)channels.objectAtIndex(j)).adaptorChannel().
              closeChannel();
           }
          }
        }
      }
    }
}


< Previous PageNext Page > Hide TOC


Last updated: 2007-07-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice