com.atomikos.jdbc.AbstractDataSourceBean#getConnection()
public Connection getConnection() throws SQLException {
if (LOGGER.isDebugEnabled()) {
LOGGER.logDebug(this + ": getConnection()...");
}
Connection connection = null;
this.init();
try {
//比较关键的一行代码,调用的com.atomikos.datasource.pool.ConnectionPoolWithSynchronizedValidation方法
//该方法最终拿到的就是AtomikosConnectionProxy
connection = (Connection)this.connectionPool.borrowConnection();
} catch (CreateConnectionException var3) {
this.throwAtomikosSQLException("Failed to grow the connection pool", var3);
} catch (PoolExhaustedException var4) {
this.throwAtomikosSQLException("Connection pool exhausted - try increasing 'maxPoolSize' and/or 'borrowConnectionTimeout' on the DataSourceBean.");
} catch (ConnectionPoolException var5) {
this.throwAtomikosSQLException("Error borrowing connection", var5);
}
if (LOGGER.isTraceEnabled()) {
LOGGER.logTrace(this + ": returning " + connection);
}
return connection;
}
AtomikosConnectionProxy类分析
com.atomikos.jdbc.AtomikosConnectionProxy#invoke
public Object invoke(Object proxy, Method method, Object[] args) throws SQLException {
String methodName = method.getName();
if (methodName.equals("getInvocationHandler")) {
return this;
} else if (methodName.equals("reap")) {
if (LOGGER.isDebugEnabled()) {
LOGGER.logDebug(this + ": reaping pending connection...");
}
this.reap();
if (LOGGER.isTraceEnabled()) {
LOGGER.logTrace(this + ": reap done!");
}
return null;
} else if (methodName.equals("isClosed")) {
if (LOGGER.isDebugEnabled()) {
LOGGER.logDebug(this + ": isClosed()...");
}
Object ret = this.closed;
if (LOGGER.isTraceEnabled()) {
LOGGER.logTrace(this + ": isClosed() returning " + ret);
}
return ret;
} else if (this.closed && !this.methodAllowedAfterClose(method)) {
String msg;
if (this.reaped) {
msg = "Connection has been reaped - calling " + methodName + " is no longer allowed! Increase reapTimeout to avoid this problem.";
AtomikosSQLException.throwAtomikosSQLException(msg);
} else {
msg = "Connection was already closed - calling " + methodName + " is no longer allowed!";
AtomikosSQLException.throwAtomikosSQLException(msg);
}
return null;
} else {
//在这里,获取连接时,第一步会走这里,这一步会去根据当前线程创建CompositeTransaction放到本地线程里面
if (this.isEnlistedInGlobalTransaction()) {
if (XA_INCOMPATIBLE_METHODS.contains(methodName)) {
AtomikosSQLException.throwAtomikosSQLException("Cannot call method '" + methodName + "' while a global transaction is running");
}
if (methodName.equals("setAutoCommit") && args[0].equals(Boolean.TRUE)) {
AtomikosSQLException.throwAtomikosSQLException("Cannot call 'setAutoCommit(true)' while a global transaction is running");
}
if (methodName.equals("getAutoCommit")) {
return Boolean.FALSE;
}
}
if (ENLISTMENT_METHODS.contains(methodName)) {
try {
this.enlist();
} catch (Exception var8) {
this.sessionHandleState.notifySessionErrorOccurred();
JdbcConnectionProxyHelper.convertProxyError(var8, "Error enlisting in transaction - connection might be broken? Please check the logs for more information...");
}
}
Object ret = null;
if (CLOSE_METHODS.contains(methodName) && args == null) {
this.close();
return null;
} else {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.logDebug(this + ": calling " + this.formatCallDetails(method, args) + "...");
}
//com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl去创建一个连接返回
ret = method.invoke(this.delegate, args);
} catch (Exception var7) {
this.sessionHandleState.notifySessionErrorOccurred();
JdbcConnectionProxyHelper.convertProxyError(var7, "Error delegating '" + methodName + "' call");
}
if (LOGGER.isTraceEnabled()) {
LOGGER.logTrace(this + ": " + methodName + " returning " + ret);
}
if (ret instanceof Statement) {
Statement s = (Statement)ret;
this.addStatement(s);
}
return ret;
}
}
}