View Javadoc
1   /*
2    *    Copyright 2009-2021 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<>();
29    protected final List<PooledConnection> activeConnections = new ArrayList<>();
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    public synchronized int getIdleConnectionCount() {
77      return idleConnections.size();
78    }
79  
80    public synchronized int getActiveConnectionCount() {
81      return activeConnections.size();
82    }
83  
84    @Override
85    public synchronized String toString() {
86      StringBuilder builder = new StringBuilder();
87      builder.append("\n===CONFIGURATION==============================================");
88      builder.append("\n jdbcDriver                     ").append(dataSource.getDriver());
89      builder.append("\n jdbcUrl                        ").append(dataSource.getUrl());
90      builder.append("\n jdbcUsername                   ").append(dataSource.getUsername());
91      builder.append("\n jdbcPassword                   ").append(dataSource.getPassword() == null ? "NULL" : "************");
92      builder.append("\n poolMaxActiveConnections       ").append(dataSource.poolMaximumActiveConnections);
93      builder.append("\n poolMaxIdleConnections         ").append(dataSource.poolMaximumIdleConnections);
94      builder.append("\n poolMaxCheckoutTime            ").append(dataSource.poolMaximumCheckoutTime);
95      builder.append("\n poolTimeToWait                 ").append(dataSource.poolTimeToWait);
96      builder.append("\n poolPingEnabled                ").append(dataSource.poolPingEnabled);
97      builder.append("\n poolPingQuery                  ").append(dataSource.poolPingQuery);
98      builder.append("\n poolPingConnectionsNotUsedFor  ").append(dataSource.poolPingConnectionsNotUsedFor);
99      builder.append("\n ---STATUS-----------------------------------------------------");
100     builder.append("\n activeConnections              ").append(getActiveConnectionCount());
101     builder.append("\n idleConnections                ").append(getIdleConnectionCount());
102     builder.append("\n requestCount                   ").append(getRequestCount());
103     builder.append("\n averageRequestTime             ").append(getAverageRequestTime());
104     builder.append("\n averageCheckoutTime            ").append(getAverageCheckoutTime());
105     builder.append("\n claimedOverdue                 ").append(getClaimedOverdueConnectionCount());
106     builder.append("\n averageOverdueCheckoutTime     ").append(getAverageOverdueCheckoutTime());
107     builder.append("\n hadToWait                      ").append(getHadToWaitCount());
108     builder.append("\n averageWaitTime                ").append(getAverageWaitTime());
109     builder.append("\n badConnectionCount             ").append(getBadConnectionCount());
110     builder.append("\n===============================================================");
111     return builder.toString();
112   }
113 
114 }