bug fixed pscache

This commit is contained in:
wenshao
2012-08-14 11:05:51 +08:00
parent 4941a2f98d
commit 66cb1de037
5 changed files with 245 additions and 4 deletions
@@ -683,7 +683,7 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM
throws SQLException {
statementLog("{conn-" + statement.getConnectionProxy().getId() + ", pstmt-" + statement.getId()
+ "} clearParameters. " + statement.getSql());
+ "} clearParameters. ");
chain.preparedStatement_clearParameters(statement);
}
@@ -770,7 +770,14 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
for (int i = 0; i < poolingCount; ++i) {
try {
JdbcUtils.close(connections[i].getConnection());
ConnectionHolder connHolder = connections[i];
for (PreparedStatementHolder stmtHolder : connHolder.getStatementPool().getMap().values()) {
connHolder.getStatementPool().closeRemovedStatement(stmtHolder);
}
connHolder.getStatementPool().getMap().clear();
JdbcUtils.close(connHolder.getConnection());
connections[i] = null;
destroyCount++;
} catch (Exception ex) {
@@ -23,6 +23,8 @@ import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey;
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.OracleUtils;
@@ -89,7 +91,7 @@ public class PreparedStatementPool {
PreparedStatementHolder oldHolder = map.put(key, holder);
if (oldHolder != null && oldHolder != holder) {
oldHolder.setPooling(false);
dataSource.closePreapredStatement(oldHolder);
closeRemovedStatement(oldHolder);
} else {
if (holder.getHitCount() == 0) {
dataSource.incrementCachedPreparedStatementCount();
@@ -97,6 +99,22 @@ public class PreparedStatementPool {
}
holder.setPooling(true);
if (LOG.isDebugEnabled()) {
String message = null;
if (holder.getStatement() instanceof PreparedStatementProxy) {
PreparedStatementProxy stmtProxy = (PreparedStatementProxy) holder.getStatement();
if (stmtProxy instanceof CallableStatementProxy) {
message = "{conn-" + stmtProxy.getConnectionProxy().getId() + ", cstmt-" + stmtProxy.getId() + "} enter cache";
} else {
message = "{conn-" + stmtProxy.getConnectionProxy().getId() + ", pstmt-" + stmtProxy.getId() + "} enter cache";
}
} else {
message = "stmt enter cache";
}
LOG.debug(message);
}
}
public void clear() throws SQLException {
@@ -110,7 +128,23 @@ public class PreparedStatementPool {
}
}
private void closeRemovedStatement(PreparedStatementHolder holder) throws SQLException {
public void closeRemovedStatement(PreparedStatementHolder holder) throws SQLException {
if (LOG.isDebugEnabled()) {
String message = null;
if (holder.getStatement() instanceof PreparedStatementProxy) {
PreparedStatementProxy stmtProxy = (PreparedStatementProxy) holder.getStatement();
if (stmtProxy instanceof CallableStatementProxy) {
message = "{conn-" + stmtProxy.getConnectionProxy().getId() + ", cstmt-" + stmtProxy.getId() + "} exit cache";
} else {
message = "{conn-" + stmtProxy.getConnectionProxy().getId() + ", pstmt-" + stmtProxy.getId() + "} exit cache";
}
} else {
message = "stmt exit cache";
}
LOG.debug(message);
}
holder.setPooling(false);
if (holder.isInUse()) {
return;
@@ -11,6 +11,7 @@ import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@@ -32,6 +33,8 @@ public class WebStatFilter implements Filter {
public final static String PARAM_NAME_SESSION_STAT_ENABLE = "sessionStatEnable";
public final static String PARAM_NAME_SESSION_STAT_MAX_COUNT = "sessionStatMaxCount";
public static final String PARAM_NAME_EXCLUSIONS = "exclusions";
public static final String PARAM_NAME_PRINCIPAL_SESSION_NAME = "princialSessionName";
public static final String PARAM_NAME_PRINCIPAL_COOKIE_NAME = "princialCookieName";
public final static int DEFAULT_MAX_STAT_SESSION_COUNT = 1000 * 100;
@@ -52,6 +55,9 @@ public class WebStatFilter implements Filter {
private String contextPath;
private String principalSessionName;
private String principalCookieName;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
@@ -102,12 +108,15 @@ public class WebStatFilter implements Filter {
if (sessionStat != null) {
sessionStat.afterInvoke(error, nanoSpan);
sessionStat.setPrincipal(getPrincipal(httpRequest));
} else {
sessionStat = getSessionStat(httpRequest);
if (sessionStat != null) {
sessionStat.setLastAccessTimeMillis(startMillis);
sessionStat.reacord(nanoSpan);
sessionStat.incrementRequestCount();
sessionStat.setPrincipal(getPrincipal(httpRequest));
}
}
@@ -162,6 +171,33 @@ public class WebStatFilter implements Filter {
return sessionId;
}
public String getPrincipal(HttpServletRequest httpRequest) {
if (principalSessionName != null) {
HttpSession session = httpRequest.getSession(createSession);
if (session == null) {
return null;
}
Object sessionValue = session.getAttribute(principalSessionName);
if (sessionValue == null) {
return null;
}
return sessionValue.toString();
}
if (principalCookieName != null) {
for (Cookie cookie : httpRequest.getCookies()) {
if (principalCookieName.equals(cookie.getName())) {
return cookie.getValue();
}
}
}
return null;
}
public boolean isExclusion(String requestURI) {
if (excludesPattern == null) {
return false;
@@ -187,6 +223,14 @@ public class WebStatFilter implements Filter {
return request.getRequestURI();
}
public String getPrincipalSessionName() {
return principalSessionName;
}
public String getPrincipalCookieName() {
return principalCookieName;
}
@Override
public void init(FilterConfig config) throws ServletException {
{
@@ -195,6 +239,27 @@ public class WebStatFilter implements Filter {
excludesPattern = new HashSet<String>(Arrays.asList(exclusions.split("\\s*,\\s*")));
}
}
{
String param = config.getInitParameter(PARAM_NAME_PRINCIPAL_SESSION_NAME);
if (param != null) {
param = param.trim();
if (param.length() != 0) {
this.principalSessionName = param;
}
}
}
{
String param = config.getInitParameter(PARAM_NAME_PRINCIPAL_COOKIE_NAME);
if (param != null) {
param = param.trim();
if (param.length() != 0) {
this.principalCookieName = param;
}
}
}
{
String param = config.getInitParameter(PARAM_NAME_SESSION_STAT_ENABLE);
if (param != null && param.trim().length() != 0) {