1818import java .util .ArrayList ;
1919import java .util .List ;
2020
21+ import org .apache .ibatis .util .LockKit ;
22+
2123/**
2224 * @author Clinton Begin
2325 */
2426public class PoolState {
2527
28+ private final LockKit .ReentrantLock reentrantLock ;
29+
2630 protected PooledDataSource dataSource ;
2731
2832 protected final List <PooledConnection > idleConnections = new ArrayList <>();
@@ -38,79 +42,134 @@ public class PoolState {
3842
3943 public PoolState (PooledDataSource dataSource ) {
4044 this .dataSource = dataSource ;
45+ this .reentrantLock = LockKit .obtainLock (dataSource );
4146 }
4247
43- public synchronized long getRequestCount () {
44- return requestCount ;
48+ public long getRequestCount () {
49+ reentrantLock .lock ();
50+ try {
51+ return requestCount ;
52+ } finally {
53+ reentrantLock .unlock ();
54+ }
4555 }
4656
47- public synchronized long getAverageRequestTime () {
48- return requestCount == 0 ? 0 : accumulatedRequestTime / requestCount ;
57+ public long getAverageRequestTime () {
58+ reentrantLock .lock ();
59+ try {
60+ return requestCount == 0 ? 0 : accumulatedRequestTime / requestCount ;
61+ } finally {
62+ reentrantLock .unlock ();
63+ }
4964 }
5065
51- public synchronized long getAverageWaitTime () {
52- return hadToWaitCount == 0 ? 0 : accumulatedWaitTime / hadToWaitCount ;
53-
66+ public long getAverageWaitTime () {
67+ reentrantLock .lock ();
68+ try {
69+ return hadToWaitCount == 0 ? 0 : accumulatedWaitTime / hadToWaitCount ;
70+ } finally {
71+ reentrantLock .unlock ();
72+ }
5473 }
5574
56- public synchronized long getHadToWaitCount () {
57- return hadToWaitCount ;
75+ public long getHadToWaitCount () {
76+ reentrantLock .lock ();
77+ try {
78+ return hadToWaitCount ;
79+ } finally {
80+ reentrantLock .unlock ();
81+ }
5882 }
5983
60- public synchronized long getBadConnectionCount () {
61- return badConnectionCount ;
84+ public long getBadConnectionCount () {
85+ reentrantLock .lock ();
86+ try {
87+ return badConnectionCount ;
88+ } finally {
89+ reentrantLock .unlock ();
90+ }
6291 }
6392
64- public synchronized long getClaimedOverdueConnectionCount () {
65- return claimedOverdueConnectionCount ;
93+ public long getClaimedOverdueConnectionCount () {
94+ reentrantLock .lock ();
95+ try {
96+ return claimedOverdueConnectionCount ;
97+ } finally {
98+ reentrantLock .unlock ();
99+ }
66100 }
67101
68- public synchronized long getAverageOverdueCheckoutTime () {
69- return claimedOverdueConnectionCount == 0 ? 0
70- : accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount ;
102+ public long getAverageOverdueCheckoutTime () {
103+ reentrantLock .lock ();
104+ try {
105+ return claimedOverdueConnectionCount == 0 ? 0
106+ : accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount ;
107+ } finally {
108+ reentrantLock .unlock ();
109+ }
71110 }
72111
73- public synchronized long getAverageCheckoutTime () {
74- return requestCount == 0 ? 0 : accumulatedCheckoutTime / requestCount ;
112+ public long getAverageCheckoutTime () {
113+ reentrantLock .lock ();
114+ try {
115+ return requestCount == 0 ? 0 : accumulatedCheckoutTime / requestCount ;
116+ } finally {
117+ reentrantLock .unlock ();
118+ }
75119 }
76120
77- public synchronized int getIdleConnectionCount () {
78- return idleConnections .size ();
121+ public int getIdleConnectionCount () {
122+ reentrantLock .lock ();
123+ try {
124+ return idleConnections .size ();
125+ } finally {
126+ reentrantLock .unlock ();
127+ }
79128 }
80129
81- public synchronized int getActiveConnectionCount () {
82- return activeConnections .size ();
130+ public int getActiveConnectionCount () {
131+ reentrantLock .lock ();
132+ try {
133+ return activeConnections .size ();
134+ } finally {
135+ reentrantLock .unlock ();
136+ }
83137 }
84138
85139 @ 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 ();
140+ public String toString () {
141+ reentrantLock .lock ();
142+ try {
143+ StringBuilder builder = new StringBuilder ();
144+ builder .append ("\n ===CONFIGURATION==============================================" );
145+ builder .append ("\n jdbcDriver " ).append (dataSource .getDriver ());
146+ builder .append ("\n jdbcUrl " ).append (dataSource .getUrl ());
147+ builder .append ("\n jdbcUsername " ).append (dataSource .getUsername ());
148+ builder .append ("\n jdbcPassword " )
149+ .append (dataSource .getPassword () == null ? "NULL" : "************" );
150+ builder .append ("\n poolMaxActiveConnections " ).append (dataSource .poolMaximumActiveConnections );
151+ builder .append ("\n poolMaxIdleConnections " ).append (dataSource .poolMaximumIdleConnections );
152+ builder .append ("\n poolMaxCheckoutTime " ).append (dataSource .poolMaximumCheckoutTime );
153+ builder .append ("\n poolTimeToWait " ).append (dataSource .poolTimeToWait );
154+ builder .append ("\n poolPingEnabled " ).append (dataSource .poolPingEnabled );
155+ builder .append ("\n poolPingQuery " ).append (dataSource .poolPingQuery );
156+ builder .append ("\n poolPingConnectionsNotUsedFor " ).append (dataSource .poolPingConnectionsNotUsedFor );
157+ builder .append ("\n ---STATUS-----------------------------------------------------" );
158+ builder .append ("\n activeConnections " ).append (getActiveConnectionCount ());
159+ builder .append ("\n idleConnections " ).append (getIdleConnectionCount ());
160+ builder .append ("\n requestCount " ).append (getRequestCount ());
161+ builder .append ("\n averageRequestTime " ).append (getAverageRequestTime ());
162+ builder .append ("\n averageCheckoutTime " ).append (getAverageCheckoutTime ());
163+ builder .append ("\n claimedOverdue " ).append (getClaimedOverdueConnectionCount ());
164+ builder .append ("\n averageOverdueCheckoutTime " ).append (getAverageOverdueCheckoutTime ());
165+ builder .append ("\n hadToWait " ).append (getHadToWaitCount ());
166+ builder .append ("\n averageWaitTime " ).append (getAverageWaitTime ());
167+ builder .append ("\n badConnectionCount " ).append (getBadConnectionCount ());
168+ builder .append ("\n ===============================================================" );
169+ return builder .toString ();
170+ } finally {
171+ reentrantLock .unlock ();
172+ }
114173 }
115174
116175}
0 commit comments