View | Details | Raw Unified | Return to bug 56318
Collapse All | Expand All

(-)org/apache/tomcat/jdbc/pool/interceptor/StatementFinalizer.java (-10 / +46 lines)
Lines 20-29 Link Here
20
import java.lang.reflect.Method;
20
import java.lang.reflect.Method;
21
import java.sql.Statement;
21
import java.sql.Statement;
22
import java.util.ArrayList;
22
import java.util.ArrayList;
23
import java.util.Map;
23
24
24
import org.apache.juli.logging.Log;
25
import org.apache.juli.logging.Log;
25
import org.apache.juli.logging.LogFactory;
26
import org.apache.juli.logging.LogFactory;
26
import org.apache.tomcat.jdbc.pool.ConnectionPool;
27
import org.apache.tomcat.jdbc.pool.ConnectionPool;
28
import org.apache.tomcat.jdbc.pool.PoolProperties;
27
import org.apache.tomcat.jdbc.pool.PooledConnection;
29
import org.apache.tomcat.jdbc.pool.PooledConnection;
28
/**
30
/**
29
 * Keeps track of statements associated with a connection and invokes close upon {@link java.sql.Connection#close()}
31
 * Keeps track of statements associated with a connection and invokes close upon {@link java.sql.Connection#close()}
Lines 34-48 Link Here
34
public class StatementFinalizer extends AbstractCreateStatementInterceptor {
36
public class StatementFinalizer extends AbstractCreateStatementInterceptor {
35
    private static final Log log = LogFactory.getLog(StatementFinalizer.class);
37
    private static final Log log = LogFactory.getLog(StatementFinalizer.class);
36
38
37
    protected ArrayList<WeakReference<Statement>> statements = new ArrayList<WeakReference<Statement>>();
39
    private boolean logCreationStack = false;
38
40
41
    protected ArrayList<WeakReference<StatementEntry>> statements = new ArrayList<WeakReference<StatementEntry>>();
42
39
    @Override
43
    @Override
40
    public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time) {
44
    public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time) {
41
        try {
45
        if (statement instanceof Statement) {
42
            if (statement instanceof Statement)
46
            statements.add(new WeakReference<StatementEntry>(new StatementEntry((Statement) statement)));
43
                statements.add(new WeakReference<Statement>((Statement)statement));
44
        }catch (ClassCastException x) {
45
            //ignore this one
46
        }
47
        }
47
        return statement;
48
        return statement;
48
    }
49
    }
Lines 50-60 Link Here
50
    @Override
51
    @Override
51
    public void closeInvoked() {
52
    public void closeInvoked() {
52
        while (statements.size()>0) {
53
        while (statements.size()>0) {
53
            WeakReference<Statement> ws = statements.remove(0);
54
            WeakReference<StatementEntry> ws = statements.remove(0);
54
            Statement st = ws.get();
55
            StatementEntry se = ws.get();
55
            if (st!=null) {
56
            if (se!=null) {
57
                Statement st = se.getStatement();
56
                try {
58
                try {
57
                    st.close();
59
                    if (!st.isClosed()) {
60
                        if (logCreationStack) {
61
                            log.warn("Statement created, but was not closed ", se.getAllocationStack());
62
                        }
63
                        st.close();
64
                    }
58
                } catch (Exception ignore) {
65
                } catch (Exception ignore) {
59
                    if (log.isDebugEnabled()) {
66
                    if (log.isDebugEnabled()) {
60
                        log.debug("Unable to closed statement upon connection close.",ignore);
67
                        log.debug("Unable to closed statement upon connection close.",ignore);
Lines 65-70 Link Here
65
    }
72
    }
66
73
67
    @Override
74
    @Override
75
    public void setProperties(Map<String, PoolProperties.InterceptorProperty> properties) {
76
        super.setProperties(properties);
77
78
        PoolProperties.InterceptorProperty logProperty = properties.get("logCreationStack");
79
        if (null != logProperty) {
80
            logCreationStack = logProperty.getValueAsBoolean(logCreationStack);
81
        }
82
    }
83
84
    @Override
68
    public void reset(ConnectionPool parent, PooledConnection con) {
85
    public void reset(ConnectionPool parent, PooledConnection con) {
69
        statements.clear();
86
        statements.clear();
70
        super.reset(parent, con);
87
        super.reset(parent, con);
Lines 71-74 Link Here
71
    }
88
    }
72
89
73
90
91
    protected class StatementEntry {
92
        private Statement statement;
93
        private Throwable allocationStack;
94
95
        public StatementEntry(Statement statement) {
96
            this.statement = statement;
97
            if (logCreationStack) {
98
                this.allocationStack = new Throwable();
99
            }
100
        }
101
102
        public Statement getStatement() {
103
            return statement;
104
        }
105
106
        public Throwable getAllocationStack() {
107
            return allocationStack;
108
        }
109
    }
74
}
110
}

Return to bug 56318