/
FBSimpleDataSource.java
542 lines (453 loc) · 14.3 KB
/
FBSimpleDataSource.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Firebird Open Source JavaEE Connector - JDBC Driver
*
* Distributable under LGPL license.
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
*
* This program 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
* LGPL License for more details.
*
* This file was created by members of the firebird development team.
* All individual contributions remain the Copyright (C) of those
* individuals. Contributors to this file are either listed here or
* can be obtained from a source control history command.
*
* All rights reserved.
*/
package org.firebirdsql.ds;
import org.firebirdsql.gds.DatabaseParameterBuffer;
import org.firebirdsql.gds.TransactionParameterBuffer;
import org.firebirdsql.gds.impl.GDSFactory;
import org.firebirdsql.gds.impl.GDSType;
import org.firebirdsql.jca.FBManagedConnectionFactory;
import org.firebirdsql.jdbc.FBDataSource;
import org.firebirdsql.jdbc.FirebirdConnectionProperties;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.resource.Referenceable;
import javax.resource.ResourceException;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
/**
* This is a simple implementation of {@link DataSource} interface. Connections
* are physically opened in {@link DataSource#getConnection()} method and
* physically closed in {@link Connection#close()} method.
*
* @author <a href="mailto:rrokytskyy@users.sourceforge.net">Roman Rokytskyy</a>
* @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
* @version 1.0
*/
public class FBSimpleDataSource extends RootCommonDataSource implements DataSource, Serializable, Referenceable, FirebirdConnectionProperties {
private static final long serialVersionUID = 3156578540634970427L;
protected FBManagedConnectionFactory mcf;
protected transient FBDataSource ds;
protected Reference jndiReference;
protected String description;
/**
* Create instance of this class.
*/
public FBSimpleDataSource() {
this(GDSFactory.getDefaultGDSType());
}
/**
* Create instance of this class.
*/
public FBSimpleDataSource(GDSType type) {
mcf = new FBManagedConnectionFactory(type);
}
/**
* Get buffer length for the BLOB fields.
*
* @return length of BLOB buffer.
*/
public Integer getBlobBufferLength() {
return mcf.getBlobBufferSize();
}
/**
* Set BLOB buffer length. This value influences the performance when
* working with BLOB fields.
*
* @param length new length of the BLOB buffer.
*/
public void setBlobBufferLength(Integer length) {
mcf.setBlobBufferSize(length);
}
/**
* Get name of the database.
*
* @return database name, value is equal to the part of full JDBC URL without
* the {@code jdbc:firebirdsql:} part.
*
* @deprecated use {@link #getDatabase} instead for the sake of naming
* compatibility.
*/
@Deprecated
public String getDatabaseName() {
return getDatabase();
}
/**
* Set database name.
*
* @param name connection URL without {@code "jdbc:firebirdsql:"}
* prefix ({@code "//localhost:3050/c:/database/employee.fdb"}) for
* example).
*
* @deprecated use {@link #setDatabase(String)} instead for the sake of
* naming compatibility.
*/
@Deprecated
public void setDatabaseName(String name) {
setDatabase(name);
}
/**
* Get name of the database.
*
* @return database name, value is equal to the part of full JDBC URL without
* the {@code jdbc:firebirdsql:} part.
*/
public String getDatabase() {
return mcf.getDatabase();
}
/**
* Set database name.
*
* @param name connection URL without {@code "jdbc:firebirdsql:"}
* prefix ({@code "//localhost:3050/c:/database/employee.fdb"}) for
* example).
*/
public void setDatabase(String name) {
mcf.setDatabase(name);
}
/**
* Get user name that is used in {@link #getConnection()} method.
*
* @return default user name.
*
* @deprecated use {@link #getUserName()} instead for the sake of naming
* compatibility.
*/
@Deprecated
public String getUser() {
return getUserName();
}
/**
* Set user name that will be used in {@link #getConnection()} method.
*
* @param user default user name.
*
* @deprecated use {@link #setUserName(String)} instead for the sake of
* naming compatibility.
*/
@Deprecated
public void setUser(String user) {
setUserName(user);
}
/**
* Get user name that is used in {@link #getConnection()} method.
*
* @return default user name.
*/
public String getUserName() {
return mcf.getUserName();
}
/**
* Set user name that will be used in {@link #getConnection()} method.
*
* @param userName default user name.
*/
public void setUserName(String userName) {
mcf.setUserName(userName);
}
/**
* Get password used in {@link #getConnection()} method.
*
* @return password corresponding to the user name returned by
* {@link #getUserName()}.
*/
public String getPassword() {
return mcf.getPassword();
}
/**
* Set password that will be used in the {@link #getConnection()} method.
*
* @param password password corresponding to the user name set in
* {@link #setUserName(String)}.
*/
public void setPassword(String password) {
mcf.setPassword(password);
}
/**
* Get encoding for connections produced by this data source.
*
* @return encoding for the connection.
*/
public String getEncoding() {
return mcf.getEncoding();
}
/**
* Set encoding for connections produced by this data source.
*
* @param encoding encoding for the connection.
*/
public void setEncoding(String encoding) {
mcf.setEncoding(encoding);
}
public String getTpbMapping() {
return mcf.getTpbMapping();
}
public void setTpbMapping(String tpbMapping) {
mcf.setTpbMapping(tpbMapping);
}
public int getBlobBufferSize() {
return mcf.getBlobBufferSize();
}
public int getBuffersNumber() {
return mcf.getBuffersNumber();
}
public String getCharSet() {
return mcf.getCharSet();
}
public DatabaseParameterBuffer getDatabaseParameterBuffer() throws SQLException {
return mcf.getDatabaseParameterBuffer();
}
public String getDefaultIsolation() {
return mcf.getDefaultIsolation();
}
public int getDefaultTransactionIsolation() {
return mcf.getDefaultTransactionIsolation();
}
public String getNonStandardProperty(String key) {
return mcf.getNonStandardProperty(key);
}
public String getRoleName() {
return mcf.getRoleName();
}
public int getSocketBufferSize() {
return mcf.getSocketBufferSize();
}
public String getSqlDialect() {
return mcf.getSqlDialect();
}
public TransactionParameterBuffer getTransactionParameters(int isolation) {
return mcf.getTransactionParameters(isolation);
}
public String getType() {
return mcf.getType();
}
public boolean isTimestampUsesLocalTimezone() {
return mcf.isTimestampUsesLocalTimezone();
}
public boolean isUseStandardUdf() {
return mcf.isUseStandardUdf();
}
public boolean isUseStreamBlobs() {
return mcf.isUseStreamBlobs();
}
public void setBlobBufferSize(int bufferSize) {
mcf.setBlobBufferSize(bufferSize);
}
public void setBuffersNumber(int buffersNumber) {
mcf.setBuffersNumber(buffersNumber);
}
public void setCharSet(String charSet) {
mcf.setCharSet(charSet);
}
public void setDefaultIsolation(String isolation) {
mcf.setDefaultIsolation(isolation);
}
public void setDefaultTransactionIsolation(int defaultIsolationLevel) {
mcf.setDefaultTransactionIsolation(defaultIsolationLevel);
}
public void setNonStandardProperty(String key, String value) {
mcf.setNonStandardProperty(key, value);
}
public void setNonStandardProperty(String propertyMapping) {
mcf.setNonStandardProperty(propertyMapping);
}
public void setRoleName(String roleName) {
mcf.setRoleName(roleName);
}
public void setSocketBufferSize(int socketBufferSize) {
mcf.setSocketBufferSize(socketBufferSize);
}
public void setSqlDialect(String sqlDialect) {
mcf.setSqlDialect(sqlDialect);
}
public void setTimestampUsesLocalTimezone(boolean timestampUsesLocalTimezone) {
mcf.setTimestampUsesLocalTimezone(timestampUsesLocalTimezone);
}
public void setTransactionParameters(int isolation, TransactionParameterBuffer tpb) {
mcf.setTransactionParameters(isolation, tpb);
}
public void setType(String type) {
mcf.setType(type);
}
public void setUseStandardUdf(boolean useStandardUdf) {
mcf.setUseStandardUdf(useStandardUdf);
}
public void setUseStreamBlobs(boolean useStreamBlobs) {
mcf.setUseStreamBlobs(useStreamBlobs);
}
public boolean isDefaultResultSetHoldable() {
return mcf.isDefaultResultSetHoldable();
}
public void setDefaultResultSetHoldable(boolean isHoldable) {
mcf.setDefaultResultSetHoldable(isHoldable);
}
public int getSoTimeout() {
return mcf.getSoTimeout();
}
public void setSoTimeout(int soTimeout) {
mcf.setSoTimeout(soTimeout);
}
public int getConnectTimeout() {
return mcf.getConnectTimeout();
}
public void setConnectTimeout(int connectTimeout) {
mcf.setConnectTimeout(connectTimeout);
}
@Override
public boolean isUseFirebirdAutocommit() {
return mcf.isUseFirebirdAutocommit();
}
@Override
public void setUseFirebirdAutocommit(boolean useFirebirdAutocommit) {
mcf.setUseFirebirdAutocommit(useFirebirdAutocommit);
}
@Override
public String getWireCrypt() {
return mcf.getWireCrypt();
}
@Override
public void setWireCrypt(String wireCrypt) {
mcf.setWireCrypt(wireCrypt);
}
@Override
public String getDbCryptConfig() {
return mcf.getDbCryptConfig();
}
@Override
public void setDbCryptConfig(String dbCryptConfig) {
mcf.setDbCryptConfig(dbCryptConfig);
}
@Override
public String getAuthPlugins() {
return mcf.getAuthPlugins();
}
@Override
public void setAuthPlugins(String authPlugins) {
mcf.setAuthPlugins(authPlugins);
}
/*
* INTERFACES IMPLEMENTATION
*/
/**
* Get previously set JNDI reference.
*
* @return instance of {@link Reference} set previously.
*
* @throws NamingException if something went wrong.
*/
public Reference getReference() throws NamingException {
return jndiReference;
}
/**
* Set JNDI reference for this data source.
*
* @param reference reference to set.
*/
public void setReference(Reference reference) {
jndiReference = reference;
}
/**
* Get JDBC connection with default credentials.
*
* @return new JDBC connection.
*
* @throws SQLException if something went wrong.
*/
public Connection getConnection() throws SQLException {
return getDataSource().getConnection();
}
/**
* Get JDBC connection with the specified credentials.
*
* @param username user name for the connection.
* @param password password for the connection.
*
* @return new JDBC connection.
*
* @throws SQLException if something went wrong.
*/
public Connection getConnection(String username, String password) throws SQLException {
return getDataSource().getConnection(username, password);
}
/**
* {@inheritDoc}
* <p>
* This property is an alias for the connectTimeout property.
* </p>
*/
public int getLoginTimeout() throws SQLException {
return getConnectTimeout();
}
/**
* {@inheritDoc}
* <p>
* This property is an alias for the connectTimeout property.
* </p>
*/
public void setLoginTimeout(int loginTimeout) throws SQLException {
setConnectTimeout(loginTimeout);
}
/**
* Get description of this datasource.
*
* @return description of this datasource.
*/
public String getDescription() {
return description;
}
/**
* Set description of this datasource.
*
* @param description description of this datasource.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Get underlying connection factory (in our case instance of
* {@link FBDataSource} class) that will provide JDBC connections.
*
* @return JDBC connection factory.
*
* @throws SQLException if something went wrong.
*/
protected synchronized DataSource getDataSource() throws SQLException {
if (ds != null)
return ds;
if (mcf.getDatabase() == null || "".equals(mcf.getDatabase().trim()))
throw new SQLException(
"Database was not specified. Cannot provide connections.");
try {
ds = (FBDataSource)mcf.createConnectionFactory();
return ds;
} catch(ResourceException rex) {
throw new SQLException(rex.getMessage());
}
}
// JDBC 4.0
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface != null && iface.isAssignableFrom(getClass());
}
public <T> T unwrap(Class<T> iface) throws SQLException {
if (!isWrapperFor(iface))
throw new SQLException("Unable to unwrap to class " + iface.getName());
return iface.cast(this);
}
}