Skip to content

Commit 6a6fdc6

Browse files
committed
Don't instantiate an intermediary object when creating DataSource
1 parent 07453c5 commit 6a6fdc6

File tree

1 file changed

+156
-62
lines changed

1 file changed

+156
-62
lines changed

src/main/java/org/openjax/dbcp/DataSources.java

+156-62
Original file line numberDiff line numberDiff line change
@@ -362,55 +362,6 @@ public static BasicDataSource createDataSource(final $Dbcp dbcp) {
362362
return createDataSource(null, ClassLoader.getSystemClassLoader(), dbcp);
363363
}
364364

365-
private final ClassLoader driverClassLoader;
366-
private BasicDataSource dataSource = null;
367-
private String driverClassName = null;
368-
private String url = null;
369-
370-
private boolean autoCommit = true;
371-
private boolean readOnly = false;
372-
373-
private Integer queryTimeout = null;
374-
private String transactionIsolation = null;
375-
376-
private int initialSize = 0;
377-
private int minIdle = 0;
378-
private String maxIdle = INDEFINITE;
379-
private String maxTotal = INDEFINITE;
380-
private boolean poolPreparedStatements = false;
381-
private String maxOpen = INDEFINITE;
382-
383-
private boolean lifo = true;
384-
385-
private boolean cacheState = true;
386-
private String maxWait = INDEFINITE;
387-
private String maxConnLifetime = INDEFINITE;
388-
private boolean autoCommitOnReturn = true;
389-
private boolean rollbackOnReturn = true;
390-
private String removeAbandonedOn = null;
391-
private int removeAbandonedTimeout = 0;
392-
private boolean abandonedUsageTracking = false;
393-
private boolean accessToUnderlyingConnectionAllowed = false;
394-
private boolean hasEviction = false;
395-
private String timeBetweenEvictionRunsMillis = INDEFINITE;
396-
private int numTestsPerRun = 3;
397-
private long minEvictableIdleTimeMillis = 1800000;
398-
private String softMinEvictableIdleTimeMillis = INDEFINITE;
399-
private String policyClassName = null;
400-
401-
private boolean hasValidation = false;
402-
private String validationQuery = null;
403-
private String validationQueryTimeout = INDEFINITE;
404-
private boolean testOnCreate = false;
405-
private boolean testOnBorrow = true;
406-
private boolean testOnReturn = false;
407-
private boolean testWhileIdle = false;
408-
private List<String> disconnectionQueryCodes = null;
409-
410-
private String loggingLevel = null;
411-
private boolean logExpiredConnections = false;
412-
private boolean logAbandoned = false;
413-
414365
/**
415366
* Create a {@link BasicDataSource} from the configuration supplied by the array of {@link Dbcp dbcp} JAX-B bindings that match the
416367
* specified {@code id}.
@@ -423,7 +374,7 @@ public static BasicDataSource createDataSource(final $Dbcp dbcp) {
423374
* @throws IllegalArgumentException If the {@code /dbcp:jdbc} element is missing from all members in {@code dbcps}.
424375
*/
425376
public static BasicDataSource createDataSource(final String id, final ClassLoader driverClassLoader, final Dbcp ... dbcps) {
426-
return new DataSources(id, driverClassLoader, dbcps).build();
377+
return DataSources(id, driverClassLoader, dbcps);
427378
}
428379

429380
/**
@@ -438,11 +389,58 @@ public static BasicDataSource createDataSource(final String id, final ClassLoade
438389
* @throws IllegalArgumentException If the {@code /dbcp:jdbc} element is missing from all members in {@code dbcps}.
439390
*/
440391
public static BasicDataSource createDataSource(final String id, final ClassLoader driverClassLoader, final $Dbcp ... dbcps) {
441-
return new DataSources(id, driverClassLoader, dbcps).build();
392+
return DataSources(id, driverClassLoader, dbcps);
442393
}
443394

444-
private DataSources(final String id, final ClassLoader driverClassLoader, final Dbcp ... dbcps) {
445-
this.driverClassLoader = driverClassLoader;
395+
static BasicDataSource DataSources(final String id, final ClassLoader driverClassLoader, final Dbcp ... dbcps) {
396+
BasicDataSource dataSource = null;
397+
String driverClassName = null;
398+
String url = null;
399+
400+
boolean autoCommit = true;
401+
boolean readOnly = false;
402+
403+
Integer queryTimeout = null;
404+
String transactionIsolation = null;
405+
406+
int initialSize = 0;
407+
int minIdle = 0;
408+
String maxIdle = INDEFINITE;
409+
String maxTotal = INDEFINITE;
410+
boolean poolPreparedStatements = false;
411+
String maxOpen = INDEFINITE;
412+
413+
boolean lifo = true;
414+
415+
boolean cacheState = true;
416+
String maxWait = INDEFINITE;
417+
String maxConnLifetime = INDEFINITE;
418+
boolean autoCommitOnReturn = true;
419+
boolean rollbackOnReturn = true;
420+
String removeAbandonedOn = null;
421+
int removeAbandonedTimeout = 0;
422+
boolean abandonedUsageTracking = false;
423+
boolean accessToUnderlyingConnectionAllowed = false;
424+
boolean hasEviction = false;
425+
String timeBetweenEvictionRunsMillis = INDEFINITE;
426+
int numTestsPerRun = 3;
427+
long minEvictableIdleTimeMillis = 1800000;
428+
String softMinEvictableIdleTimeMillis = INDEFINITE;
429+
String policyClassName = null;
430+
431+
boolean hasValidation = false;
432+
String validationQuery = null;
433+
String validationQueryTimeout = INDEFINITE;
434+
boolean testOnCreate = false;
435+
boolean testOnBorrow = true;
436+
boolean testOnReturn = false;
437+
boolean testWhileIdle = false;
438+
List<String> disconnectionQueryCodes = null;
439+
440+
String loggingLevel = null;
441+
boolean logExpiredConnections = false;
442+
boolean logAbandoned = false;
443+
446444
for (final Dbcp dbcp : dbcps) { // [A]
447445
if (id != null && !id.equals(dbcp.getId()))
448446
continue;
@@ -494,13 +492,13 @@ private DataSources(final String id, final ClassLoader driverClassLoader, final
494492
if (CollectionUtil.isRandomAccess(properties)) {
495493
int i = 0;
496494
do // [RA]
497-
add(properties.get(i));
495+
add(dataSource, properties.get(i));
498496
while (++i < i$);
499497
}
500498
else {
501499
final Iterator<Dbcp.Connection.Properties.Property> it = properties.iterator();
502500
do // [I]
503-
add(it.next());
501+
add(dataSource, it.next());
504502
while (it.hasNext());
505503
}
506504
}
@@ -667,24 +665,73 @@ else if ("fifo".equals(queue))
667665

668666
dataSource.setJmxName(dbcp.getJmxName());
669667
}
668+
669+
return build(driverClassLoader, dataSource, driverClassName, url, autoCommit, readOnly, queryTimeout, transactionIsolation, initialSize, minIdle, maxIdle, maxTotal, poolPreparedStatements, maxOpen, lifo, cacheState, maxWait, maxConnLifetime, autoCommitOnReturn, rollbackOnReturn, removeAbandonedOn, removeAbandonedTimeout, abandonedUsageTracking, accessToUnderlyingConnectionAllowed, hasEviction, timeBetweenEvictionRunsMillis, numTestsPerRun, minEvictableIdleTimeMillis, softMinEvictableIdleTimeMillis, policyClassName, hasValidation, validationQuery, validationQueryTimeout, testOnCreate, testOnBorrow, testOnReturn, testWhileIdle, disconnectionQueryCodes, loggingLevel, logExpiredConnections, logAbandoned);
670670
}
671671

672-
private void add(final $Dbcp.Connection.Properties.Property property) {
672+
private static void add(final BasicDataSource dataSource, final $Dbcp.Connection.Properties.Property property) {
673673
final $Dbcp.Connection.Properties.Property.Name$ name = property.getName$();
674674
final $Dbcp.Connection.Properties.Property.Value$ value = property.getValue$();
675675
if (name != null && value != null)
676676
dataSource.addConnectionProperty(name.text(), value.text());
677677
}
678678

679-
private void add(final Dbcp.Connection.Properties.Property property) {
679+
private static void add(final BasicDataSource dataSource, final Dbcp.Connection.Properties.Property property) {
680680
final String name = property.getName();
681681
final String value = property.getValue();
682682
if (name != null && value != null)
683683
dataSource.addConnectionProperty(name, value);
684684
}
685685

686-
private DataSources(final String id, final ClassLoader driverClassLoader, final $Dbcp ... dbcps) {
687-
this.driverClassLoader = driverClassLoader;
686+
static BasicDataSource DataSources(final String id, final ClassLoader driverClassLoader, final $Dbcp ... dbcps) {
687+
BasicDataSource dataSource = null;
688+
String driverClassName = null;
689+
String url = null;
690+
691+
boolean autoCommit = true;
692+
boolean readOnly = false;
693+
694+
Integer queryTimeout = null;
695+
String transactionIsolation = null;
696+
697+
int initialSize = 0;
698+
int minIdle = 0;
699+
String maxIdle = INDEFINITE;
700+
String maxTotal = INDEFINITE;
701+
boolean poolPreparedStatements = false;
702+
String maxOpen = INDEFINITE;
703+
704+
boolean lifo = true;
705+
706+
boolean cacheState = true;
707+
String maxWait = INDEFINITE;
708+
String maxConnLifetime = INDEFINITE;
709+
boolean autoCommitOnReturn = true;
710+
boolean rollbackOnReturn = true;
711+
String removeAbandonedOn = null;
712+
int removeAbandonedTimeout = 0;
713+
boolean abandonedUsageTracking = false;
714+
boolean accessToUnderlyingConnectionAllowed = false;
715+
boolean hasEviction = false;
716+
String timeBetweenEvictionRunsMillis = INDEFINITE;
717+
int numTestsPerRun = 3;
718+
long minEvictableIdleTimeMillis = 1800000;
719+
String softMinEvictableIdleTimeMillis = INDEFINITE;
720+
String policyClassName = null;
721+
722+
boolean hasValidation = false;
723+
String validationQuery = null;
724+
String validationQueryTimeout = INDEFINITE;
725+
boolean testOnCreate = false;
726+
boolean testOnBorrow = true;
727+
boolean testOnReturn = false;
728+
boolean testWhileIdle = false;
729+
List<String> disconnectionQueryCodes = null;
730+
731+
String loggingLevel = null;
732+
boolean logExpiredConnections = false;
733+
boolean logAbandoned = false;
734+
688735
for (final $Dbcp dbcp : dbcps) { // [A]
689736
final $Dbcp.Id$ id$ = dbcp.getId$();
690737
if (id != null && (id$ == null || !id.equals(id$.text())))
@@ -737,13 +784,13 @@ private DataSources(final String id, final ClassLoader driverClassLoader, final
737784
if (CollectionUtil.isRandomAccess(properties)) {
738785
int i = 0;
739786
do // [RA]
740-
add(properties.get(i));
787+
add(dataSource, properties.get(i));
741788
while (++i < i$);
742789
}
743790
else {
744791
final Iterator<$Dbcp.Connection.Properties.Property> it = properties.iterator();
745792
do // [I]
746-
add(it.next());
793+
add(dataSource, it.next());
747794
while (it.hasNext());
748795
}
749796
}
@@ -931,9 +978,53 @@ else if ("fifo".equals(text))
931978
if (dbcp.getJmxName() != null)
932979
dataSource.setJmxName(dbcp.getJmxName().text());
933980
}
981+
982+
return build(driverClassLoader, dataSource, driverClassName, url, autoCommit, readOnly, queryTimeout, transactionIsolation, initialSize, minIdle, maxIdle, maxTotal, poolPreparedStatements, maxOpen, lifo, cacheState, maxWait, maxConnLifetime, autoCommitOnReturn, rollbackOnReturn, removeAbandonedOn, removeAbandonedTimeout, abandonedUsageTracking, accessToUnderlyingConnectionAllowed, hasEviction, timeBetweenEvictionRunsMillis, numTestsPerRun, minEvictableIdleTimeMillis, softMinEvictableIdleTimeMillis, policyClassName, hasValidation, validationQuery, validationQueryTimeout, testOnCreate, testOnBorrow, testOnReturn, testWhileIdle, disconnectionQueryCodes, loggingLevel, logExpiredConnections, logAbandoned);
934983
}
935984

936-
private BasicDataSource build() {
985+
private static BasicDataSource build(
986+
final ClassLoader driverClassLoader,
987+
final BasicDataSource dataSource,
988+
final String driverClassName,
989+
final String url,
990+
final boolean autoCommit,
991+
final boolean readOnly,
992+
final Integer queryTimeout,
993+
final String transactionIsolation,
994+
final int initialSize,
995+
final int minIdle,
996+
final String maxIdle,
997+
final String maxTotal,
998+
final boolean poolPreparedStatements,
999+
final String maxOpen,
1000+
final boolean lifo,
1001+
final boolean cacheState,
1002+
final String maxWait,
1003+
final String maxConnLifetime,
1004+
final boolean autoCommitOnReturn,
1005+
final boolean rollbackOnReturn,
1006+
final String removeAbandonedOn,
1007+
final int removeAbandonedTimeout,
1008+
final boolean abandonedUsageTracking,
1009+
final boolean accessToUnderlyingConnectionAllowed,
1010+
final boolean hasEviction,
1011+
final String timeBetweenEvictionRunsMillis,
1012+
final int numTestsPerRun,
1013+
final long minEvictableIdleTimeMillis,
1014+
final String softMinEvictableIdleTimeMillis,
1015+
final String policyClassName,
1016+
final boolean hasValidation,
1017+
final String validationQuery,
1018+
final String validationQueryTimeout,
1019+
final boolean testOnCreate,
1020+
final boolean testOnBorrow,
1021+
final boolean testOnReturn,
1022+
final boolean testWhileIdle,
1023+
final List<String> disconnectionQueryCodes,
1024+
final String loggingLevel,
1025+
final boolean logExpiredConnections,
1026+
final boolean logAbandoned
1027+
) {
9371028
if (dataSource == null)
9381029
return null;
9391030

@@ -1032,4 +1123,7 @@ else if ("maintenance".equals(removeAbandonedOn))
10321123

10331124
return dataSource;
10341125
}
1126+
1127+
private DataSources() {
1128+
}
10351129
}

0 commit comments

Comments
 (0)