File: PoolableCallableStatement.java

package info (click to toggle)
tomcat8 8.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 33,280 kB
  • sloc: java: 303,023; xml: 46,103; jsp: 3,182; sh: 1,358; perl: 269; makefile: 114
file content (130 lines) | stat: -rw-r--r-- 4,613 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.tomcat.dbcp.dbcp2;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.tomcat.dbcp.pool2.KeyedObjectPool;

/**
 * A {@link DelegatingCallableStatement} that cooperates with
 * {@link PoolingConnection} to implement a pool of {@link CallableStatement}s.
 * <p>
 * The {@link #close} method returns this statement to its containing pool. (See {@link PoolingConnection}.)
 *
 * @see PoolingConnection
 * @since 2.0
 */
public class PoolableCallableStatement extends DelegatingCallableStatement {

    /**
     * The {@link KeyedObjectPool} from which this CallableStatement was obtained.
     */
    private final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> _pool;

    /**
     * Key for this statement in the containing {@link KeyedObjectPool}.
     */
    private final PStmtKey _key;

    /**
     * Constructor.
     *
     * @param stmt the underlying {@link CallableStatement}
     * @param key the key for this statement in the {@link KeyedObjectPool}
     * @param pool the {@link KeyedObjectPool} from which this CallableStatement was obtained
     * @param conn the {@link DelegatingConnection} that created this CallableStatement
     */
    public PoolableCallableStatement(CallableStatement stmt, PStmtKey key,
            KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool,
            DelegatingConnection<Connection> conn) {
        super(conn, stmt);
        _pool = pool;
        _key = key;

        // Remove from trace now because this statement will be
        // added by the activate method.
        if(getConnectionInternal() != null) {
            getConnectionInternal().removeTrace(this);
        }
    }

    /**
     * Returns the CallableStatement to the pool.  If {{@link #isClosed()}, this is a No-op.
     */
    @Override
    public void close() throws SQLException {
        // calling close twice should have no effect
        if (!isClosed()) {
            try {
                _pool.returnObject(_key,this);
            } catch(SQLException e) {
                throw e;
            } catch(RuntimeException e) {
                throw e;
            } catch(Exception e) {
                throw new SQLException("Cannot close CallableStatement (return to pool failed)", e);
            }
        }
    }

    /**
     * Activates after retrieval from the pool. Adds a trace for this CallableStatement to the Connection
     * that created it.
     */
    @Override
    protected void activate() throws SQLException {
        setClosedInternal(false);
        if( getConnectionInternal() != null ) {
            getConnectionInternal().addTrace( this );
        }
        super.activate();
    }

    /**
     * Passivates to prepare for return to the pool.  Removes the trace associated with this CallableStatement
     * from the Connection that created it.  Also closes any associated ResultSets.
     */
    @Override
    protected void passivate() throws SQLException {
        setClosedInternal(true);
        if( getConnectionInternal() != null ) {
            getConnectionInternal().removeTrace(this);
        }

        // The JDBC spec requires that a statment close any open
        // ResultSet's when it is closed.
        // FIXME The PreparedStatement we're wrapping should handle this for us.
        // See DBCP-10 for what could happen when ResultSets are closed twice.
        List<AbandonedTrace> resultSets = getTrace();
        if(resultSets != null) {
            ResultSet[] set = resultSets.toArray(new ResultSet[resultSets.size()]);
            for (ResultSet element : set) {
                element.close();
            }
            clearTrace();
        }

        super.passivate();
    }

}