1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbcp2.datasources;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.sql.Connection;
22 import java.sql.SQLException;
23
24 import javax.naming.NamingException;
25 import javax.naming.Reference;
26 import javax.naming.StringRefAddr;
27 import javax.sql.ConnectionPoolDataSource;
28
29 import org.apache.commons.pool2.KeyedObjectPool;
30 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
31 import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class SharedPoolDataSource extends InstanceKeyDataSource {
51
52 private static final long serialVersionUID = -1458539734480586454L;
53
54
55 private int maxTotal = GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL;
56
57 private transient KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool;
58 private transient KeyedCPDSConnectionFactory factory;
59
60
61
62
63 public SharedPoolDataSource() {
64
65 }
66
67
68
69
70 @Override
71 public void close() throws SQLException {
72 if (pool != null) {
73 pool.close();
74 }
75 InstanceKeyDataSourceFactory.removeInstance(getInstanceKey());
76 }
77
78 @Override
79 protected PooledConnectionManager getConnectionManager(final UserPassKey userPassKey) {
80 return factory;
81 }
82
83
84
85
86
87
88 public int getMaxTotal() {
89 return this.maxTotal;
90 }
91
92
93
94
95
96
97
98
99
100 public int getNumActive() {
101 return pool == null ? 0 : pool.getNumActive();
102 }
103
104
105
106
107
108
109 public int getNumIdle() {
110 return pool == null ? 0 : pool.getNumIdle();
111 }
112
113 @Override
114 protected PooledConnectionAndInfo getPooledConnectionAndInfo(final String userName, final String userPassword)
115 throws SQLException {
116
117 synchronized (this) {
118 if (pool == null) {
119 try {
120 registerPool(userName, userPassword);
121 } catch (final NamingException e) {
122 throw new SQLException("registerPool failed", e);
123 }
124 }
125 }
126
127 try {
128 return pool.borrowObject(new UserPassKey(userName, userPassword));
129 } catch (final Exception e) {
130 throw new SQLException("Could not retrieve connection info from pool", e);
131 }
132 }
133
134
135
136
137 @Override
138 public Reference getReference() throws NamingException {
139 final Reference ref = new Reference(getClass().getName(), SharedPoolDataSourceFactory.class.getName(), null);
140 ref.add(new StringRefAddr("instanceKey", getInstanceKey()));
141 return ref;
142 }
143
144
145
146
147
148
149
150
151
152
153
154 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
155 try {
156 in.defaultReadObject();
157 final SharedPoolDataSource oldDS = (SharedPoolDataSource) new SharedPoolDataSourceFactory().getObjectInstance(getReference(), null, null, null);
158 this.pool = oldDS.pool;
159 } catch (final NamingException e) {
160 throw new IOException("NamingException: " + e);
161 }
162 }
163
164 private void registerPool(final String userName, final String password) throws NamingException, SQLException {
165
166 final ConnectionPoolDataSource cpds = testCPDS(userName, password);
167
168
169 factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeoutDuration(), isRollbackAfterValidation());
170 factory.setMaxConn(getMaxConnDuration());
171
172 final GenericKeyedObjectPoolConfig<PooledConnectionAndInfo> config = new GenericKeyedObjectPoolConfig<>();
173 config.setBlockWhenExhausted(getDefaultBlockWhenExhausted());
174 config.setEvictionPolicyClassName(getDefaultEvictionPolicyClassName());
175 config.setLifo(getDefaultLifo());
176 config.setMaxIdlePerKey(getDefaultMaxIdle());
177 config.setMaxTotal(getMaxTotal());
178 config.setMaxTotalPerKey(getDefaultMaxTotal());
179 config.setMaxWait(getDefaultMaxWait());
180 config.setMinEvictableIdleDuration(getDefaultMinEvictableIdleDuration());
181 config.setMinIdlePerKey(getDefaultMinIdle());
182 config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
183 config.setSoftMinEvictableIdleDuration(getDefaultSoftMinEvictableIdleDuration());
184 config.setTestOnCreate(getDefaultTestOnCreate());
185 config.setTestOnBorrow(getDefaultTestOnBorrow());
186 config.setTestOnReturn(getDefaultTestOnReturn());
187 config.setTestWhileIdle(getDefaultTestWhileIdle());
188 config.setTimeBetweenEvictionRuns(getDefaultDurationBetweenEvictionRuns());
189
190 final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory, config);
191 factory.setPool(tmpPool);
192 pool = tmpPool;
193 }
194
195
196
197
198
199
200
201 public void setMaxTotal(final int maxTotal) {
202 assertInitializationAllowed();
203 this.maxTotal = maxTotal;
204 }
205
206 @Override
207 protected void setupDefaults(final Connection connection, final String userName) throws SQLException {
208 final Boolean defaultAutoCommit = isDefaultAutoCommit();
209 if (defaultAutoCommit != null && connection.getAutoCommit() != defaultAutoCommit) {
210 connection.setAutoCommit(defaultAutoCommit);
211 }
212
213 final int defaultTransactionIsolation = getDefaultTransactionIsolation();
214 if (defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION) {
215 connection.setTransactionIsolation(defaultTransactionIsolation);
216 }
217
218 final Boolean defaultReadOnly = isDefaultReadOnly();
219 if (defaultReadOnly != null && connection.isReadOnly() != defaultReadOnly) {
220 connection.setReadOnly(defaultReadOnly);
221 }
222 }
223
224 @Override
225 protected void toStringFields(final StringBuilder builder) {
226 super.toStringFields(builder);
227 builder.append(", maxTotal=");
228 builder.append(maxTotal);
229 }
230 }