You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2015/05/19 11:19:25 UTC
[2/3] cayenne git commit: CAY-2010 DataSourceBuilder to help users
create pooling and non-pooling DataSources
CAY-2010 DataSourceBuilder to help users create pooling and non-pooling DataSources
* splitting pooling and non-pooling flow
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/246aae96
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/246aae96
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/246aae96
Branch: refs/heads/master
Commit: 246aae96461601fc388bfb24be0e783235b2503d
Parents: e3eb315
Author: aadamchik <aa...@apache.org>
Authored: Tue May 19 11:42:20 2015 +0300
Committer: aadamchik <aa...@apache.org>
Committed: Tue May 19 12:07:04 2015 +0300
----------------------------------------------------------------------
.../apache/cayenne/configuration/Constants.java | 15 +++-
.../server/PropertyDataSourceFactory.java | 8 +-
.../server/ServerRuntimeBuilder.java | 10 +++
.../server/XMLPoolingDataSourceFactory.java | 7 +-
.../cayenne/datasource/DataSourceBuilder.java | 62 ++-----------
.../datasource/PoolingDataSourceBuilder.java | 95 ++++++++++++++++++++
.../di/server/ServerCaseDataSourceFactory.java | 3 +-
docs/doc/src/main/resources/RELEASE-NOTES.txt | 2 +-
8 files changed, 138 insertions(+), 64 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/main/java/org/apache/cayenne/configuration/Constants.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/Constants.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/Constants.java
index a94f19b..6497630 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/Constants.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/Constants.java
@@ -92,6 +92,17 @@ public interface Constants {
public static final String JDBC_MAX_CONNECTIONS_PROPERTY = "cayenne.jdbc.max_connections";
/**
+ * Defines a maximum time in milliseconds that a connection request could
+ * wait in the connection queue. After this period expires, an exception
+ * will be thrown in the calling method. A value of zero will make the
+ * thread wait until a connection is available with no time out. Defaults to
+ * 20 seconds.
+ *
+ * @since 4.0
+ */
+ public static final String JDBC_MAX_QUEUE_WAIT_TIME = "cayenne.jdbc.max_wait";
+
+ /**
* @since 4.0
*/
public static final String JDBC_VALIDATION_QUERY_PROPERTY = "cayenne.jdbc.validation_query";
@@ -170,8 +181,10 @@ public interface Constants {
* will be thrown in the calling method. A value of zero will make the
* thread wait until a connection is available with no time out. Defaults to
* 20 seconds.
+ *
+ * @deprecated since 4.0 renamed to {@link #JDBC_MAX_QUEUE_WAIT_TIME}. Property name is preserved.
*/
- public static final String SERVER_MAX_QUEUE_WAIT_TIME = "cayenne.jdbc.max_wait";
+ public static final String SERVER_MAX_QUEUE_WAIT_TIME = JDBC_MAX_QUEUE_WAIT_TIME;
/** Defines if database uses case-insensitive collation */
public final static String CI_PROPERTY = "cayenne.runtime.db.collation.assume.ci";
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
index 4bd2dbb..e060a51 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
@@ -67,14 +67,14 @@ public class PropertyDataSourceFactory implements DataSourceFactory {
String password = getProperty(Constants.JDBC_PASSWORD_PROPERTY, suffix);
int minConnections = getIntProperty(Constants.JDBC_MIN_CONNECTIONS_PROPERTY, suffix, 1);
int maxConnections = getIntProperty(Constants.JDBC_MAX_CONNECTIONS_PROPERTY, suffix, 1);
- long maxQueueWaitTime = properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME,
+ long maxQueueWaitTime = properties.getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME,
PoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);
String validationQuery = properties.get(Constants.JDBC_VALIDATION_QUERY_PROPERTY);
Driver driver = objectFactory.newInstance(Driver.class, driverClass);
- return DataSourceBuilder.url(url).driver(driver).userName(username)
- .password(password).minConnections(minConnections).maxConnections(maxConnections)
- .maxQueueWaitTime(maxQueueWaitTime).validationQuery(validationQuery).build();
+ return DataSourceBuilder.url(url).driver(driver).userName(username).password(password)
+ .pool(minConnections, maxConnections).maxQueueWaitTime(maxQueueWaitTime)
+ .validationQuery(validationQuery).build();
}
protected int getIntProperty(String propertyName, String suffix, int defaultValue) {
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
index 522d8b0..ea649eb 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
@@ -54,6 +54,7 @@ public class ServerRuntimeBuilder {
private String jdbcPassword;
private int jdbcMinConnections;
private int jdbcMaxConnections;
+ private long maxQueueWaitTime;
private String validationQuery;
public static ServerRuntimeBuilder builder() {
@@ -132,6 +133,11 @@ public class ServerRuntimeBuilder {
this.validationQuery = validationQuery;
return this;
}
+
+ public ServerRuntimeBuilder maxQueueWaitTime(long maxQueueWaitTime) {
+ this.maxQueueWaitTime = maxQueueWaitTime;
+ return this;
+ }
/**
* Sets a user name for the default DataSource.
@@ -255,6 +261,10 @@ public class ServerRuntimeBuilder {
if (jdbcMaxConnections > 0) {
props.put(Constants.JDBC_MAX_CONNECTIONS_PROPERTY, Integer.toString(jdbcMaxConnections));
}
+
+ if (maxQueueWaitTime > 0) {
+ props.put(Constants.JDBC_MAX_QUEUE_WAIT_TIME, Long.toString(maxQueueWaitTime));
+ }
if (validationQuery != null) {
props.put(Constants.JDBC_VALIDATION_QUERY_PROPERTY, validationQuery);
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java
index caec026..d41ab97 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java
@@ -64,14 +64,15 @@ public class XMLPoolingDataSourceFactory implements DataSourceFactory {
throw new ConfigurationException(message);
}
- long maxQueueWaitTime = properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME,
+ long maxQueueWaitTime = properties.getLong(Constants.JDBC_MAX_QUEUE_WAIT_TIME,
PoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);
Driver driver = objectFactory.newInstance(Driver.class, descriptor.getJdbcDriver());
return DataSourceBuilder.url(descriptor.getDataSourceUrl()).driver(driver).userName(descriptor.getUserName())
- .password(descriptor.getPassword()).minConnections(descriptor.getMinConnections())
- .maxConnections(descriptor.getMaxConnections()).maxQueueWaitTime(maxQueueWaitTime).build();
+ .password(descriptor.getPassword())
+ .pool(descriptor.getMinConnections(), descriptor.getMaxConnections())
+ .maxQueueWaitTime(maxQueueWaitTime).build();
}
}
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/main/java/org/apache/cayenne/datasource/DataSourceBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/datasource/DataSourceBuilder.java b/cayenne-server/src/main/java/org/apache/cayenne/datasource/DataSourceBuilder.java
index 5e1b33d..e8c8104 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/datasource/DataSourceBuilder.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/datasource/DataSourceBuilder.java
@@ -37,20 +37,13 @@ public class DataSourceBuilder {
private String driverClassName;
private Driver driver;
private String url;
- private PoolingDataSourceParameters poolParameters;
public static DataSourceBuilder url(String url) {
return new DataSourceBuilder(url);
}
private DataSourceBuilder(String url) {
-
this.url = url;
- this.poolParameters = new PoolingDataSourceParameters();
-
- poolParameters.setMinConnections(1);
- poolParameters.setMaxConnections(1);
- poolParameters.setMaxQueueWaitTime(PoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);
}
public DataSourceBuilder userName(String userName) {
@@ -76,59 +69,22 @@ public class DataSourceBuilder {
return this;
}
- public DataSourceBuilder minConnections(int minConnections) {
- poolParameters.setMinConnections(minConnections);
- return this;
- }
-
- public DataSourceBuilder maxConnections(int maxConnections) {
- poolParameters.setMaxConnections(maxConnections);
- return this;
- }
-
- public DataSourceBuilder maxQueueWaitTime(long maxQueueWaitTime) {
- poolParameters.setMaxQueueWaitTime(maxQueueWaitTime);
- return this;
- }
-
- public DataSourceBuilder validationQuery(String validationQuery) {
- poolParameters.setValidationQuery(validationQuery);
- return this;
+ /**
+ * Turns produced DataSource into a pooled DataSource.
+ */
+ public PoolingDataSourceBuilder pool(int minConnection, int maxConnections) {
+ return new PoolingDataSourceBuilder(this);
}
+ /**
+ * Builds a non-pooling DataSource. To create connection pool use
+ * {@link #pool(int, int)} method.
+ */
public DataSource build() {
-
- // sanity checks...
- if (poolParameters.getMaxConnections() < 0) {
- throw new CayenneRuntimeException("Maximum number of connections can not be negative ("
- + poolParameters.getMaxConnections() + ").");
- }
-
- if (poolParameters.getMinConnections() < 0) {
- throw new CayenneRuntimeException("Minimum number of connections can not be negative ("
- + poolParameters.getMinConnections() + ").");
- }
-
- if (poolParameters.getMinConnections() > poolParameters.getMaxConnections()) {
- throw new CayenneRuntimeException("Minimum number of connections can not be bigger then maximum.");
- }
-
- return buildManaged(buildPooling(buildNonPooling()));
- }
-
- private DataSource buildNonPooling() {
Driver driver = loadDriver();
return new DriverDataSource(driver, url, userName, password);
}
- private PoolingDataSource buildPooling(DataSource nonPoolingDataSource) {
- return new PoolingDataSource(nonPoolingDataSource, poolParameters);
- }
-
- private DataSource buildManaged(PoolingDataSource dataSource) {
- return new ManagedPoolingDataSource(dataSource);
- }
-
private Driver loadDriver() {
if (driver != null) {
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/main/java/org/apache/cayenne/datasource/PoolingDataSourceBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/datasource/PoolingDataSourceBuilder.java b/cayenne-server/src/main/java/org/apache/cayenne/datasource/PoolingDataSourceBuilder.java
new file mode 100644
index 0000000..5106ea2
--- /dev/null
+++ b/cayenne-server/src/main/java/org/apache/cayenne/datasource/PoolingDataSourceBuilder.java
@@ -0,0 +1,95 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.datasource;
+
+import javax.sql.DataSource;
+
+import org.apache.cayenne.CayenneRuntimeException;
+
+/**
+ * Turns unpooled DataSource to a connection pool. Normally you won't be
+ * creating this builder explicitly. Call
+ * {@link DataSourceBuilder#pool(int, int)} method instead.
+ *
+ * @since 4.0
+ */
+public class PoolingDataSourceBuilder {
+
+ private DataSourceBuilder nonPoolingBuilder;
+ private PoolingDataSourceParameters poolParameters;
+
+ public PoolingDataSourceBuilder(DataSourceBuilder nonPoolingBuilder) {
+ this.nonPoolingBuilder = nonPoolingBuilder;
+ this.poolParameters = new PoolingDataSourceParameters();
+
+ poolParameters.setMinConnections(1);
+ poolParameters.setMaxConnections(1);
+ poolParameters.setMaxQueueWaitTime(PoolingDataSource.MAX_QUEUE_WAIT_DEFAULT);
+ }
+
+ public PoolingDataSourceBuilder minConnections(int minConnections) {
+ poolParameters.setMinConnections(minConnections);
+ return this;
+ }
+
+ public PoolingDataSourceBuilder maxConnections(int maxConnections) {
+ poolParameters.setMaxConnections(maxConnections);
+ return this;
+ }
+
+ public PoolingDataSourceBuilder maxQueueWaitTime(long maxQueueWaitTime) {
+ poolParameters.setMaxQueueWaitTime(maxQueueWaitTime);
+ return this;
+ }
+
+ public PoolingDataSourceBuilder validationQuery(String validationQuery) {
+ poolParameters.setValidationQuery(validationQuery);
+ return this;
+ }
+
+ public DataSource build() {
+
+ // sanity checks...
+ if (poolParameters.getMaxConnections() < 0) {
+ throw new CayenneRuntimeException("Maximum number of connections can not be negative ("
+ + poolParameters.getMaxConnections() + ").");
+ }
+
+ if (poolParameters.getMinConnections() < 0) {
+ throw new CayenneRuntimeException("Minimum number of connections can not be negative ("
+ + poolParameters.getMinConnections() + ").");
+ }
+
+ if (poolParameters.getMinConnections() > poolParameters.getMaxConnections()) {
+ throw new CayenneRuntimeException("Minimum number of connections can not be bigger then maximum.");
+ }
+
+ DataSource nonPooling = nonPoolingBuilder.build();
+ return buildManaged(buildPooling(nonPooling));
+ }
+
+ private PoolingDataSource buildPooling(DataSource nonPoolingDataSource) {
+ return new PoolingDataSource(nonPoolingDataSource, poolParameters);
+ }
+
+ private DataSource buildManaged(PoolingDataSource dataSource) {
+ return new ManagedPoolingDataSource(dataSource);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java b/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
index bf43870..8b1308a 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/unit/di/server/ServerCaseDataSourceFactory.java
@@ -71,8 +71,7 @@ public class ServerCaseDataSourceFactory {
return DataSourceBuilder.url(dataSourceInfo.getDataSourceUrl()).driver(driver)
.userName(dataSourceInfo.getUserName()).password(dataSourceInfo.getPassword())
- .minConnections(dataSourceInfo.getMinConnections()).maxConnections(dataSourceInfo.getMaxConnections())
- .build();
+ .pool(dataSourceInfo.getMinConnections(), dataSourceInfo.getMaxConnections()).build();
}
}
http://git-wip-us.apache.org/repos/asf/cayenne/blob/246aae96/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index a47eaec..b6446bb 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -23,7 +23,7 @@ CAY-2004 EJBQL: Support for ordering on aggregate expressions
CAY-2007 Refactoring SelectTranslator for better extensibility
CAY-2008 Connection pool refactoring and validation query support in Cayenne DataSource
CAY-2009 Non-blocking connection pool
-CAY-2010 Splitting DataSourceBuilder from ServerRuntimeBuilder
+CAY-2010 DataSourceBuilder to help users create pooling and non-pooling DataSources
CAY-2011 Support for Java 8 date and time types
CAY-2012 ObjectSelect, SelectById: eliminating methods that reset query state