// Aim - To obtain a connection for any purpose, from the outside.
// 1st, get initial JNDI context (Java Naming and Directory Interfaces).
InitialContext initctx = new InitialContext();
// Perform JNDI lookup to obtain a connection factory.
connectionFactory =
(IConnectionFactory) initctx.lookup("EISConnections/JDBCFactory");
// Next retrieve the ConnectionSpec and set the values.
IConnectionSpec spec = connectionFactory.getConnectionSpec();
spec.setPropertyValue("client", "800");
spec.setPropertyValue("user", "aditm");
spec.setPropertyValue("passwd", "macpc");
spec.setPropertyValue("language", "EN");
spec.setPropertyValue("ashost", "10.30.116.40");
spec.setPropertyValue("sysnr", "00");
// Then retrieve the connection handle.
con = connectionFactory.getConnectionEx(spec);
// Writing the code in the connector that establish the connection.
// First, the application server passes the connection manager instance // to the connection
// factory object and instantiates the connection factory object during // startup.
public CCIConnectionFactory(
JDBCManagedConnectionFactory managedConnectionFactory,
ConnectionManager connectionManager) {
m_managedConnectionFactory = managedConnectionFactory;
if (connectionManager == null)
m_connectionManager = new JDBCConnectionManager();
else
m_connectionManager = connectionManager;
}
// We delegate the request for a new connection to the application
// server connection manager.
public IConnection getConnectionEx(IConnectionSpec properties)
throws ConnectionFailedException {
try {
return (IConnection)
// We are passing two parameters here: the real connection factory,
// and the connection properties object.
m_connectionManager.allocateConnection(
m_managedConnectionFactory,
(ConnectionRequestInfo) properties);
} catch (ResourceException re) {
throw new ConnectionFailedException(
re.getLinkedException().getMessage());
}
}
// Next, the connection manager, which is not part of our code, but
// rather part of the application server, looks up the requested
// connection using the connection properties
(ConnectionReuqestInfo).
// The connection manager has a pool of connections, but it does not
// know if a certain connection is the same as the one requested. That
// can only be determined by the connector.
// Therefore, it calls the matching method on the real connection
// factor y (the managed connection factory).
public ManagedConnection matchManagedConnections(
Set connectionSet,
Subject subject,
ConnectionRequestInfo info)
throws ResourceException {
Map connectionProperties = (Map) info;
Iterator it = connectionSet.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof JDBCManagedConnection) {
JDBCManagedConnection mc = (JDBCManagedConnection) obj;
// Depending on the following method, the pool will either be
// efficient or not, that is,
// there may be two connections that can be treated as the same.
// They connect to the same
// data source. Yet, they are not treated as such because they
// differ on an irrelevant
// property. the connection manager is then forced to create a new
// connection when one
// from the pool would have sufficed.
if (this.equals(mc.getManagedConnectionFactory())
&& connectionProperties.equals(mc.getConnectionProperties()))
//The following is pseudocode.
if (connectionProperties essentialyEquals mc
.getConnectionProperties())
change the Non Essential Properies of mc.return mc;
}
}
return null;
}
// Then we found the connector in the pool, the connection manager calls
// createConnection on the managed connection.
public Object getConnection(Subject subject, ConnectionRequestInfo info)
throws javax.resource.ResourceException {
return (javax.resource.cci.Connection) new CCIConnection(this);
}
// The connection object is returned to the user.
// If the connector was not found in the pool, we need to create
// a new managed connection.
public ManagedConnection createManagedConnection(
Subject subject,
ConnectionRequestInfo connectionRequestInfo)
throws ResourceException {
try {
Map connectionProperties = (Map) connectionRequestInfo;
JDBCManagedConnection managedConnection =
new JDBCManagedConnection(this);
if (m_connectionTimeout == -1) // no timeout
{
managedConnection.init(connectionProperties);
if (managedConnection.isInitialized())
return managedConnection;
else
throw new ConnectionFailedException(
"Connection Properties:"
+ connectionProperties.toString());
} else {
Thread asyncConnectionThread =
new Thread(
new AsyncConnectionFactory(
Thread.currentThread(),
managedConnection,
connectionProperties));
asyncConnectionThread.start();
try {
Thread.sleep(m_connectionTimeout);
} catch (InterruptedException ie) {
asyncConnectionThread.destroy();
if (managedConnection.isInitialized())
return managedConnection;
else
throw new ConnectionFailedException(
"Connection Properties:"
+ connectionProperties.toString());
}
asyncConnectionThread.destroy();
throw new TimeoutException(
"Connection Properties:" + connectionProperties.toString());
}
} catch (Exception e) {
ResourceException re = new ResourceException(e.getMessage());
re.setLinkedException(e);
throw re;
}
// Lets see the constructor and init method in managed connection.
public JDBCManagedConnection(JDBCManagedConnectionFactory managedConnectionFactory) {
m_connection = null;
m_managedConnectionFactory = managedConnectionFactory;
m_initialized = false;
// this is a flag to indicate if init has succeeded.
}
public void init(Map connectionProperties) {
m_connectionProperties = connectionProperties;
try {
// Now, the actual connection object is being instanstiated.
m_connection =
DriverManager.getConnection(
(String) connectionProperties.get("url"),
(String) connectionProperties.get("username"),
(String) connectionProperties.get("password"));
} catch (SQLException se) {
throw new RuntimeException("SQL Exception: " + se.getMessage());
}
m_eventListeners = new ArrayList();
try {
m_initialized = !(m_connection.isClosed());
} catch (Exception e) {
}
}
// The application server adds an event listener to the managed
// connection object. when the managed connection object will close,
// it will notify the event listener.
public void addConnectionEventListener
(ConnectionEventListener addedEventListener) {
m_eventListeners.add(addedEventListener);
}