org.tentackle.db
Class MpxConnectionManager

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

public class MpxConnectionManager
extends DefaultConnectionManager

Multiplexing connection manager.
A connection manager for applications with a large number of Db instances, e.g. application servers. The manager will multiplex N Db instances against M connections, allowing N > M. This is not to be mixed up with db-pooling, as connection multiplexing is completely transparent to the application whereas pooling requires an explicit relation to something like a session. Note that the authentication must be done at the application level because the pool's connections are derived from the userinfo of a server db.

Author:
harald

Field Summary
protected  int incSize
           
protected  int maxMinutes
           
protected  int maxSize
           
protected  int minMinutes
           
protected  int minSize
           
protected  Random random
           
protected  Db serverDb
           
protected  int unConCount
           
protected  int[] unConList
           
 
Fields inherited from class org.tentackle.db.DefaultConnectionManager
conList, dbList, freeConCount, freeConList, freeDbCount, freeDbList, idOffset, iniSize, maxConSize, maxDbSize, name
 
Constructor Summary
MpxConnectionManager(Db serverDb)
          Creates a connection manager with reasonable values for most servers.
MpxConnectionManager(String name, Db serverDb, int maxDb, int idOffset, int iniSize, int incSize, int minSize, int maxSize, int minMinutes, int maxMinutes)
          Creates a new connection manager.
 
Method Summary
 ManagedConnection attach(int id)
          Attaches a Db to a connection.
A Db must be attached before it can use any statements.
protected  int createConnections(int count)
          Create spare connections.
 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 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  int popUnattached()
          Gets a connection from the unattached freelist.
protected  void pushUnattached(int index)
          Adds the index of an unused connection to the freelist.
 void shutdown()
          Shuts down this connection manager.
All connections are closed and the threads stopped.
 
Methods inherited from class org.tentackle.db.DefaultConnectionManager
addConnection, addDb, getConnectionCount, getDbCount, getMaxConnections, getMaxCountForClearWarnings, getMaxLogins, removeConnection, removeDb, setMaxCountForClearWarnings, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serverDb

protected Db serverDb

incSize

protected int incSize

minSize

protected int minSize

maxSize

protected int maxSize

minMinutes

protected int minMinutes

maxMinutes

protected int maxMinutes

unConList

protected int[] unConList

unConCount

protected int unConCount

random

protected Random random
Constructor Detail

MpxConnectionManager

public MpxConnectionManager(String name,
                            Db serverDb,
                            int maxDb,
                            int idOffset,
                            int iniSize,
                            int incSize,
                            int minSize,
                            int maxSize,
                            int minMinutes,
                            int maxMinutes)
Creates a new connection manager.

Parameters:
name - the name of the connection manager
serverDb - the root db to use for creating connections (may be open or closed)
maxDb - the maximum number of Db instances, 0 = no limit
idOffset - the offset for connection ids (> 0)
iniSize - the initial size of the connection pool
incSize - the number of connections to add if all in use
minSize - the minimum number of connections
maxSize - the maximum number of connections
minMinutes - minimum minutes a connection should be used
maxMinutes - maximum minutes a connection should be used

MpxConnectionManager

public MpxConnectionManager(Db serverDb)
Creates a connection manager with reasonable values for most servers. Maximum of 1000 Db instances (500 clients). Start with 8 connections. Add 2 connections at a time if all connections are in use. Don't drop below 4 connections. Maximum of 100 connections, i.e. concurrent db operations (i.e. info above 25, warning above 50) Within 12 to 36h (approx. once a day), close and reopen connections (allows updates of the database servers's QEPs, prepared statements cache, etc...). The idOffset will be the connectionId of the serverDb + 1 (i.e. normally 2 for application servers)

Parameters:
serverDb - the root db to use for creating connections (may be open or closed).
Method Detail

shutdown

public void shutdown()
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
Overrides:
shutdown in class DefaultConnectionManager

pushUnattached

protected void pushUnattached(int index)
Adds the index of an unused connection to the freelist.

Parameters:
index - the index of the connection in the connections list

popUnattached

protected int popUnattached()
Gets a connection from the unattached freelist.

Returns:
the index to the connections list, -1 if no more unattached found

createConnections

protected int createConnections(int count)
Create spare connections.

Parameters:
count - the number of connections to create
Returns:
the number of connections created

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
Overrides:
login in class DefaultConnectionManager
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
Overrides:
logout in class DefaultConnectionManager
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
Overrides:
attach in class DefaultConnectionManager
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
Overrides:
detach in class DefaultConnectionManager
Parameters:
id - the connection ID of the db
Throws:
DbRuntimeException - if detach failed.


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