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.Named;
19  import jakarta.inject.Provider;
20  
21  import java.time.Duration;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import javax.sql.ConnectionPoolDataSource;
26  import javax.sql.DataSource;
27  
28  import org.apache.commons.dbcp2.datasources.PerUserPoolDataSource;
29  
30  /**
31   * Provides the Apache commons-dbcp2 {@code PerUserPoolDataSource}.
32   */
33  public final class PerUserPoolDataSourceProvider implements Provider<DataSource> {
34  
35    private final PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
36  
37    @com.google.inject.Inject(optional = true)
38    public void setConnectionPoolDataSource(ConnectionPoolDataSource cpds) {
39      dataSource.setConnectionPoolDataSource(cpds);
40    }
41  
42    @com.google.inject.Inject(optional = true)
43    public void setDataSourceName(@Named("DBCP.name") String name) {
44      dataSource.setDataSourceName(name);
45    }
46  
47    @com.google.inject.Inject(optional = true)
48    public void setDefaultAutoCommit(@Named("JDBC.autoCommit") boolean autoCommit) {
49      dataSource.setDefaultAutoCommit(autoCommit);
50    }
51  
52    @com.google.inject.Inject(optional = true)
53    public void setDefaultReadOnly(@Named("DBCP.defaultReadOnly") boolean defaultReadOnly) {
54      dataSource.setDefaultReadOnly(defaultReadOnly);
55    }
56  
57    @com.google.inject.Inject(optional = true)
58    public void setDefaultTransactionIsolation(
59        @Named("DBCP.defaultTransactionIsolation") int defaultTransactionIsolation) {
60      dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
61    }
62  
63    @com.google.inject.Inject(optional = true)
64    public void setDescription(@Named("DBCP.description") String description) {
65      dataSource.setDescription(description);
66    }
67  
68    @com.google.inject.Inject(optional = true)
69    public void setJndiEnvironment(@Named("DBCP.jndi.key") String key, @Named("DBCP.jndi.value") String value) {
70      dataSource.setJndiEnvironment(key, value);
71    }
72  
73    @com.google.inject.Inject(optional = true)
74    public void setLoginTimeout(@Named("JDBC.loginTimeout") int loginTimeout) {
75      dataSource.setLoginTimeout(Duration.ofSeconds(loginTimeout));
76    }
77  
78    @com.google.inject.Inject(optional = true)
79    public void setDefaultMinEvictableIdleTimeMillis(
80        @Named("DBCP.defaultMinEvictableIdleTimeMillis") int defaultMinEvictableIdleTimeMillis) {
81      dataSource.setDefaultMinEvictableIdle(Duration.ofMillis(defaultMinEvictableIdleTimeMillis));
82    }
83  
84    @com.google.inject.Inject(optional = true)
85    public void setDefaultNumTestsPerEvictionRun(
86        @Named("DBCP.defaultNumTestsPerEvictionRun") int defaultNumTestsPerEvictionRun) {
87      dataSource.setDefaultNumTestsPerEvictionRun(defaultNumTestsPerEvictionRun);
88    }
89  
90    @com.google.inject.Inject(optional = true)
91    public void setRollbackAfterValidation(@Named("DBCP.rollbackAfterValidation") boolean rollbackAfterValidation) {
92      dataSource.setRollbackAfterValidation(rollbackAfterValidation);
93    }
94  
95    @com.google.inject.Inject(optional = true)
96    public void setDefaultTestOnBorrow(@Named("DBCP.defaultTestOnBorrow") boolean defaultTestOnBorrow) {
97      dataSource.setDefaultTestOnBorrow(defaultTestOnBorrow);
98    }
99  
100   @com.google.inject.Inject(optional = true)
101   public void setDefaultTestOnReturn(@Named("DBCP.defaultTestOnReturn") boolean defaultTestOnReturn) {
102     dataSource.setDefaultTestOnReturn(defaultTestOnReturn);
103   }
104 
105   @com.google.inject.Inject(optional = true)
106   public void setDefaultTestWhileIdle(@Named("DBCP.defaultTestWhileIdle") boolean defaultTestWhileIdle) {
107     dataSource.setDefaultTestWhileIdle(defaultTestWhileIdle);
108   }
109 
110   @com.google.inject.Inject(optional = true)
111   public void setDefaultTimeBetweenEvictionRunsMillis(
112       @Named("DBCP.defaultTimeBetweenEvictionRunsMillis") int defaultTimeBetweenEvictionRunsMillis) {
113     dataSource.setDefaultDurationBetweenEvictionRuns(Duration.ofMillis(defaultTimeBetweenEvictionRunsMillis));
114   }
115 
116   @com.google.inject.Inject(optional = true)
117   public void setValidationQuery(@Named("DBCP.validationQuery") String validationQuery) {
118     dataSource.setValidationQuery(validationQuery);
119   }
120 
121   @com.google.inject.Inject(optional = true)
122   public void setDefaultMaxTotal(@Named("DBCP.defaultMaxTotal") int defaultMaxTotal) {
123     dataSource.setDefaultMaxTotal(defaultMaxTotal);
124   }
125 
126   @com.google.inject.Inject(optional = true)
127   public void setDefaultMaxIdle(@Named("DBCP.defaultMaxIdle") int defaultMaxIdle) {
128     dataSource.setDefaultMaxIdle(defaultMaxIdle);
129   }
130 
131   @com.google.inject.Inject(optional = true)
132   public void setDefaultMaxWaitMillis(@Named("DBCP.defaultMaxWaitMillis") int defaultMaxWaitMillis) {
133     dataSource.setDefaultMaxWait(Duration.ofMillis(defaultMaxWaitMillis));
134   }
135 
136   /**
137    * Sets the per user default auto commit.
138    *
139    * @param perUserDefaultAutoCommit
140    *          the per user default auto commit
141    */
142   @com.google.inject.Inject(optional = true)
143   public void setPerUserDefaultAutoCommit(@PerUserDefaultAutoCommit Map<String, Boolean> perUserDefaultAutoCommit) {
144     for (Entry<String, Boolean> entry : perUserDefaultAutoCommit.entrySet()) {
145       dataSource.setPerUserDefaultAutoCommit(entry.getKey(), entry.getValue());
146     }
147   }
148 
149   /**
150    * Sets the per user default read only.
151    *
152    * @param perUserDefaultReadOnly
153    *          the per user default read only
154    */
155   @com.google.inject.Inject(optional = true)
156   public void setPerUserDefaultReadOnly(@PerUserDefaultReadOnly Map<String, Boolean> perUserDefaultReadOnly) {
157     for (Entry<String, Boolean> entry : perUserDefaultReadOnly.entrySet()) {
158       dataSource.setPerUserDefaultReadOnly(entry.getKey(), entry.getValue());
159     }
160   }
161 
162   /**
163    * Sets the per user default transaction isolation.
164    *
165    * @param perUserDefaultTransactionIsolation
166    *          the per user default transaction isolation
167    */
168   @com.google.inject.Inject(optional = true)
169   public void setPerUserDefaultTransactionIsolation(
170       @PerUserDefaultTransactionIsolation Map<String, Integer> perUserDefaultTransactionIsolation) {
171     for (Entry<String, Integer> entry : perUserDefaultTransactionIsolation.entrySet()) {
172       dataSource.setPerUserDefaultTransactionIsolation(entry.getKey(), entry.getValue());
173     }
174   }
175 
176   /**
177    * Sets the per user max total.
178    *
179    * @param perUserMaxTotal
180    *          the per user max total
181    */
182   @com.google.inject.Inject(optional = true)
183   public void setPerUserMaxTotal(@PerUserMaxTotal Map<String, Integer> perUserMaxTotal) {
184     for (Entry<String, Integer> entry : perUserMaxTotal.entrySet()) {
185       dataSource.setPerUserMaxTotal(entry.getKey(), entry.getValue());
186     }
187   }
188 
189   /**
190    * Sets the per user max idle.
191    *
192    * @param perUserMaxIdle
193    *          the per user max idle
194    */
195   @com.google.inject.Inject(optional = true)
196   public void setPerUserMaxIdle(@PerUserMaxIdle Map<String, Integer> perUserMaxIdle) {
197     for (Entry<String, Integer> entry : perUserMaxIdle.entrySet()) {
198       dataSource.setPerUserMaxIdle(entry.getKey(), entry.getValue());
199     }
200   }
201 
202   /**
203    * Sets the per user max wait in milliseconds.
204    *
205    * @param perUserMaxWaitMillis
206    *          the per user max wait in milliseconds
207    */
208   @com.google.inject.Inject(optional = true)
209   public void setPerUserMaxWaitMillis(@PerUserMaxWaitMillis Map<String, Long> perUserMaxWaitMillis) {
210     for (Entry<String, Long> entry : perUserMaxWaitMillis.entrySet()) {
211       dataSource.setPerUserMaxWait(entry.getKey(), Duration.ofMillis(entry.getValue()));
212     }
213   }
214 
215   @Override
216   public DataSource get() {
217     return dataSource;
218   }
219 
220 }