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.hikaricp;
17  
18  import com.zaxxer.hikari.HikariConfig;
19  import com.zaxxer.hikari.HikariDataSource;
20  import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
21  
22  import jakarta.inject.Inject;
23  import jakarta.inject.Named;
24  import jakarta.inject.Provider;
25  
26  import java.util.Properties;
27  import java.util.concurrent.ScheduledExecutorService;
28  import java.util.concurrent.ThreadFactory;
29  
30  import javax.sql.DataSource;
31  
32  /**
33   * Provides the HikariCP DataSource.
34   * <p>
35   * For additional information see configuration here
36   * <a href="https://github.com/brettwooldridge/HikariCP">https://github.com/brettwooldridge/HikariCP</a>.
37   */
38  public final class HikariCPProvider implements Provider<DataSource> {
39    private final HikariConfig configuration = new HikariConfig();
40  
41    private Integer loginTimeout;
42  
43    @Override
44    public DataSource get() {
45      HikariDataSource dataSource = new HikariDataSource(configuration);
46      if (loginTimeout != null) {
47        try {
48          dataSource.setLoginTimeout(loginTimeout);
49        } catch (Exception e) {
50          throw new RuntimeException("Failed to set login timeout '" + loginTimeout + "' for the HikariCP datasource", e);
51        }
52      }
53  
54      return dataSource;
55    }
56  
57    @com.google.inject.Inject(optional = true)
58    public void setAllowPoolSuspension(@Named("hikaricp.allowPoolSuspension") boolean allowPoolSuspension) {
59      configuration.setAllowPoolSuspension(allowPoolSuspension);
60    }
61  
62    @com.google.inject.Inject(optional = true)
63    public void setAutoCommit(@Named("hikaricp.autoCommit") boolean autoCommit) {
64      configuration.setAutoCommit(autoCommit);
65    }
66  
67    @com.google.inject.Inject(optional = true)
68    public void setCatalog(@Named("hikaricp.catalog") String catalog) {
69      configuration.setCatalog(catalog);
70    }
71  
72    @com.google.inject.Inject(optional = true)
73    public void setConnectionInitSql(@Named("hikaricp.connectionInitSql") String connectionInitSql) {
74      configuration.setConnectionInitSql(connectionInitSql);
75    }
76  
77    @com.google.inject.Inject(optional = true)
78    public void setConnectionTestQuery(@Named("hikaricp.connectionTestQuery") String connectionTestQuery) {
79      configuration.setConnectionTestQuery(connectionTestQuery);
80    }
81  
82    /**
83     * Sets the <code>connectionTimeout</code> HikariCP configuration property.
84     *
85     * @param connectionTimeoutMs
86     *          the connection timeout in milliseconds
87     */
88    @com.google.inject.Inject(optional = true)
89    public void setConnectionTimeout(@Named("hikaricp.connectionTimeoutMs") long connectionTimeoutMs) {
90      configuration.setConnectionTimeout(connectionTimeoutMs);
91    }
92  
93    @com.google.inject.Inject(optional = true)
94    public void setDriverClassName(@Named("hikaricp.driverClassName") String driverClassName) {
95      configuration.setDriverClassName(driverClassName);
96    }
97  
98    @com.google.inject.Inject(optional = true)
99    public void setHealthCheckProperties(@Named("hikaricp.healthCheckProperties") Properties healthCheckProperties) {
100     configuration.setHealthCheckProperties(healthCheckProperties);
101   }
102 
103   @com.google.inject.Inject(optional = true)
104   public void setHealthCheckRegistry(@Named("hikaricp.healthCheckRegistry") Object healthCheckRegistry) {
105     configuration.setHealthCheckRegistry(healthCheckRegistry);
106   }
107 
108   @com.google.inject.Inject(optional = true)
109   public void setIdleTimeout(@Named("hikaricp.idleTimeoutMs") long idleTimeoutMs) {
110     configuration.setIdleTimeout(idleTimeoutMs);
111   }
112 
113   @com.google.inject.Inject(optional = true)
114   public void setInitializationFailTimeout(
115       @Named("hikaricp.initializationFailTimeout") long initializationFailTimeout) {
116     configuration.setInitializationFailTimeout(initializationFailTimeout);
117   }
118 
119   @com.google.inject.Inject(optional = true)
120   public void setIsolateInternalQueries(@Named("hikaricp.isolateInternalQueries") boolean isolateInternalQueries) {
121     configuration.setIsolateInternalQueries(isolateInternalQueries);
122   }
123 
124   @Inject
125   public void setJdbcUrl(@Named("JDBC.url") String jdbcUrl) {
126     configuration.setJdbcUrl(jdbcUrl);
127   }
128 
129   @com.google.inject.Inject(optional = true)
130   public void setLeakDetectionThreshold(@Named("hikaricp.leakDetectionThresholdMs") long leakDetectionThresholdMs) {
131     configuration.setLeakDetectionThreshold(leakDetectionThresholdMs);
132   }
133 
134   @com.google.inject.Inject(optional = true)
135   public void setLoginTimeout(@Named("JDBC.loginTimeout") int loginTimeout) {
136     this.loginTimeout = loginTimeout;
137   }
138 
139   @com.google.inject.Inject(optional = true)
140   public void setMaxLifetime(@Named("hikaricp.maxLifetimeMs") long maxLifetimeMs) {
141     configuration.setMaxLifetime(maxLifetimeMs);
142   }
143 
144   @com.google.inject.Inject(optional = true)
145   public void setMaximumPoolSize(@Named("hikaricp.maxPoolSize") int maxPoolSize) {
146     configuration.setMaximumPoolSize(maxPoolSize);
147   }
148 
149   @com.google.inject.Inject(optional = true)
150   public void setMetricRegistry(@Named("hikaricp.metricRegistry") Object metricRegistry) {
151     configuration.setMetricRegistry(metricRegistry);
152   }
153 
154   @com.google.inject.Inject(optional = true)
155   public void setMetricsTrackerFactory(
156       @Named("hikaricp.metricsTrackerFactory") MetricsTrackerFactory metricsTrackerFactory) {
157     configuration.setMetricsTrackerFactory(metricsTrackerFactory);
158   }
159 
160   @com.google.inject.Inject(optional = true)
161   public void setMinimumIdle(@Named("hikaricp.minimumIdle") int minimumIdle) {
162     configuration.setMinimumIdle(minimumIdle);
163   }
164 
165   @Inject
166   public void setPassword(@Named("JDBC.password") String password) {
167     configuration.setPassword(password);
168   }
169 
170   @com.google.inject.Inject(optional = true)
171   public void setPoolName(@Named("hikaricp.poolName") String poolName) {
172     configuration.setPoolName(poolName);
173   }
174 
175   @com.google.inject.Inject(optional = true)
176   public void setReadOnly(@Named("hikaricp.readOnly") boolean readOnly) {
177     configuration.setReadOnly(readOnly);
178   }
179 
180   @com.google.inject.Inject(optional = true)
181   public void setRegisterMbeans(@Named("hikaricp.registerMbeans") boolean registerMbeans) {
182     configuration.setRegisterMbeans(registerMbeans);
183   }
184 
185   @com.google.inject.Inject(optional = true)
186   public void setScheduledExecutor(
187       @Named("hikaricp.scheduledExecutorService") ScheduledExecutorService scheduledExecutorService) {
188     configuration.setScheduledExecutor(scheduledExecutorService);
189   }
190 
191   @com.google.inject.Inject(optional = true)
192   public void setSchema(@Named("hikaricp.schema") String schema) {
193     configuration.setSchema(schema);
194   }
195 
196   @com.google.inject.Inject(optional = true)
197   public void setThreadFactory(@Named("hikaricp.threadFactory") ThreadFactory threadFactory) {
198     configuration.setThreadFactory(threadFactory);
199   }
200 
201   @com.google.inject.Inject(optional = true)
202   public void setTransactionIsolation(@Named("hikaricp.transactionIsolation") String transactionIsolation) {
203     configuration.setTransactionIsolation(transactionIsolation);
204   }
205 
206   @Inject
207   public void setUsername(@Named("JDBC.username") String username) {
208     configuration.setUsername(username);
209   }
210 
211   @com.google.inject.Inject(optional = true)
212   public void setValidationTimeout(@Named("hikaricp.validationTimeoutMs") long validationTimeoutMs) {
213     configuration.setValidationTimeout(validationTimeoutMs);
214   }
215 }