net.sf.farrago.db
Class FarragoDbSingleton

java.lang.Object
  extended by org.eigenbase.util.CompoundClosableAllocation
      extended by net.sf.farrago.util.FarragoCompoundAllocation
          extended by net.sf.farrago.db.FarragoDbSingleton
All Implemented Interfaces:
FarragoAllocation, FarragoAllocationOwner, ClosableAllocation, ClosableAllocationOwner
Direct Known Subclasses:
FarragoDatabase

public abstract class FarragoDbSingleton
extends FarragoCompoundAllocation

FarragoDbSingleton manages a singleton instance of FarragoDatabase. It is reference-counted to allow it to be shared in a library environment such as the directly embedded JDBC driver. Note that all synchronization is done at the class level, not the object level.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/db/FarragoDbSingleton.java#16 $
Author:
John V. Sichi

Field Summary
private static boolean backupInProgress
          Flag indicating whether a database backup is already in progress
private static boolean inShutdown
          Flag indicating whether FarragoDbSingleton is already in shutdown(), to help prevent recursive shutdown.
private static boolean inShutdownConditional
          Flag indicating whether FarragoDbSingleton is already in shutdownConditional(int), to help prevent recursive shutdown.
protected static FarragoDatabase instance
          Singleton instance, or null when nReferences == 0.
private static int nReferences
          Reference count.
protected static Logger tracer
           
 
Fields inherited from class org.eigenbase.util.CompoundClosableAllocation
allocations
 
Constructor Summary
FarragoDbSingleton()
           
 
Method Summary
(package private) static void addSession(FarragoDatabase db, FarragoDbSession session)
           
private static int countLoopbackSessions()
           
(package private) static void disconnectSession(FarragoDbSession session)
           
static List<FarragoSession> getSessions()
           
static List<FarragoSession> getSessions(FarragoDatabase db)
          Retrieve a list of connected FarragoSession objects.
static boolean isReferenced()
           
static FarragoDatabase pinReference(FarragoSessionFactory sessionFactory)
          Establishes a database reference.
static FarragoDatabase pinReference(FarragoSessionFactory sessionFactory, boolean requireExistingEngine)
          Establishes a database reference.
static boolean setBackupFlag(boolean inProgress)
          Sets a flag indicating whether a backup is in progress.
static void shutdown()
          Shuts down the database, killing any running sessions.
static boolean shutdownConditional(int groundReferences)
          Conditionally shuts down the database depending on the number of references.
 
Methods inherited from class org.eigenbase.util.CompoundClosableAllocation
addAllocation, closeAllocation, forgetAllocation, hasAllocations
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.eigenbase.util.ClosableAllocationOwner
addAllocation
 
Methods inherited from interface org.eigenbase.util.ClosableAllocation
closeAllocation
 
Methods inherited from interface org.eigenbase.util.ClosableAllocation
closeAllocation
 

Field Detail

tracer

protected static final Logger tracer

nReferences

private static int nReferences
Reference count.


instance

protected static FarragoDatabase instance
Singleton instance, or null when nReferences == 0.


inShutdown

private static boolean inShutdown
Flag indicating whether FarragoDbSingleton is already in shutdown(), to help prevent recursive shutdown.


backupInProgress

private static boolean backupInProgress
Flag indicating whether a database backup is already in progress


inShutdownConditional

private static boolean inShutdownConditional
Flag indicating whether FarragoDbSingleton is already in shutdownConditional(int), to help prevent recursive shutdown.

Constructor Detail

FarragoDbSingleton

public FarragoDbSingleton()
Method Detail

pinReference

public static FarragoDatabase pinReference(FarragoSessionFactory sessionFactory)
Establishes a database reference. If this is the first reference, the database will be loaded first; otherwise, the existing database is reused with an increased reference count.

Parameters:
sessionFactory - factory for various database-level objects
Returns:
loaded database

pinReference

public static FarragoDatabase pinReference(FarragoSessionFactory sessionFactory,
                                           boolean requireExistingEngine)
Establishes a database reference. If requireExistingEngine, a new database will not be loaded, even if this is the first reference.

Parameters:
sessionFactory - factory for various database-level objects
requireExistingEngine - true if require an already created reference
Returns:
loaded database

addSession

static void addSession(FarragoDatabase db,
                       FarragoDbSession session)

disconnectSession

static void disconnectSession(FarragoDbSession session)

getSessions

public static List<FarragoSession> getSessions(FarragoDatabase db)
Retrieve a list of connected FarragoSession objects. Each invocation produces a new List. Altering the list has no effect on the given FarragoDatabase.

The returned FarragoSession objects may be disconnected at any time. See FarragoSession.isClosed().

Parameters:
db - sessions are retrieved from this FarragoDatabase instance
Returns:
non-null List of FarragoSession objects

getSessions

public static List<FarragoSession> getSessions()

shutdownConditional

public static boolean shutdownConditional(int groundReferences)
Conditionally shuts down the database depending on the number of references.

Parameters:
groundReferences - threshold for shutdown; if actual number of sessions is greater than this, no shutdown takes place
Returns:
whether shutdown took place

shutdown

public static void shutdown()
Shuts down the database, killing any running sessions.


countLoopbackSessions

private static int countLoopbackSessions()

isReferenced

public static boolean isReferenced()
Returns:
true if the single currently exists

setBackupFlag

public static boolean setBackupFlag(boolean inProgress)
Sets a flag indicating whether a backup is in progress. If the flag is true and a backup is already in progress, then false is returned. Otherwise, true is returned.

Parameters:
inProgress - true if the flag is to be set, indicating that a backup is in progress
Returns:
true if setting of the flag was successful