View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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;
31    protected long accumulatedRequestTime;
32    protected long accumulatedCheckoutTime;
33    protected long claimedOverdueConnectionCount;
34    protected long accumulatedCheckoutTimeOfOverdueConnections;
35    protected long accumulatedWaitTime;
36    protected long hadToWaitCount;
37    protected long badConnectionCount;
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
70          : accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount;
71    }
72  
73    public synchronized long getAverageCheckoutTime() {
74      return requestCount == 0 ? 0 : accumulatedCheckoutTime / requestCount;
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===CONFIGURATION==============================================");
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                   ")
93          .append(dataSource.getPassword() == null ? "NULL" : "************");
94      builder.append("\n poolMaxActiveConnections       ").append(dataSource.poolMaximumActiveConnections);
95      builder.append("\n poolMaxIdleConnections         ").append(dataSource.poolMaximumIdleConnections);
96      builder.append("\n poolMaxCheckoutTime            ").append(dataSource.poolMaximumCheckoutTime);
97      builder.append("\n poolTimeToWait                 ").append(dataSource.poolTimeToWait);
98      builder.append("\n poolPingEnabled                ").append(dataSource.poolPingEnabled);
99      builder.append("\n poolPingQuery                  ").append(dataSource.poolPingQuery);
100     builder.append("\n poolPingConnectionsNotUsedFor  ").append(dataSource.poolPingConnectionsNotUsedFor);
101     builder.append("\n ---STATUS-----------------------------------------------------");
102     builder.append("\n activeConnections              ").append(getActiveConnectionCount());
103     builder.append("\n idleConnections                ").append(getIdleConnectionCount());
104     builder.append("\n requestCount                   ").append(getRequestCount());
105     builder.append("\n averageRequestTime             ").append(getAverageRequestTime());
106     builder.append("\n averageCheckoutTime            ").append(getAverageCheckoutTime());
107     builder.append("\n claimedOverdue                 ").append(getClaimedOverdueConnectionCount());
108     builder.append("\n averageOverdueCheckoutTime     ").append(getAverageOverdueCheckoutTime());
109     builder.append("\n hadToWait                      ").append(getHadToWaitCount());
110     builder.append("\n averageWaitTime                ").append(getAverageWaitTime());
111     builder.append("\n badConnectionCount             ").append(getBadConnectionCount());
112     builder.append("\n===============================================================");
113     return builder.toString();
114   }
115 
116 }