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.mybatis.guice.datasource.c3p0;
17  
18  import com.mchange.v2.c3p0.ComboPooledDataSource;
19  
20  import jakarta.inject.Inject;
21  import jakarta.inject.Named;
22  import jakarta.inject.Provider;
23  
24  import java.beans.PropertyVetoException;
25  import java.util.Properties;
26  
27  import javax.sql.DataSource;
28  
29  /**
30   * Provides the C3P0 DataSource.
31   */
32  public final class C3p0DataSourceProvider implements Provider<DataSource> {
33  
34    /**
35     * The ComboPooledDataSource reference.
36     */
37    private final ComboPooledDataSource dataSource = new ComboPooledDataSource();
38    private String username;
39    private String password;
40  
41    /**
42     * Creates a new ComboPooledDataSource using the needed parameter.
43     *
44     * @param driver
45     *          The JDBC driver class.
46     * @param url
47     *          the database URL of the form <code>jdbc:subprotocol:subname</code>.
48     */
49    @Inject
50    public C3p0DataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url) {
51      try {
52        dataSource.setDriverClass(driver);
53      } catch (PropertyVetoException e) {
54        throw new RuntimeException(
55            "Impossible to initialize C3P0 Data Source with driver class '" + driver + "', see nested exceptions", e);
56      }
57      dataSource.setJdbcUrl(url);
58    }
59  
60    /**
61     * Sets the user.
62     *
63     * @param username
64     *          the new user
65     *
66     * @since 3.3
67     */
68    @com.google.inject.Inject(optional = true)
69    public void setUser(@Named("JDBC.username") final String username) {
70      this.username = username;
71    }
72  
73    /**
74     * Sets the password.
75     *
76     * @param password
77     *          the new password
78     *
79     * @since 3.3
80     */
81    @com.google.inject.Inject(optional = true)
82    public void setPassword(@Named("JDBC.password") final String password) {
83      this.password = password;
84    }
85  
86    /**
87     * Sets the acquire increment.
88     *
89     * @param acquireIncrement
90     *          the new acquire increment
91     */
92    @com.google.inject.Inject(optional = true)
93    public void setAcquireIncrement(@Named("c3p0.acquireIncrement") final int acquireIncrement) {
94      dataSource.setAcquireIncrement(acquireIncrement);
95    }
96  
97    /**
98     * Sets the acquire retry attempts.
99     *
100    * @param acquireRetryAttempts
101    *          the new acquire retry attempts
102    */
103   @com.google.inject.Inject(optional = true)
104   public void setAcquireRetryAttempts(@Named("c3p0.acquireRetryAttempts") final int acquireRetryAttempts) {
105     dataSource.setAcquireRetryAttempts(acquireRetryAttempts);
106   }
107 
108   /**
109    * Sets the acquire retry delay.
110    *
111    * @param acquireRetryDelay
112    *          the new acquire retry delay
113    */
114   @com.google.inject.Inject(optional = true)
115   public void setAcquireRetryDelay(@Named("c3p0.acquireRetryDelay") final int acquireRetryDelay) {
116     dataSource.setAcquireRetryDelay(acquireRetryDelay);
117   }
118 
119   /**
120    * Sets the auto commit on close.
121    *
122    * @param autoCommit
123    *          the new auto commit on close
124    */
125   @com.google.inject.Inject(optional = true)
126   public void setAutoCommitOnClose(@Named("JDBC.autoCommit") final boolean autoCommit) {
127     dataSource.setAutoCommitOnClose(autoCommit);
128   }
129 
130   /**
131    * Sets the driver properties.
132    *
133    * @param driverProperties
134    *          the new driver properties
135    */
136   @com.google.inject.Inject(optional = true)
137   public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
138     dataSource.setProperties(driverProperties);
139   }
140 
141   /**
142    * Sets the aautomatic test table.
143    *
144    * @param automaticTestTable
145    *          the new aautomatic test table
146    */
147   @com.google.inject.Inject(optional = true)
148   public void setAautomaticTestTable(@Named("c3p0.automaticTestTable") final String automaticTestTable) {
149     dataSource.setAutomaticTestTable(automaticTestTable);
150   }
151 
152   /**
153    * Sets the break after acquire failure.
154    *
155    * @param breakAfterAcquireFailure
156    *          the new break after acquire failure
157    */
158   @com.google.inject.Inject(optional = true)
159   public void setBreakAfterAcquireFailure(
160       @Named("c3p0.breakAfterAcquireFailure") final boolean breakAfterAcquireFailure) {
161     dataSource.setBreakAfterAcquireFailure(breakAfterAcquireFailure);
162   }
163 
164   /**
165    * Sets the checkout timeout.
166    *
167    * @param checkoutTimeout
168    *          the new checkout timeout
169    */
170   @com.google.inject.Inject(optional = true)
171   public void setCheckoutTimeout(@Named("c3p0.checkoutTimeout") final int checkoutTimeout) {
172     dataSource.setCheckoutTimeout(checkoutTimeout);
173   }
174 
175   /**
176    * Sets the connection customizer class name.
177    *
178    * @param connectionCustomizerClassName
179    *          the new connection customizer class name
180    */
181   @com.google.inject.Inject(optional = true)
182   public void setConnectionCustomizerClassName(
183       @Named("c3p0.connectionCustomizerClassName") final String connectionCustomizerClassName) {
184     dataSource.setConnectionCustomizerClassName(connectionCustomizerClassName);
185   }
186 
187   /**
188    * Sets the connection tester class name.
189    *
190    * @param connectionTesterClassName
191    *          the new connection tester class name
192    */
193   @com.google.inject.Inject(optional = true)
194   public void setConnectionTesterClassName(
195       @Named("c3p0.connectionTesterClassName") final String connectionTesterClassName) {
196     try {
197       dataSource.setConnectionTesterClassName(connectionTesterClassName);
198     } catch (PropertyVetoException e) {
199       throw new RuntimeException("Impossible to set C3P0 Data Source connection tester class name '"
200           + connectionTesterClassName + "', see nested exceptions", e);
201     }
202   }
203 
204   /**
205    * Sets the idle connection test period.
206    *
207    * @param idleConnectionTestPeriod
208    *          the new idle connection test period
209    */
210   @com.google.inject.Inject(optional = true)
211   public void setIdleConnectionTestPeriod(@Named("c3p0.idleConnectionTestPeriod") final int idleConnectionTestPeriod) {
212     dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
213   }
214 
215   /**
216    * Sets the initial pool size.
217    *
218    * @param initialPoolSize
219    *          the new initial pool size
220    */
221   @com.google.inject.Inject(optional = true)
222   public void setInitialPoolSize(@Named("c3p0.initialPoolSize") final int initialPoolSize) {
223     dataSource.setInitialPoolSize(initialPoolSize);
224   }
225 
226   /**
227    * Sets the max administrative task time.
228    *
229    * @param maxAdministrativeTaskTime
230    *          the new max administrative task time
231    */
232   @com.google.inject.Inject(optional = true)
233   public void setMaxAdministrativeTaskTime(
234       @Named("c3p0.maxAdministrativeTaskTime") final int maxAdministrativeTaskTime) {
235     dataSource.setMaxAdministrativeTaskTime(maxAdministrativeTaskTime);
236   }
237 
238   /**
239    * Sets the max connection age.
240    *
241    * @param maxConnectionAge
242    *          the new max connection age
243    */
244   @com.google.inject.Inject(optional = true)
245   public void setMaxConnectionAge(@Named("c3p0.maxConnectionAge") final int maxConnectionAge) {
246     dataSource.setMaxConnectionAge(maxConnectionAge);
247   }
248 
249   /**
250    * Sets the max idle time.
251    *
252    * @param maxIdleTime
253    *          the new max idle time
254    */
255   @com.google.inject.Inject(optional = true)
256   public void setMaxIdleTime(@Named("c3p0.maxIdleTime") final int maxIdleTime) {
257     dataSource.setMaxIdleTime(maxIdleTime);
258   }
259 
260   /**
261    * Sets the max idle time excess connections.
262    *
263    * @param maxIdleTimeExcessConnections
264    *          the new max idle time excess connections
265    */
266   @com.google.inject.Inject(optional = true)
267   public void setMaxIdleTimeExcessConnections(
268       @Named("c3p0.maxIdleTimeExcessConnections") final int maxIdleTimeExcessConnections) {
269     dataSource.setMaxIdleTimeExcessConnections(maxIdleTimeExcessConnections);
270   }
271 
272   /**
273    * Sets the max pool size.
274    *
275    * @param maxPoolSize
276    *          the new max pool size
277    */
278   @com.google.inject.Inject(optional = true)
279   public void setMaxPoolSize(@Named("c3p0.maxPoolSize") final int maxPoolSize) {
280     dataSource.setMaxPoolSize(maxPoolSize);
281   }
282 
283   /**
284    * Sets the max statements.
285    *
286    * @param maxStatements
287    *          the new max statements
288    */
289   @com.google.inject.Inject(optional = true)
290   public void setMaxStatements(@Named("c3p0.maxStatements") final int maxStatements) {
291     dataSource.setMaxStatements(maxStatements);
292   }
293 
294   /**
295    * Sets the max statements per connection.
296    *
297    * @param maxStatementsPerConnection
298    *          the new max statements per connection
299    */
300   @com.google.inject.Inject(optional = true)
301   public void setMaxStatementsPerConnection(
302       @Named("c3p0.maxStatementsPerConnection") final int maxStatementsPerConnection) {
303     dataSource.setMaxStatementsPerConnection(maxStatementsPerConnection);
304   }
305 
306   /**
307    * Sets the min pool size.
308    *
309    * @param minPoolSize
310    *          the new min pool size
311    */
312   @com.google.inject.Inject(optional = true)
313   public void setMinPoolSize(@Named("c3p0.minPoolSize") final int minPoolSize) {
314     dataSource.setMinPoolSize(minPoolSize);
315   }
316 
317   /**
318    * Sets the preferred test query.
319    *
320    * @param preferredTestQuery
321    *          the new preferred test query
322    */
323   @com.google.inject.Inject(optional = true)
324   public void setPreferredTestQuery(@Named("c3p0.preferredTestQuery") final String preferredTestQuery) {
325     dataSource.setPreferredTestQuery(preferredTestQuery);
326   }
327 
328   /**
329    * Sets the property cycle.
330    *
331    * @param propertyCycle
332    *          the new property cycle
333    */
334   @com.google.inject.Inject(optional = true)
335   public void setPropertyCycle(@Named("c3p0.propertyCycle") final int propertyCycle) {
336     dataSource.setPropertyCycle(propertyCycle);
337   }
338 
339   /**
340    * Sets the test connection on checkin.
341    *
342    * @param testConnectionOnCheckin
343    *          the new test connection on checkin
344    */
345   @com.google.inject.Inject(optional = true)
346   public void setTestConnectionOnCheckin(@Named("c3p0.testConnectionOnCheckin") final boolean testConnectionOnCheckin) {
347     dataSource.setTestConnectionOnCheckin(testConnectionOnCheckin);
348   }
349 
350   /**
351    * Sets the test connection on checkout.
352    *
353    * @param testConnectionOnCheckout
354    *          the new test connection on checkout
355    */
356   @com.google.inject.Inject(optional = true)
357   public void setTestConnectionOnCheckout(
358       @Named("c3p0.testConnectionOnCheckout") final boolean testConnectionOnCheckout) {
359     dataSource.setTestConnectionOnCheckout(testConnectionOnCheckout);
360   }
361 
362   /**
363    * Sets the unreturned connection timeout.
364    *
365    * @param unreturnedConnectionTimeout
366    *          the new unreturned connection timeout
367    */
368   @com.google.inject.Inject(optional = true)
369   public void setUnreturnedConnectionTimeout(
370       @Named("c3p0.unreturnedConnectionTimeout") final int unreturnedConnectionTimeout) {
371     dataSource.setUnreturnedConnectionTimeout(unreturnedConnectionTimeout);
372   }
373 
374   /**
375    * Sets the uses traditional reflective proxies.
376    *
377    * @param usesTraditionalReflectiveProxies
378    *          the new uses traditional reflective proxies
379    */
380   @com.google.inject.Inject(optional = true)
381   public void setUsesTraditionalReflectiveProxies(
382       @Named("c3p0.usesTraditionalReflectiveProxies") final boolean usesTraditionalReflectiveProxies) {
383     dataSource.setUsesTraditionalReflectiveProxies(usesTraditionalReflectiveProxies);
384   }
385 
386   @Override
387   public DataSource get() {
388     if (username != null) {
389       dataSource.setUser(username);
390     }
391     if (password != null) {
392       dataSource.setPassword(password);
393     }
394     return dataSource;
395   }
396 
397 }