| Method from org.enhydra.jdbc.pool.StandardXAPoolDataSource Detail: |
public void connectionClosed(ConnectionEvent event) {
Object obj = event.getSource();
log.debug("StandardXAPoolDataSource:connectionClosed");
XAConnection xac = (XAConnection) obj; // cast it into an xaConnection
Transaction tx = null;
try {
if (transactionManager == null) {
TransactionManager tm =
((StandardXADataSource) xads).getTransactionManager();
if (tm == null) {
throw new NullPointerException("TM is null");
} else
// here we use tm instead to setup transactionManager = tm
// if the current transactionManager property is null, it stays
// there, and we continue to use the TM from the XADataSource
tx = tm.getTransaction();
} else {
tx = transactionManager.getTransaction();
}
log.debug(
"StandardXAPoolDataSource:connectionClosed get a transaction");
} catch (NullPointerException n) {
// current is null: we are not in EJBServer.
log.error(
"StandardXAPoolDataSource:connectionClosed should not be used outside an EJBServer");
} catch (SystemException e) {
log.error(
"StandardXAPoolDataSource:connectionClosed getTransaction failed:"
+ e);
}
// delist Resource if in transaction
// We must keep the connection till commit or rollback
if ((tx != null)
&& (((StandardXAConnection) xac).connectionHandle.isReallyUsed)) {
try {
tx.delistResource(xac.getXAResource(), XAResource.TMSUCCESS);
// delist the xaResource
log.debug(
"StandardXAPoolDataSource:connectionClosed the resourse is delisted");
} catch (Exception e) {
log.error(
"StandardXAPoolDataSource:connectionClosed Exception in connectionClosed:"
+ e);
}
}
log.debug(
"StandardXAPoolDataSource:connectionClosed checkIn an object to the pool");
pool.checkIn(obj); // return the connection to the pool
}
Invoked when the application calls close()
on its representation of the connection |
public GenerationObject create(String _user,
String _password) throws SQLException {
GenerationObject genObject;
XAConnection xaCon = xads.getXAConnection(_user, _password);
// get the xa connection
xaCon.addConnectionEventListener(this); // add it to the event listener
log.debug(
"StandardXAPoolDataSource:create create a object for the pool");
genObject =
new GenerationObject(xaCon, pool.getGeneration(), _user, _password);
return genObject;
}
|
public XADataSource getDataSource() {
return xads;
}
Getter for property dataSource. |
public Object getObjectInstance(Object refObj,
Name name,
Context nameCtx,
Hashtable env) throws Exception {
super.getObjectInstance(refObj, name, nameCtx, env);
Reference ref = (Reference) refObj;
InitialContext ictx = new InitialContext(env);
this.setTransactionManager(
(TransactionManager) ictx.lookup(
"javax.transaction.TransactionManager"));
this.setDataSource((XADataSource) ictx.lookup(this.dataSourceName));
log.debug("StandardPoolDataSource:getObjectInstance: instance created");
return this;
}
|
public Reference getReference() throws NamingException {
log.debug(
"StandardXAPoolDataSource:getReference return a reference of the object");
Reference ref = super.getReference();
ref.add(
new StringRefAddr("transactionManagerName", "TransactionManager"));
return ref;
}
Retrieves the Reference of this object. Used at binding time by JNDI
to build a reference on this object. |
public void setDataSource(XADataSource dataSource) {
this.xads = dataSource;
if (transactionManager != null)
((StandardXADataSource) dataSource).setTransactionManager(
transactionManager);
}
Setter for property dataSource. |
public void setTransactionManager(TransactionManager tm) {
log.debug("StandardXAPoolDataSource:setTransactionManager");
transactionManager = tm;
}
|
public boolean testThisObject(Object o) {
Connection ret = null;
log.debug(
"StandardPoolDataSource:testThisObject verify the current object");
Transaction suspended = null;
try {
Transaction tx = transactionManager == null
? null
: transactionManager.getTransaction();
boolean isActive = tx == null
? false
: tx.getStatus() == Status.STATUS_ACTIVE;
if (isActive) {
suspended = transactionManager.suspend();
}
PooledConnection con = (PooledConnection) o;
ret = con.getConnection();
Statement s = ret.createStatement();
s.execute(jdbcTestStmt);
s.close();
try {
ret.close();
} catch (Exception e) {
log.error(
"StandardPoolDataSource:checkThisObject can't closed the connection: "
+ e);
}
return true;
} catch (SQLException e) {
log.error(
"StandardXAPoolDataSource:checkThisObject Error java.sql.SQLException in StandardXAPoolDataSource:testThisObject");
return false;
} catch (SystemException e) {
log.error(
"StandardXAPoolDataSource:checkThisObject Error java.sql.SystemException in StandardXAPoolDataSource:testThisObject");
return false;
} finally {
if (suspended != null) {
try {
transactionManager.resume(suspended);
} catch (Exception ex) {
log.error(
"StandardXAPoolDataSource:checkThisObject Error Exception in StandardXAPoolDataSource:testThisObject");
return false;
}
}
}
}
This method tests if a connection is valid or not. It overrides the
method in StandardPoolDataSource to take into account global transactions:
if global transaction is in progress - suspend it so that
connection testing happens ouside of transaction.
If connection testing fails - it will not affect transaction
and next good connection can join the transaction |
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("StandardXAPoolDataSource:\n");
if (this.transactionManager != null)
sb.append(" transaction manager=< "+this.transactionManager.toString()+" >\n");
if (this.xads != null)
sb.append(this.xads.toString());
sb.append(super.toString());
return sb.toString();
}
|