*/
public class ConnectionPool {
- public static final String POOL_JMX_TYPE_PREFIX = "org.apache.tomcat.jdbc.pool.jmx:type=";
+ public static final String POOL_JMX_TYPE_PREFIX = "tomcat.jdbc:type=";
//logger
protected static Log log = LogFactory.getLog(ConnectionPool.class);
protected boolean closed = false;
/**
- * Size of the pool
- */
- protected AtomicInteger size = new AtomicInteger(0);
-
- /**
* Since newProxyInstance performs the same operation, over and over
* again, it is much more optimized if we simply store the constructor ourselves.
*/
}
if (pool.size()==0 && force && pool!=busy) pool = busy;
}
- size.set(0);
if (this.getPoolProperties().isJmxEnabled()) stopJmx();
PoolProperties.InterceptorDefinition[] proxies = getPoolProperties().getJdbcInterceptorsAsArray();
for (int i=0; i<proxies.length; i++) {
if (con!=null) {
PooledConnection result = borrowConnection(now, con);
//validation might have failed, in which case null is returned
+ //should not happen anymore
if (result!=null) return result;
}
- if (size.get() < getPoolProperties().getMaxActive()) {
- if (size.addAndGet(1) <= getPoolProperties().getMaxActive()) {
- return createConnection(now, con);
- } else {
- size.addAndGet(-1); //restore the value, we didn't create a connection
- }
+
+ //if we get here, see if we need to create one
+ //this is not 100% accurate since it doesn't use a shared
+ //atomic variable - a connection can become idle while we are creating
+ //a new connection
+ if (busy.size() < getPoolProperties().getMaxActive()) {
+ return createConnection(now, con);
} //end if
//calculate wait time for this iteration
con.validate(PooledConnection.VALIDATE_RETURN)) {
con.setStackTrace(null);
con.setTimestamp(System.currentTimeMillis());
- if (!idle.offer(con)) {
+ if ((idle.size()>=poolProperties.getMaxIdle()) || (!idle.offer(con))) {
if (log.isDebugEnabled()) {
- log.debug("Connection ["+con+"] will be closed and not returned to the pool, idle.offer failed.");
+ log.debug("Connection ["+con+"] will be closed and not returned to the pool, idle["+idle.size()+"]>=maxIdle["+poolProperties.getMaxIdle()+"] idle.offer failed.");
}
release(con);
}
}
protected void finalize(PooledConnection con) {
- size.addAndGet(-1);
+
}
protected void startJmx() {
import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
m = datasource.getClass().getMethod(method.getName(), method.getParameterTypes());
methods.put(method, m);
}
- return m.invoke(datasource, args);
+ try {
+ return m.invoke(datasource, args);
+ }catch (InvocationTargetException t) {
+ throw t.getTargetException();
+ }
}
}