-
Notifications
You must be signed in to change notification settings - Fork 219
/
MySQLDataSource.java
484 lines (427 loc) · 16.5 KB
/
MySQLDataSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
MariaDB Client for Java
Copyright (c) 2012 Monty Program Ab.
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to Monty Program Ab info@montyprogram.com.
This particular MariaDB Client for Java file is work
derived from a Drizzle-JDBC. Drizzle-JDBC file which is covered by subject to
the following copyright and notice provisions:
Copyright (c) 2009-2011, Marcus Eriksson
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the driver nor the names of its contributors may not be
used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
package org.mariadb.jdbc;
import org.mariadb.jdbc.internal.SQLExceptionMapper;
import org.mariadb.jdbc.internal.common.QueryException;
import org.mariadb.jdbc.internal.common.Utils;
import org.mariadb.jdbc.internal.mysql.*;
import javax.sql.*;
import java.io.PrintWriter;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
public class MySQLDataSource implements DataSource, ConnectionPoolDataSource, XADataSource {
private String hostname = "localhost";
private int port = 0;
private String database = "";
private String username = null;
private String password = null;
private Properties info = null;
private JDBCUrl url;
public MySQLDataSource(String hostname, int port, String database) {
this.hostname = hostname;
this.port = port;
this.database = database;
this.info = new Properties();
}
public MySQLDataSource() {
this.info = new Properties();
}
/**
* Sets the database name.
*
* @param dbName
* the name of the database
*/
public void setDatabaseName(String dbName) {
this.database = dbName;
resetUrl();
}
/**
* Gets the name of the database
*
* @return the name of the database for this data source
*/
public String getDatabaseName() {
return (this.database != null) ? this.database : "";
}
/**
* Sets the username
*
* @param userName
* the username
*/
public void setUser(String userName) {
setUserName(userName);
}
/**
* Gets the username
*
* @return the username to use when connecting to the database
*/
public String getUser() {
return this.username;
}
/**
* Sets the username
*
* @param userName
* the username
*/
public void setUserName(String userName) {
this.username = userName;
}
/**
* Gets the username
*
* @return the username to use when connecting to the database
*/
public String getUserName() {
return this.username;
}
/**
* Sets the password
*
* @param pass
* the password
*/
public void setPassword(String pass) {
this.password = pass;
}
/**
* Sets the database port.
*
* @param p
* the port
*/
public void setPort(int p) {
this.port = p;
resetUrl();
}
/**
* Returns the port number
*
* @return the port number
*/
public int getPort() {
return this.port;
}
/**
* Sets the port number
*
* @param p
* the port
*
* @see #setPort
*/
public void setPortNumber(int p) {
setPort(p);
}
/**
* Returns the port number
*
* @return the port number
*/
public int getPortNumber() {
return getPort();
}
/**
* Sets the server name.
*
* @param serverName
* the server name
*/
public void setServerName(String serverName) {
this.hostname = serverName;
resetUrl();
}
public void setProperties(String properties) {
Utils.setUrlParameters(properties, this.info);
}
/**
* Sets the connection string URL.
*
* @param url
* the connection string
*/
public void setURL(String url) {
setUrl(url);
}
/**
* Sets the connection string URL.
*
* @param s
* the connection string
*/
public void setUrl(String s) {
String baseUrl = s;
int idx = s.lastIndexOf("?");
if (idx > 0) {
baseUrl = s.substring(0,idx);
String urlParams = s.substring(idx+1);
setProperties(urlParams);
}
this.url = JDBCUrl.parse(baseUrl);
String tmpStr;
if ((tmpStr = url.getDatabase()) != null) {
this.database = tmpStr;
}
if ((tmpStr = url.getHostname()) != null) {
this.hostname = tmpStr;
}
tmpStr = url.getUsername();
if (tmpStr.equals("")) {
tmpStr = this.info.getProperty("user", "");
}
if (!tmpStr.equals("")) {
this.username = tmpStr;
}
tmpStr = url.getPassword();
if (tmpStr.equals("")) {
tmpStr = this.info.getProperty("password", "");
}
if (!tmpStr.equals("")) {
this.password = tmpStr;
}
this.port = url.getPort();
}
/**
* Returns the name of the database server
*
* @return the name of the database server
*/
public String getServerName() {
return (this.hostname != null) ? this.hostname : "";
}
void createUrl() {
if (url != null)
return;
String urlString = "jdbc:mysql://" + hostname;
if (port != 0)
urlString = urlString + ":" + port;
if (database != null)
urlString = urlString + "/" + database;
url = JDBCUrl.parse(urlString);
}
private void resetUrl() {
this.url = null;
}
/**
* Attempts to establish a connection with the data source that this <code>DataSource</code> object represents.
*
* @return a connection to the data source
* @throws java.sql.SQLException if a database access error occurs
*/
public Connection getConnection() throws SQLException {
createUrl();
try {
Protocol proxyfiedProtocol = (Protocol) Proxy.newProxyInstance(
MySQLProtocol.class.getClassLoader(),
new Class[]{Protocol.class},
new FailoverProxy(new MySQLProtocol(url, username, password, info), new SingleHostListener())
);
proxyfiedProtocol.initializeConnection();
return MySQLConnection.newConnection(proxyfiedProtocol);
} catch (QueryException e) {
SQLExceptionMapper.throwException(e, null, null);
return null;
}
}
/**
* Attempts to establish a connection with the data source that this <code>DataSource</code> object represents.
*
* @param username the database user on whose behalf the connection is being made
* @param password the user's password
* @return a connection to the data source
* @throws java.sql.SQLException if a database access error occurs
* @since 1.4
*/
public Connection getConnection(final String username, final String password) throws SQLException {
createUrl();
try {
Properties props = info == null ? new Properties() : info;
Protocol proxyfiedProtocol = ConnectorUtils.retrieveProxy(url, username, password, props);
proxyfiedProtocol.initializeConnection();
return MySQLConnection.newConnection(proxyfiedProtocol);
} catch (QueryException e) {
SQLExceptionMapper.throwException(e, null, null);
return null;
}
}
/**
* Retrieves the log writer for this <code>DataSource</code> object.
*
* The log writer is a character output stream to which all logging and tracing messages for this data source
* will be printed. This includes messages printed by the methods of this object, messages printed by methods of
* other objects manufactured by this object, and so on. Messages printed to a data source specific log writer are
* not printed to the log writer associated with the <code>java.sql.DriverManager</code> class. When a
* <code>DataSource</code> object is created, the log writer is initially null; in other words, the default is for
* logging to be disabled.
*
* @return the log writer for this data source or null if logging is disabled
* @throws java.sql.SQLException if a database access error occurs
* @see #setLogWriter
* @since 1.4
*/
public PrintWriter getLogWriter() throws SQLException {
return null;
}
/**
* Sets the log writer for this <code>DataSource</code> object to the given <code>java.io.PrintWriter</code>
* object.
*
* The log writer is a character output stream to which all logging and tracing messages for this data source
* will be printed. This includes messages printed by the methods of this object, messages printed by methods of
* other objects manufactured by this object, and so on. Messages printed to a data source- specific log writer are
* not printed to the log writer associated with the <code>java.sql.DriverManager</code> class. When a
* <code>DataSource</code> object is created the log writer is initially null; in other words, the default is for
* logging to be disabled.
*
* @param out the new log writer; to disable logging, set to null
* @throws java.sql.SQLException if a database access error occurs
* @see #getLogWriter
* @since 1.4
*/
public void setLogWriter(final PrintWriter out) throws SQLException {
}
/**
* Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. A
* value of zero specifies that the timeout is the default system timeout if there is one; otherwise, it specifies
* that there is no timeout. When a <code>DataSource</code> object is created, the login timeout is initially zero.
*
* @param seconds the data source login time limit
* @throws java.sql.SQLException if a database access error occurs.
* @see #getLoginTimeout
* @since 1.4
*/
public void setLoginTimeout(final int seconds) throws SQLException {
}
/**
* Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. A
* value of zero means that the timeout is the default system timeout if there is one; otherwise, it means that
* there is no timeout. When a <code>DataSource</code> object is created, the login timeout is initially zero.
*
* @return the data source login time limit
* @throws java.sql.SQLException if a database access error occurs.
* @see #setLoginTimeout
* @since 1.4
*/
public int getLoginTimeout() throws SQLException {
return 0;
}
/**
* Returns an object that implements the given interface to allow access to non-standard methods, or standard
* methods not exposed by the proxy.
*
* If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the
* receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a
* proxy for the wrapped object. Otherwise return the the result of calling <code>unwrap</code> recursively on the
* wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface,
* then an <code>SQLException</code> is thrown.
*
* @param iface A Class defining an interface that the result must implement.
* @return an object that implements the interface. May be a proxy for the actual implementing object.
* @throws java.sql.SQLException If no object found that implements the interface
* @since 1.6
*/
public <T> T unwrap(final Class<T> iface) throws SQLException {
return null;
}
/**
* Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an
* object that does. Returns false otherwise. If this implements the interface then return true, else if this is a
* wrapper then return the result of recursively calling <code>isWrapperFor</code> on the wrapped object. If this
* does not implement the interface and is not a wrapper, return false. This method should be implemented as a
* low-cost operation compared to <code>unwrap</code> so that callers can use this method to avoid expensive
* <code>unwrap</code> calls that may fail. If this method returns true then calling <code>unwrap</code> with the
* same argument should succeed.
*
* @param iface a Class defining an interface.
* @return true if this implements the interface or directly or indirectly wraps an object that does.
* @throws java.sql.SQLException if an error occurs while determining whether this is a wrapper for an object with
* the given interface.
* @since 1.6
*/
public boolean isWrapperFor(final Class<?> iface) throws SQLException {
return false;
}
/**
* Attempts to establish a physical database connection that can
* be used as a pooled connection.
*
* @return a <code>PooledConnection</code> object that is a physical
* connection to the database that this
* <code>ConnectionPoolDataSource</code> object represents
* @throws java.sql.SQLException if a database access error occurs
* if the JDBC driver does not support
* this method
* @since 1.4
*/
public PooledConnection getPooledConnection() throws SQLException {
return new MySQLPooledConnection((MySQLConnection)getConnection());
}
/**
* Attempts to establish a physical database connection that can
* be used as a pooled connection.
*
* @param user the database user on whose behalf the connection is being made
* @param password the user's password
* @return a <code>PooledConnection</code> object that is a physical
* connection to the database that this
* <code>ConnectionPoolDataSource</code> object represents
* @throws java.sql.SQLException if a database access error occurs
* @since 1.4
*/
public PooledConnection getPooledConnection(String user, String password) throws SQLException {
return new MySQLPooledConnection((MySQLConnection)getConnection(user,password));
}
public XAConnection getXAConnection() throws SQLException {
return new MySQLXAConnection((MySQLConnection)getConnection());
}
public XAConnection getXAConnection(String user, String password) throws SQLException {
return new MySQLXAConnection((MySQLConnection)getConnection(user,password));
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
}