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.builtin;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Named;
20  import jakarta.inject.Provider;
21  
22  import java.util.Properties;
23  
24  import javax.sql.DataSource;
25  
26  import org.apache.ibatis.datasource.pooled.PooledDataSource;
27  
28  /**
29   * Provides the myBatis built-in PooledDataSource.
30   */
31  public final class PooledDataSourceProvider implements Provider<DataSource> {
32  
33    /**
34     * The PooledDataSource reference.
35     */
36    private final PooledDataSource dataSource;
37  
38    /**
39     * Creates a new PooledDataSource using the needed parameter.
40     *
41     * @param driver
42     *          The JDBC driver class.
43     * @param url
44     *          the database URL of the form <code>jdbc:subprotocol:subname</code>.
45     * @param driverClassLoader
46     *          ClassLoader to use to load JDBC driver class.
47     */
48    @Inject
49    public PooledDataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url,
50        @Named("JDBC.driverClassLoader") final ClassLoader driverClassLoader) {
51      dataSource = new PooledDataSource(driverClassLoader, driver, url, null, null);
52    }
53  
54    /**
55     * Sets the user.
56     *
57     * @param username
58     *          the new user
59     *
60     * @since 3.3
61     */
62    @com.google.inject.Inject(optional = true)
63    public void setUser(@Named("JDBC.username") final String username) {
64      dataSource.setUsername(username);
65    }
66  
67    /**
68     * Sets the password.
69     *
70     * @param password
71     *          the new password
72     *
73     * @since 3.3
74     */
75    @com.google.inject.Inject(optional = true)
76    public void setPassword(@Named("JDBC.password") final String password) {
77      dataSource.setPassword(password);
78    }
79  
80    /**
81     * Sets the auto commit.
82     *
83     * @param autoCommit
84     *          the new auto commit
85     */
86    @com.google.inject.Inject(optional = true)
87    public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
88      dataSource.setDefaultAutoCommit(autoCommit);
89    }
90  
91    /**
92     * Sets the login timeout.
93     *
94     * @param loginTimeout
95     *          the new login timeout
96     */
97    @com.google.inject.Inject(optional = true)
98    public void setLoginTimeout(@Named("JDBC.loginTimeout") final int loginTimeout) {
99      dataSource.setLoginTimeout(loginTimeout);
100   }
101 
102   @com.google.inject.Inject(optional = true)
103   public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
104     dataSource.setDriverProperties(driverProperties);
105   }
106 
107   /**
108    * Sets the maximum active connections.
109    *
110    * @param maximumActiveConnections
111    *          the new maximum active connections
112    */
113   @com.google.inject.Inject(optional = true)
114   public void setMaximumActiveConnections(
115       @Named("mybatis.pooled.maximumActiveConnections") final int maximumActiveConnections) {
116     dataSource.setPoolMaximumActiveConnections(maximumActiveConnections);
117   }
118 
119   /**
120    * Sets the maximum checkout time.
121    *
122    * @param maximumCheckoutTime
123    *          the new maximum checkout time
124    */
125   @com.google.inject.Inject(optional = true)
126   public void setMaximumCheckoutTime(@Named("mybatis.pooled.maximumCheckoutTime") final int maximumCheckoutTime) {
127     dataSource.setPoolMaximumCheckoutTime(maximumCheckoutTime);
128   }
129 
130   /**
131    * Sets the maximum idle connections.
132    *
133    * @param maximumIdleConnections
134    *          the new maximum idle connections
135    */
136   @com.google.inject.Inject(optional = true)
137   public void setMaximumIdleConnections(
138       @Named("mybatis.pooled.maximumIdleConnections") final int maximumIdleConnections) {
139     dataSource.setPoolMaximumIdleConnections(maximumIdleConnections);
140   }
141 
142   /**
143    * Sets the ping connections not used for.
144    *
145    * @param pingConnectionsNotUsedFor
146    *          the new ping connections not used for
147    */
148   @com.google.inject.Inject(optional = true)
149   public void setPingConnectionsNotUsedFor(
150       @Named("mybatis.pooled.pingConnectionsNotUsedFor") final int pingConnectionsNotUsedFor) {
151     dataSource.setPoolPingConnectionsNotUsedFor(pingConnectionsNotUsedFor);
152   }
153 
154   /**
155    * Sets the ping enabled.
156    *
157    * @param pingEnabled
158    *          the new ping enabled
159    */
160   @com.google.inject.Inject(optional = true)
161   public void setPingEnabled(@Named("mybatis.pooled.pingEnabled") final boolean pingEnabled) {
162     dataSource.setPoolPingEnabled(pingEnabled);
163   }
164 
165   /**
166    * Sets the ping enabled.
167    *
168    * @param pingQuery
169    *          the new ping enabled
170    */
171   @com.google.inject.Inject(optional = true)
172   public void setPingEnabled(@Named("mybatis.pooled.pingQuery") final String pingQuery) {
173     dataSource.setPoolPingQuery(pingQuery);
174   }
175 
176   /**
177    * Sets the time to wait.
178    *
179    * @param timeToWait
180    *          the new time to wait
181    */
182   @com.google.inject.Inject(optional = true)
183   public void setTimeToWait(@Named("mybatis.pooled.timeToWait") final int timeToWait) {
184     dataSource.setPoolTimeToWait(timeToWait);
185   }
186 
187   @Override
188   public DataSource get() {
189     return dataSource;
190   }
191 
192 }