home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JVISBRKR.Z / TimedConnect.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  2.4 KB  |  72 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. //--------------------------------------------------------------------------------------------------
  21. // CORBA Reference Application
  22. // Copyright (c) 1997 by Borland International, All Rights Reserved
  23. //
  24. // Contains TimedConnect Class.
  25. //--------------------------------------------------------------------------------------------------
  26.  
  27. package borland.reference.creditapproval.server;
  28.  
  29. import borland.sql.dataset.*;
  30. import borland.jbcl.dataset.*;
  31. import java.util.*;
  32.  
  33. /**
  34. * TimedConnect class repeatedly attempts to connect to a database
  35. * every X number of seconds for a total specified number of seconds.
  36. * If unable to connect, the Dataset/SQLException is rethrown.
  37. */
  38. public class TimedConnect {
  39.  
  40.   public TimedConnect(Database connectDb, int retrySeconds, int totalSeconds )
  41.        throws DataSetException {
  42.  
  43.     long current;
  44.     long nextRetry;
  45.  
  46.     long end = System.currentTimeMillis() + totalSeconds * 1000l;
  47.  
  48.     do {
  49.       try {
  50.         connectDb.openConnection();
  51.         break;
  52.       } catch (DataSetException e) {
  53.         System.err.println(e);
  54.  
  55.         // Pause for the specified number of seconds before attempting
  56.         // to connect again
  57.         nextRetry = System.currentTimeMillis() + retrySeconds * 1000l;
  58.  
  59.         do {
  60.  
  61.           current = System.currentTimeMillis();
  62.  
  63.           // Re-throw the exception if the maximum amount of
  64.           // time specified to attempt a connection has expired.
  65.           if (current >= end) throw e;
  66.  
  67.         } while (current < nextRetry );
  68.       }
  69.     } while (current < end);
  70.   }
  71. }
  72.