View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.datasource.pooled;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * @author Clinton Begin
23   */
24  public class PoolState {
25  
26    protected PooledDataSource dataSource;
27  
28    protected final List<PooledConnection> idleConnections = new ArrayList<PooledConnection>();
29    protected final List<PooledConnection> activeConnections = new ArrayList<PooledConnection>();
30    protected long requestCount = 0;
31    protected long accumulatedRequestTime = 0;
32    protected long accumulatedCheckoutTime = 0;
33    protected long claimedOverdueConnectionCount = 0;
34    protected long accumulatedCheckoutTimeOfOverdueConnections = 0;
35    protected long accumulatedWaitTime = 0;
36    protected long hadToWaitCount = 0;
37    protected long badConnectionCount = 0;
38  
39    public PoolState(PooledDataSource dataSource) {
40      this.dataSource = dataSource;
41    }
42  
43    public synchronized long getRequestCount() {
44      return requestCount;
45    }
46  
47    public synchronized long getAverageRequestTime() {
48      return requestCount == 0 ? 0 : accumulatedRequestTime / requestCount;
49    }
50  
51    public synchronized long getAverageWaitTime() {
52      return hadToWaitCount == 0 ? 0 : accumulatedWaitTime / hadToWaitCount;
53  
54    }
55  
56    public synchronized long getHadToWaitCount() {
57      return hadToWaitCount;
58    }
59  
60    public synchronized long getBadConnectionCount() {
61      return badConnectionCount;
62    }
63  
64    public synchronized long getClaimedOverdueConnectionCount() {
65      return claimedOverdueConnectionCount;
66    }
67  
68    public synchronized long getAverageOverdueCheckoutTime() {
69      return claimedOverdueConnectionCount == 0 ? 0 : accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount;
70    }
71  
72    public synchronized long getAverageCheckoutTime() {
73      return requestCount == 0 ? 0 : accumulatedCheckoutTime / requestCount;
74    }
75  
76  
77    public synchronized int getIdleConnectionCount() {
78      return idleConnections.size();
79    }
80  
81    public synchronized int getActiveConnectionCount() {
82      return activeConnections.size();
83    }
84  
85    @Override
86    public synchronized String toString() {
87      StringBuilder builder = new StringBuilder();
88      builder.append("\n===CONFINGURATION==============================================");
89      builder.append("\n jdbcDriver                     ").append(dataSource.getDriver());
90      builder.append("\n jdbcUrl                        ").append(dataSource.getUrl());
91      builder.append("\n jdbcUsername                   ").append(dataSource.getUsername());
92      builder.append("\n jdbcPassword                   ").append((dataSource.getPassword() == null ? "NULL" : "************"));
93      builder.append("\n poolMaxActiveConnections       ").append(dataSource.poolMaximumActiveConnections);
94      builder.append("\n poolMaxIdleConnections         ").append(dataSource.poolMaximumIdleConnections);
95      builder.append("\n poolMaxCheckoutTime            ").append(dataSource.poolMaximumCheckoutTime);
96      builder.append("\n poolTimeToWait                 ").append(dataSource.poolTimeToWait);
97      builder.append("\n poolPingEnabled                ").append(dataSource.poolPingEnabled);
98      builder.append("\n poolPingQuery                  ").append(dataSource.poolPingQuery);
99      builder.append("\n poolPingConnectionsNotUsedFor  ").append(dataSource.poolPingConnectionsNotUsedFor);
100     builder.append("\n ---STATUS-----------------------------------------------------");
101     builder.append("\n activeConnections              ").append(getActiveConnectionCount());
102     builder.append("\n idleConnections                ").append(getIdleConnectionCount());
103     builder.append("\n requestCount                   ").append(getRequestCount());
104     builder.append("\n averageRequestTime             ").append(getAverageRequestTime());
105     builder.append("\n averageCheckoutTime            ").append(getAverageCheckoutTime());
106     builder.append("\n claimedOverdue                 ").append(getClaimedOverdueConnectionCount());
107     builder.append("\n averageOverdueCheckoutTime     ").append(getAverageOverdueCheckoutTime());
108     builder.append("\n hadToWait                      ").append(getHadToWaitCount());
109     builder.append("\n averageWaitTime                ").append(getAverageWaitTime());
110     builder.append("\n badConnectionCount             ").append(getBadConnectionCount());
111     builder.append("\n===============================================================");
112     return builder.toString();
113   }
114 
115 }