org.tentackle.db
Class DefaultConnectionManager

java.lang.Object
  extended by org.tentackle.db.DefaultConnectionManager
All Implemented Interfaces:
ConnectionManager
Direct Known Subclasses:
MpxConnectionManager

public class DefaultConnectionManager
extends Object
implements ConnectionManager

The default implementation of a connection manager.
Each Db will get its own physical connection. This kind of manager is useful for 2-tier applications directly connecting to a database backend.

Although this manager implements a strict 1:1 mapping between dbs and connections it can be easily extended to implememt a M:N mapping, see the MpxConnectionManager.

Author:
harald

Field Summary
protected  ManagedConnection[] conList
          managed connections
protected  Db[] dbList
          Dbs logged in
protected  int freeConCount
          number of entries in freeConList
protected  int[] freeConList
          free list for conList (unused entries in conList)
protected  int freeDbCount
          number of entries in freeDbList
protected  int[] freeDbList
          free list for dbList (unused entries in dbList)
protected  int idOffset
          offset for connection IDs
protected  int iniSize
          initial size
protected  int maxConSize
          maximum number of connections, 0 = unlimited
protected  int maxDbSize
          maximum number of Dbs, 0 = unlimited
protected  String name
          name of the connection manager
 
Constructor Summary
DefaultConnectionManager()
          Creates a new connection manager with an initial size of 2, a maximum of 8 concurrent connections and an id offset of 1.
DefaultConnectionManager(String name, int iniSize, int maxSize, int idOffset)
          Creates a new connection manager.
 
Method Summary
protected  int addConnection(ManagedConnection con)
          Adds a connection to the list.
protected  int addDb(Db db)
          Adds a Db to the list.
 ManagedConnection attach(int id)
          Attaches a Db to a connection.
A Db must be attached before it can use any statements.
 void detach(int id)
          Detaches a connection from a Db.
A Db must be detached to release the connection for use of other Db instances.
 int getConnectionCount()
          Gets the number of established connections
 int getDbCount()
          Gets the number of valid db entries.
 int getMaxConnections()
          Gets the maximum number of connections.
 int getMaxCountForClearWarnings()
          Gets the current setting for clearWarnings() trigger
 int getMaxLogins()
          Gets the maximum number of allowed logins.
 int login(Db db)
          Logs in a Db connection.
It is up to the manager how to verify whether the Db is allowed to open, a real connection is initiated or just an application level authorization is performed.
 Db logout(int id)
          Logs out a Db connection.
protected  ManagedConnection removeConnection(int index)
          Removes a connection from the list.
protected  Db removeDb(int index)
          Removes a Db from the list.
 void setMaxCountForClearWarnings(int maxCountForClearWarnings)
          Sets the countForClearWarnings trigger, 0 = app must eat the warnings!
 void shutdown()
          Shuts down this connection manager.
All connections are closed and the threads stopped.
 String toString()
          Gets the name of the manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected String name
name of the connection manager


iniSize

protected int iniSize
initial size


idOffset

protected int idOffset
offset for connection IDs


dbList

protected Db[] dbList
Dbs logged in


maxDbSize

protected int maxDbSize
maximum number of Dbs, 0 = unlimited


freeDbList

protected int[] freeDbList
free list for dbList (unused entries in dbList)


freeDbCount

protected int freeDbCount
number of entries in freeDbList


conList

protected ManagedConnection[] conList
managed connections


maxConSize

protected int maxConSize
maximum number of connections, 0 = unlimited


freeConList

protected int[] freeConList
free list for conList (unused entries in conList)


freeConCount

protected int freeConCount
number of entries in freeConList

Constructor Detail

DefaultConnectionManager

public DefaultConnectionManager(String name,
                                int iniSize,
                                int maxSize,
                                int idOffset)
Creates a new connection manager.

Parameters:
name - the name of the connection manager
iniSize - the initial iniSize of the db table
maxSize - the maximum number of connections, 0 = unlimited (dangerous!)
idOffset - the offset for connection ids (> 0)

DefaultConnectionManager

public DefaultConnectionManager()
Creates a new connection manager with an initial size of 2, a maximum of 8 concurrent connections and an id offset of 1. This is the default connection manager for 2-tier client applications. The max connections will prevent ill behaving applications from tearing down the dbserver by opening connections excessively. The usual application holds 2 connections and temporarily 1 or 2 more. If you need more, change the connection manager in Db.

Method Detail

toString

public String toString()
Gets the name of the manager.

Overrides:
toString in class Object
Returns:
a string representation of the object.

setMaxCountForClearWarnings

public void setMaxCountForClearWarnings(int maxCountForClearWarnings)
Sets the countForClearWarnings trigger, 0 = app must eat the warnings!

Parameters:
maxCountForClearWarnings - the maxcount

getMaxCountForClearWarnings

public int getMaxCountForClearWarnings()
Gets the current setting for clearWarnings() trigger

Returns:
the countForClearWarnings trigger, 0 = app must eat the warnings!

addDb

protected int addDb(Db db)
Adds a Db to the list.

Parameters:
db - the db to add
Returns:
the index of db in the dblist

removeDb

protected Db removeDb(int index)
Removes a Db from the list.

Parameters:
index - the index of db in the dblist
Returns:
the removed db

getDbCount

public int getDbCount()
Gets the number of valid db entries.

Returns:
the number of db entries

addConnection

protected int addConnection(ManagedConnection con)
Adds a connection to the list.

Parameters:
con - the connection to add
Returns:
the index of connection in the conlist

removeConnection

protected ManagedConnection removeConnection(int index)
Removes a connection from the list.

Parameters:
index - the index of connection in the conlist
Returns:
the removed connection

getConnectionCount

public int getConnectionCount()
Gets the number of established connections

Returns:
the number of connections

getMaxLogins

public int getMaxLogins()
Description copied from interface: ConnectionManager
Gets the maximum number of allowed logins.

Specified by:
getMaxLogins in interface ConnectionManager
Returns:
max. number of concurrent logins, 0 = unlimited

getMaxConnections

public int getMaxConnections()
Description copied from interface: ConnectionManager
Gets the maximum number of connections.

Specified by:
getMaxConnections in interface ConnectionManager
Returns:
max. number of concurrent connections, 0 = unlimited

login

public int login(Db db)
          throws DbRuntimeException
Description copied from interface: ConnectionManager
Logs in a Db connection.
It is up to the manager how to verify whether the Db is allowed to open, a real connection is initiated or just an application level authorization is performed. Note that the ID may be recycled by the manager after a Db is logged out.

Specified by:
login in interface ConnectionManager
Parameters:
db - the db to login
Returns:
the connection ID of the db (> 0)
Throws:
DbRuntimeException - if login failed.

logout

public Db logout(int id)
          throws DbRuntimeException
Description copied from interface: ConnectionManager
Logs out a Db connection. The Db is not allowed to attach anymore. If the Db is still attached, a rollback of any pending tx is done and an exception thrown.

Specified by:
logout in interface ConnectionManager
Parameters:
id - the connection ID of the db
Returns:
the logged out Db
Throws:
DbRuntimeException - if logout failed.

attach

public ManagedConnection attach(int id)
                         throws DbRuntimeException
Description copied from interface: ConnectionManager
Attaches a Db to a connection.
A Db must be attached before it can use any statements. The framework will attach at the begining of a transaction or when getting a prepared statement or when getting a one-shot non-prepared statement. Note that attachments can be nested to any depth, i.e. only the first attach really binds the connection to the Db.

Specified by:
attach in interface ConnectionManager
Parameters:
id - the connection ID of the db
Returns:
the connection attached to be used by subsequent operations
Throws:
DbRuntimeException - if attach failed.

detach

public void detach(int id)
            throws DbRuntimeException
Description copied from interface: ConnectionManager
Detaches a connection from a Db.
A Db must be detached to release the connection for use of other Db instances. The framework will detach the db on every commit or rollback, after executeUpdate or after a resultset is closed for an executeQuery. Note that attachments can be nested to any depth, i.e. only the last detach really unbinds the connection from the Db.

Specified by:
detach in interface ConnectionManager
Parameters:
id - the connection ID of the db
Throws:
DbRuntimeException - if detach failed.

shutdown

public void shutdown()
Description copied from interface: ConnectionManager
Shuts down this connection manager.
All connections are closed and the threads stopped. Application servers should invoke this method when shut down.

Specified by:
shutdown in interface ConnectionManager


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de