View Javadoc
1   /*
2    *    Copyright 2009-2025 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.mybatis.guice.datasource.dbcp;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Named;
20  import jakarta.inject.Provider;
21  
22  import java.time.Duration;
23  import java.util.Map.Entry;
24  import java.util.Properties;
25  
26  import javax.sql.DataSource;
27  
28  import org.apache.commons.dbcp2.BasicDataSource;
29  
30  /**
31   * Provides the Apache commons-dbcp2 {@code BasicDataSource}.
32   */
33  public final class BasicDataSourceProvider implements Provider<DataSource> {
34  
35    /**
36     * The BasicDataSource reference.
37     */
38    private final BasicDataSource dataSource = new BasicDataSource();
39  
40    /**
41     * Creates a new BasicDataSource using the needed parameter.
42     *
43     * @param driver
44     *          The JDBC driver class.
45     * @param url
46     *          the database URL of the form <code>jdbc:subprotocol:subname</code>.
47     * @param driverClassLoader
48     *          ClassLoader to use to load JDBC driver class.
49     */
50    @Inject
51    public BasicDataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url,
52        @Named("JDBC.driverClassLoader") final ClassLoader driverClassLoader) {
53      dataSource.setDriverClassLoader(driverClassLoader);
54      dataSource.setDriverClassName(driver);
55      dataSource.setUrl(url);
56    }
57  
58    /**
59     * Sets the user.
60     *
61     * @param username
62     *          the new user
63     *
64     * @since 3.3
65     */
66    @com.google.inject.Inject(optional = true)
67    public void setUser(@Named("JDBC.username") final String username) {
68      dataSource.setUsername(username);
69    }
70  
71    /**
72     * Sets the password.
73     *
74     * @param password
75     *          the new password
76     *
77     * @since 3.3
78     */
79    @com.google.inject.Inject(optional = true)
80    public void setPassword(@Named("JDBC.password") final String password) {
81      dataSource.setPassword(password);
82    }
83  
84    /**
85     * Sets the auto commit.
86     *
87     * @param autoCommit
88     *          the new auto commit
89     */
90    @com.google.inject.Inject(optional = true)
91    public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
92      dataSource.setDefaultAutoCommit(autoCommit);
93    }
94  
95    /**
96     * Sets the driver properties.
97     *
98     * @param driverProperties
99     *          the new driver properties
100    */
101   @com.google.inject.Inject(optional = true)
102   public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
103     for (Entry<Object, Object> property : driverProperties.entrySet()) {
104       String name = property.getKey().toString();
105       String value = property.getValue().toString();
106       dataSource.addConnectionProperty(name, value);
107     }
108   }
109 
110   /**
111    * Sets the access to underlying connection allowed.
112    *
113    * @param allow
114    *          the new access to underlying connection allowed
115    */
116   @com.google.inject.Inject(optional = true)
117   public void setAccessToUnderlyingConnectionAllowed(
118       @Named("DBCP.accessToUnderlyingConnectionAllowed") final boolean allow) {
119     dataSource.setAccessToUnderlyingConnectionAllowed(allow);
120   }
121 
122   /**
123    * Sets the default catalog.
124    *
125    * @param defaultCatalog
126    *          the new default catalog
127    */
128   @com.google.inject.Inject(optional = true)
129   public void setDefaultCatalog(@Named("DBCP.defaultCatalog") final String defaultCatalog) {
130     dataSource.setDefaultCatalog(defaultCatalog);
131   }
132 
133   /**
134    * Sets the default read only.
135    *
136    * @param defaultReadOnly
137    *          the new default read only
138    */
139   @com.google.inject.Inject(optional = true)
140   public void setDefaultReadOnly(@Named("DBCP.defaultReadOnly") final boolean defaultReadOnly) {
141     dataSource.setDefaultReadOnly(defaultReadOnly);
142   }
143 
144   /**
145    * Sets the default transaction isolation.
146    *
147    * @param defaultTransactionIsolation
148    *          the new default transaction isolation
149    */
150   @com.google.inject.Inject(optional = true)
151   public void setDefaultTransactionIsolation(
152       @Named("DBCP.defaultTransactionIsolation") final int defaultTransactionIsolation) {
153     dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
154   }
155 
156   /**
157    * Sets the initial size.
158    *
159    * @param initialSize
160    *          the new initial size
161    */
162   @com.google.inject.Inject(optional = true)
163   public void setInitialSize(@Named("DBCP.initialSize") final int initialSize) {
164     dataSource.setInitialSize(initialSize);
165   }
166 
167   /**
168    * Sets the max total.
169    *
170    * @param maxTotal
171    *          the new max total
172    */
173   @com.google.inject.Inject(optional = true)
174   public void setMaxTotal(@Named("DBCP.maxTotal") final int maxTotal) {
175     dataSource.setMaxTotal(maxTotal);
176   }
177 
178   /**
179    * Sets the max idle.
180    *
181    * @param maxIdle
182    *          the new max idle
183    */
184   @com.google.inject.Inject(optional = true)
185   public void setMaxIdle(@Named("DBCP.maxIdle") final int maxIdle) {
186     dataSource.setMaxIdle(maxIdle);
187   }
188 
189   /**
190    * Sets the max open prepared statements.
191    *
192    * @param maxOpenPreparedStatements
193    *          the new max open prepared statements
194    */
195   @com.google.inject.Inject(optional = true)
196   public void setMaxOpenPreparedStatements(
197       @Named("DBCP.maxOpenPreparedStatements") final int maxOpenPreparedStatements) {
198     dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
199   }
200 
201   /**
202    * Sets the max wait in milliseconds.
203    *
204    * @param maxWaitMillis
205    *          the new max wait in milliseconds
206    */
207   @com.google.inject.Inject(optional = true)
208   public void setMaxWaitMillis(@Named("DBCP.maxWaitMillis") final long maxWaitMillis) {
209     dataSource.setMaxWait(Duration.ofMillis(maxWaitMillis));
210   }
211 
212   /**
213    * Sets the min evictable idle time millis.
214    *
215    * @param minEvictableIdleTimeMillis
216    *          the new min evictable idle time millis
217    */
218   @com.google.inject.Inject(optional = true)
219   public void setMinEvictableIdleTimeMillis(
220       @Named("DBCP.minEvictableIdleTimeMillis") final long minEvictableIdleTimeMillis) {
221     dataSource.setMinEvictableIdle(Duration.ofMillis(minEvictableIdleTimeMillis));
222   }
223 
224   /**
225    * Sets the min idle.
226    *
227    * @param minIdle
228    *          the new min idle
229    */
230   @com.google.inject.Inject(optional = true)
231   public void setMinIdle(@Named("DBCP.minIdle") final int minIdle) {
232     dataSource.setMinIdle(minIdle);
233   }
234 
235   /**
236    * Sets the num tests per eviction run.
237    *
238    * @param numTestsPerEvictionRun
239    *          the new num tests per eviction run
240    */
241   @com.google.inject.Inject(optional = true)
242   public void setNumTestsPerEvictionRun(@Named("DBCP.numTestsPerEvictionRun") final int numTestsPerEvictionRun) {
243     dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
244   }
245 
246   /**
247    * Sets the pool prepared statements.
248    *
249    * @param poolPreparedStatements
250    *          the new pool prepared statements
251    */
252   @com.google.inject.Inject(optional = true)
253   public void setPoolPreparedStatements(@Named("DBCP.poolPreparedStatements") final boolean poolPreparedStatements) {
254     dataSource.setPoolPreparedStatements(poolPreparedStatements);
255   }
256 
257   /**
258    * Sets the test on borrow.
259    *
260    * @param testOnBorrow
261    *          the new test on borrow
262    */
263   @com.google.inject.Inject(optional = true)
264   public void setTestOnBorrow(@Named("DBCP.testOnBorrow") final boolean testOnBorrow) {
265     dataSource.setTestOnBorrow(testOnBorrow);
266   }
267 
268   /**
269    * Sets the test on return.
270    *
271    * @param testOnReturn
272    *          the new test on return
273    */
274   @com.google.inject.Inject(optional = true)
275   public void setTestOnReturn(@Named("DBCP.testOnReturn") final boolean testOnReturn) {
276     dataSource.setTestOnReturn(testOnReturn);
277   }
278 
279   /**
280    * Sets the test while idle.
281    *
282    * @param testWhileIdle
283    *          the new test while idle
284    */
285   @com.google.inject.Inject(optional = true)
286   public void setTestWhileIdle(@Named("DBCP.testWhileIdle") final boolean testWhileIdle) {
287     dataSource.setTestWhileIdle(testWhileIdle);
288   }
289 
290   /**
291    * Sets the time between eviction runs millis.
292    *
293    * @param timeBetweenEvictionRunsMillis
294    *          the new time between eviction runs millis
295    */
296   @com.google.inject.Inject(optional = true)
297   public void setTimeBetweenEvictionRunsMillis(
298       @Named("DBCP.timeBetweenEvictionRunsMillis") int timeBetweenEvictionRunsMillis) {
299     dataSource.setDurationBetweenEvictionRuns(Duration.ofMillis(timeBetweenEvictionRunsMillis));
300   }
301 
302   /**
303    * Sets the validation query.
304    *
305    * @param validationQuery
306    *          the new validation query
307    */
308   @com.google.inject.Inject(optional = true)
309   public void setValidationQuery(@Named("DBCP.validationQuery") final String validationQuery) {
310     dataSource.setValidationQuery(validationQuery);
311   }
312 
313   @Override
314   public DataSource get() {
315     return dataSource;
316   }
317 
318 }