1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.datasource.pooled;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21
22
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 }