Skip to content

Commit 6fd7207

Browse files
author
diego Dupin
committed
[misc] Datasource permitting setting user/password for 2.x compatibility
1 parent f4538c5 commit 6fd7207

File tree

5 files changed

+358
-25
lines changed

5 files changed

+358
-25
lines changed

src/main/java/org/mariadb/jdbc/MariaDbDataSource.java

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,34 @@
1212

1313
public class MariaDbDataSource implements DataSource, ConnectionPoolDataSource, XADataSource {
1414

15-
private final Configuration conf;
15+
private Configuration conf = null;
16+
private String url = null;
17+
private String user = null;
18+
private String password = null;
19+
private Integer loginTimeout = null;
20+
21+
public MariaDbDataSource() {}
1622

1723
public MariaDbDataSource(String url) throws SQLException {
1824
if (Configuration.acceptsUrl(url)) {
19-
conf = Configuration.parse(url);
25+
this.url = url;
2026
} else {
2127
throw new SQLException(String.format("Wrong mariaDB url: %s", url));
2228
}
2329
}
2430

31+
private void config() throws SQLException {
32+
if (url == null) throw new SQLException("url not set");
33+
conf = Configuration.parse(url);
34+
if (loginTimeout != null) conf.connectTimeout(loginTimeout * 1000);
35+
if (user != null) {
36+
conf = conf.clone(user, password);
37+
} else {
38+
user = conf.user();
39+
password = conf.password();
40+
}
41+
}
42+
2543
/**
2644
* Attempts to establish a connection with the data source that this {@code DataSource} object
2745
* represents.
@@ -34,6 +52,7 @@ public MariaDbDataSource(String url) throws SQLException {
3452
*/
3553
@Override
3654
public Connection getConnection() throws SQLException {
55+
if (conf == null) config();
3756
return Driver.connect(conf);
3857
}
3958

@@ -51,6 +70,7 @@ public Connection getConnection() throws SQLException {
5170
*/
5271
@Override
5372
public Connection getConnection(String username, String password) throws SQLException {
73+
if (conf == null) config();
5474
Configuration conf = this.conf.clone(username, password);
5575
return Driver.connect(conf);
5676
}
@@ -129,7 +149,9 @@ public void setLogWriter(PrintWriter out) {}
129149
*/
130150
@Override
131151
public int getLoginTimeout() {
132-
return conf.connectTimeout() / 1000;
152+
if (loginTimeout != null) return loginTimeout;
153+
if (conf != null) return conf.connectTimeout() / 1000;
154+
return DriverManager.getLoginTimeout() > 0 ? DriverManager.getLoginTimeout() : 30;
133155
}
134156

135157
/**
@@ -139,12 +161,13 @@ public int getLoginTimeout() {
139161
* is created, the login timeout is initially 30s.
140162
*
141163
* @param seconds the data source login time limit
164+
* @throws SQLException if wrong configuration set
142165
* @see #getLoginTimeout
143-
* @since 1.4
144166
*/
145167
@Override
146-
public void setLoginTimeout(int seconds) {
147-
conf.connectTimeout(seconds * 1000);
168+
public void setLoginTimeout(int seconds) throws SQLException {
169+
loginTimeout = seconds;
170+
if (conf != null) config();
148171
}
149172

150173
/**
@@ -159,24 +182,71 @@ public Logger getParentLogger() {
159182

160183
@Override
161184
public PooledConnection getPooledConnection() throws SQLException {
185+
if (conf == null) config();
162186
return new MariaDbPoolConnection(Driver.connect(conf));
163187
}
164188

165189
@Override
166190
public PooledConnection getPooledConnection(String username, String password)
167191
throws SQLException {
192+
if (conf == null) config();
168193
Configuration conf = this.conf.clone(username, password);
169194
return new MariaDbPoolConnection(Driver.connect(conf));
170195
}
171196

172197
@Override
173198
public XAConnection getXAConnection() throws SQLException {
199+
if (conf == null) config();
174200
return new MariaDbPoolConnection(Driver.connect(conf));
175201
}
176202

177203
@Override
178204
public XAConnection getXAConnection(String username, String password) throws SQLException {
205+
if (conf == null) config();
179206
Configuration conf = this.conf.clone(username, password);
180207
return new MariaDbPoolConnection(Driver.connect(conf));
181208
}
209+
210+
/**
211+
* Sets the URL for this datasource
212+
*
213+
* @param url connection string
214+
* @throws SQLException if url is not accepted
215+
*/
216+
public void setUrl(String url) throws SQLException {
217+
if (Configuration.acceptsUrl(url)) {
218+
this.url = url;
219+
config();
220+
} else {
221+
throw new SQLException(String.format("Wrong mariaDB url: %s", url));
222+
}
223+
}
224+
225+
/**
226+
* Returns the URL for this datasource
227+
*
228+
* @return the URL for this datasource
229+
*/
230+
public String getUrl() {
231+
if (conf == null) return url;
232+
return conf.initialUrl();
233+
}
234+
235+
public String getUser() {
236+
return user;
237+
}
238+
239+
public void setUser(String user) throws SQLException {
240+
this.user = user;
241+
if (conf != null) config();
242+
}
243+
244+
public String getPassword() {
245+
return password;
246+
}
247+
248+
public void setPassword(String password) throws SQLException {
249+
this.password = password;
250+
if (conf != null) config();
251+
}
182252
}

src/main/java/org/mariadb/jdbc/MariaDbPoolDataSource.java

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,50 @@
77
import java.io.Closeable;
88
import java.io.PrintWriter;
99
import java.sql.Connection;
10+
import java.sql.DriverManager;
1011
import java.sql.SQLException;
1112
import java.sql.SQLTimeoutException;
1213
import java.util.List;
1314
import java.util.logging.Logger;
14-
import javax.sql.ConnectionPoolDataSource;
15-
import javax.sql.DataSource;
16-
import javax.sql.XAConnection;
17-
import javax.sql.XADataSource;
18-
import org.mariadb.jdbc.pool.MariaDbInnerPoolConnection;
15+
import javax.sql.*;
1916
import org.mariadb.jdbc.pool.Pool;
2017
import org.mariadb.jdbc.pool.Pools;
2118

2219
public class MariaDbPoolDataSource
2320
implements DataSource, ConnectionPoolDataSource, XADataSource, Closeable, AutoCloseable {
2421

25-
private final Pool pool;
22+
private Pool pool;
23+
private Configuration conf = null;
24+
private String url = null;
25+
private String user = null;
26+
private String password = null;
27+
private Integer loginTimeout = null;
28+
29+
public MariaDbPoolDataSource() {}
2630

2731
public MariaDbPoolDataSource(String url) throws SQLException {
2832
if (Configuration.acceptsUrl(url)) {
29-
Configuration conf = Configuration.parse(url);
33+
this.url = url;
34+
conf = Configuration.parse(url);
3035
pool = Pools.retrievePool(conf);
3136
} else {
3237
throw new SQLException(String.format("Wrong mariaDB url: %s", url));
3338
}
3439
}
3540

41+
private void config() throws SQLException {
42+
if (url == null) throw new SQLException("url not set");
43+
conf = Configuration.parse(url);
44+
if (loginTimeout != null) conf.connectTimeout(loginTimeout * 1000);
45+
if (user != null) {
46+
conf = conf.clone(user, password);
47+
} else {
48+
user = conf.user();
49+
password = conf.password();
50+
}
51+
pool = Pools.retrievePool(conf);
52+
}
53+
3654
/**
3755
* Attempts to establish a connection with the data source that this {@code DataSource} object
3856
* represents.
@@ -45,6 +63,7 @@ public MariaDbPoolDataSource(String url) throws SQLException {
4563
*/
4664
@Override
4765
public Connection getConnection() throws SQLException {
66+
if (conf == null) config();
4867
return pool.getPoolConnection().getConnection();
4968
}
5069

@@ -62,6 +81,7 @@ public Connection getConnection() throws SQLException {
6281
*/
6382
@Override
6483
public Connection getConnection(String username, String password) throws SQLException {
84+
if (conf == null) config();
6585
return pool.getPoolConnection(username, password).getConnection();
6686
}
6787

@@ -139,7 +159,9 @@ public void setLogWriter(PrintWriter out) {}
139159
*/
140160
@Override
141161
public int getLoginTimeout() {
142-
return pool.getConf().connectTimeout() / 1000;
162+
if (loginTimeout != null) return loginTimeout;
163+
if (conf != null) return conf.connectTimeout() / 1000;
164+
return DriverManager.getLoginTimeout() > 0 ? DriverManager.getLoginTimeout() : 30;
143165
}
144166

145167
/**
@@ -149,12 +171,13 @@ public int getLoginTimeout() {
149171
* is created, the login timeout is initially 30s.
150172
*
151173
* @param seconds the data source login time limit
174+
* @throws SQLException if wrong configuration set
152175
* @see #getLoginTimeout
153-
* @since 1.4
154176
*/
155177
@Override
156-
public void setLoginTimeout(int seconds) {
157-
pool.getConf().connectTimeout(seconds * 1000);
178+
public void setLoginTimeout(int seconds) throws SQLException {
179+
loginTimeout = seconds;
180+
if (conf != null) config();
158181
}
159182

160183
/**
@@ -168,37 +191,84 @@ public Logger getParentLogger() {
168191
}
169192

170193
@Override
171-
public MariaDbInnerPoolConnection getPooledConnection() throws SQLException {
194+
public PooledConnection getPooledConnection() throws SQLException {
195+
if (conf == null) config();
172196
return pool.getPoolConnection();
173197
}
174198

175199
@Override
176-
public MariaDbInnerPoolConnection getPooledConnection(String username, String password)
200+
public PooledConnection getPooledConnection(String username, String password)
177201
throws SQLException {
202+
if (conf == null) config();
178203
return pool.getPoolConnection(username, password);
179204
}
180205

181206
@Override
182207
public XAConnection getXAConnection() throws SQLException {
208+
if (conf == null) config();
183209
return pool.getPoolConnection();
184210
}
185211

186212
@Override
187213
public XAConnection getXAConnection(String username, String password) throws SQLException {
214+
if (conf == null) config();
188215
return pool.getPoolConnection(username, password);
189216
}
190217

218+
/**
219+
* Sets the URL for this datasource
220+
*
221+
* @param url connection string
222+
* @throws SQLException if url is not accepted
223+
*/
224+
public void setUrl(String url) throws SQLException {
225+
if (Configuration.acceptsUrl(url)) {
226+
this.url = url;
227+
config();
228+
} else {
229+
throw new SQLException(String.format("Wrong mariaDB url: %s", url));
230+
}
231+
}
232+
233+
/**
234+
* Returns the URL for this datasource
235+
*
236+
* @return the URL for this datasource
237+
*/
238+
public String getUrl() {
239+
if (conf == null) return url;
240+
return conf.initialUrl();
241+
}
242+
243+
public String getUser() {
244+
return user;
245+
}
246+
247+
public void setUser(String user) throws SQLException {
248+
this.user = user;
249+
if (conf != null) config();
250+
}
251+
252+
public String getPassword() {
253+
return password;
254+
}
255+
256+
public void setPassword(String password) throws SQLException {
257+
this.password = password;
258+
if (conf != null) config();
259+
}
260+
191261
/** Close datasource. */
192262
public void close() {
193263
try {
194-
pool.close();
264+
if (pool != null) pool.close();
195265
} catch (Exception interrupted) {
196266
// eat
197267
}
198268
}
199269

200270
public String getPoolName() {
201-
return pool.getPoolTag();
271+
return (pool != null) ? pool.getPoolTag() : null;
202272
}
203273

204274
/**
@@ -207,6 +277,6 @@ public String getPoolName() {
207277
* @return current thread id's
208278
*/
209279
public List<Long> testGetConnectionIdleThreadIds() {
210-
return pool.testGetConnectionIdleThreadIds();
280+
return (pool != null) ? pool.testGetConnectionIdleThreadIds() : null;
211281
}
212282
}

0 commit comments

Comments
 (0)