You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by gk...@apache.org on 2023/10/13 13:35:45 UTC
svn commit: r1912940 - in /db/torque/trunk/torque-runtime/src: conf/ main/java/org/apache/torque/ main/java/org/apache/torque/dsfactory/ main/java/org/apache/torque/dsfactory/converters/ test/resources/
Author: gk
Date: Fri Oct 13 13:35:45 2023
New Revision: 1912940
URL: http://svn.apache.org/viewvc?rev=1912940&view=rev
Log:
- allow Duration text format in configuration
- resolve in TORQUE-362 follow-up error
Added:
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/converters/
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/converters/DurationConverter.java
Modified:
db/torque/trunk/torque-runtime/src/conf/Torque.properties
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
db/torque/trunk/torque-runtime/src/test/resources/Torque.properties
Modified: db/torque/trunk/torque-runtime/src/conf/Torque.properties
URL: http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/conf/Torque.properties?rev=1912940&r1=1912939&r2=1912940&view=diff
==============================================================================
--- db/torque/trunk/torque-runtime/src/conf/Torque.properties (original)
+++ db/torque/trunk/torque-runtime/src/conf/Torque.properties Fri Oct 13 13:35:45 2023
@@ -54,8 +54,9 @@ torque.applicationRoot = .
# Time to wait for a connection to the database in milliseconds.
-# pool2, dbcp 2: maxWait -> maxWaitMillis
-torque.defaults.pool.maxWaitMillis=10000
+# pool2, dbcp 2: maxWait -> maxWaitMillis, which is already deprecated in 2.10
+#torque.defaults.pool.maxWaitMillis=10000
+torque.defaults.pool.defaultMaxWait=PT10S
# Maximum number of idle and active connections cached in a database
# definition.
@@ -64,7 +65,8 @@ torque.defaults.pool.maxWaitMillis=10000
# multiple pools and each has this maximum number. So if you have a
# connection licensed database engine, you must multiply this number by
# the number of times you use a specific database URL.
-torque.defaults.pool.maxIdle = 8
+#torque.defaults.pool.maxIdle = 8
+torque.defaults.pool.defaultMaxIdle = 8
# pool2, dbcp 2: maxActive -> maxTotal
torque.defaults.pool.maxTotal = 10
@@ -73,12 +75,13 @@ torque.defaults.pool.maxTotal = 10
# for too long. Defaults to 5 minutes (5 * 60 * 1000)
# remove property if the idle object evictor should not be run
#torque.defaults.pool.torque.defaults.pool.timeBetweenEvictionRunsMillis= 300000
-# SINCE DBCP 2.9.0 replace with to Duration.parse
-#torque.defaults.pool.torque.defaults.pool.durationBetweenEvictionRuns=
+# SINCE DBCP 2.9.0 replace with Duration.parse
+torque.defaults.pool.defaultDurationBetweenEvictionRuns=PT12S
# Lifetime of an idle connection in the pool in milliseconds.
# Defaults to one hour (1000 * 60 * 60)
-torque.defaults.pool.minEvictableIdleTimeMillis = 3600000
+#torque.defaults.pool.minEvictableIdleTimeMillis = 3600000
+torque.defaults.pool.defaultSoftMinEvictableIdle=PT3600S
# Sets the driver for the data sources.
torque.defaults.connection.driver = org.gjt.mm.mysql.Driver
Modified: db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
URL: http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java?rev=1912940&r1=1912939&r2=1912940&view=diff
==============================================================================
--- db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java (original)
+++ db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java Fri Oct 13 13:35:45 2023
@@ -458,7 +458,7 @@ public class TorqueInstance
String handle = key.substring(0, key.indexOf('.'));
log.debug("handle: {} DataSourceFactory: {}", handle, classname);
Class<?> dsfClass = Class.forName(classname);
- DataSourceFactory dsf = (DataSourceFactory) dsfClass.newInstance();
+ DataSourceFactory dsf = (DataSourceFactory) dsfClass.getDeclaredConstructor( null ).newInstance();
Configuration subConf = c.subset(handle);
dsf.initialize(subConf);
Modified: db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
URL: http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java?rev=1912940&r1=1912939&r2=1912940&view=diff
==============================================================================
--- db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java (original)
+++ db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java Fri Oct 13 13:35:45 2023
@@ -1,5 +1,7 @@
package org.apache.torque.dsfactory;
+import java.time.Duration;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -28,11 +30,12 @@ import org.apache.commons.beanutils.Conv
import org.apache.commons.beanutils.MappedPropertyDescriptor;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.configuration2.Configuration;
-import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.torque.TorqueRuntimeException;
+import org.apache.torque.dsfactory.converters.DurationConverter;
/**
* A class that contains common functionality of the factories in this
@@ -62,6 +65,11 @@ implements DataSourceFactory
public static final String DEFAULT_CONNECTION_KEY
= DEFAULTS_KEY + "." + CONNECTION_KEY;
+
+ /** if format described in {@link Duration#parse(CharSequence)} should be used. */
+ public static boolean USE_DURATION_TEXT_FORMAT = true;
+
+
/** The log */
private static final Logger log = LogManager.getLogger(AbstractDataSourceFactory.class);
@@ -85,6 +93,7 @@ implements DataSourceFactory
String key = property;
Class<?> dsClass = ds.getClass();
int dot = property.indexOf('.');
+
try
{
if (dot > 0)
@@ -118,17 +127,22 @@ implements DataSourceFactory
String value = c.getString(property);
PropertyUtils.setSimpleProperty(ds, property, value);
- log.debug("setSimpleProperty({}, {}, (value not logged))", ds, property);
+ log.debug("setSimpleProperty({}, {}, (value ot logged))", ds, property);
}
else
{
Class<?> propertyType =
+
PropertyUtils.getPropertyType(ds, property);
- Object value =
- ConvertUtils.convert(c.getString(property), propertyType);
+
+ Object value = null;
+ // could use also configuration2 converter
+ // value = (Duration.class.isInstance( propertyType ))? value = c.getDuration( property):
+ value = ConvertUtils.convert(c.getString(property), propertyType);
PropertyUtils.setSimpleProperty(ds, property, value);
- log.debug("setSimpleProperty({}, {}, {})", ds, property, value);
+ log.info("setSimpleProperty({}, {}, {})", propertyType, property, value);
+ log.debug("Result datasource configuration ({})", ds);
}
}
}
@@ -225,7 +239,11 @@ implements DataSourceFactory
}
else
{
+
Configuration conf = c.subset(DEFAULT_POOL_KEY);
+
+ applyAndRemoveGlobal( conf );
+
applyConfiguration(conf, dataSource);
}
@@ -233,6 +251,19 @@ implements DataSourceFactory
applyConfiguration(conf, dataSource);
}
+ public void applyAndRemoveGlobal(Configuration conf)
+ {
+ Configuration global = conf.subset( "global" );
+ USE_DURATION_TEXT_FORMAT = global.getBoolean( "useDurationTextFormat", true );
+ log.info("useDurationTextFormat: {} ", USE_DURATION_TEXT_FORMAT);
+
+ if (ConvertUtils.lookup( Duration.class ) == null) {
+ ConvertUtils.register(
+ USE_DURATION_TEXT_FORMAT? new DurationConverter(USE_DURATION_TEXT_FORMAT): new DurationConverter(), Duration.class);
+ }
+ conf.clearProperty( "global" );
+ }
+
/**
* @return the <code>DataSource</code> configured by the factory.
Added: db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/converters/DurationConverter.java
URL: http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/converters/DurationConverter.java?rev=1912940&view=auto
==============================================================================
--- db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/converters/DurationConverter.java (added)
+++ db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/converters/DurationConverter.java Fri Oct 13 13:35:45 2023
@@ -0,0 +1,115 @@
+/*
+ * 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.torque.dsfactory.converters;
+
+import java.time.Duration;
+
+import org.apache.commons.beanutils.converters.AbstractConverter;
+
+/**
+ * {@link org.apache.commons.beanutils.Converter} implementation that handles conversion
+ * to and from <b>java.time.Duration</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
+ *
+ * @version $Id$
+ * @since 1.3
+ */
+public final class DurationConverter extends AbstractConverter {
+
+ boolean useTextFormat = false;
+ /**
+ * Construct a <b>java.time.Duration</b> <i>Converter</i> that throws
+ * a <code>ConversionException</code> if an error occurs.
+ */
+ public DurationConverter() {
+ super();
+ }
+ /**
+ * Construct a <b>java.time.DurationL</b> <i>Converter</i> that returns
+ * a default value if an error occurs.
+ *
+ * @param defaultValue The default value to be returned
+ * if the value to be converted is missing or an error
+ * occurs converting the value.
+ * @param useTextFormat if values are in ISO8901 formats.
+ */
+ public DurationConverter(final Object defaultValue, boolean useTextFormat) {
+ super(defaultValue);
+ this.useTextFormat = useTextFormat;
+ }
+
+ /**
+ * Construct a <b>java.time.DurationL</b> <i>Converter</i> that returns
+ * a default value if an error occurs.
+ *
+ * @param defaultValue The default value to be returned
+ * if the value to be converted is missing or an error
+ * occurs converting the value.
+ */
+ public DurationConverter(final Object defaultValue) {
+ super(defaultValue);
+ }
+ /**
+ * Construct a <b>java.time.DurationL</b> <i>Converter</i> that throws
+ * a <code>ConversionException</code> if an error occurs.
+ *
+ * @param useTextFormat if values are in ISO8901 formats.
+ *
+ * @see {@link Duration#parse(CharSequence)}
+ */
+ public DurationConverter(boolean useTextFormat) {
+ super();
+ this.useTextFormat = useTextFormat;
+ }
+
+ /**
+ * Return the default type this <code>Converter</code> handles.
+ *
+ * @return The default type this <code>Converter</code> handles.
+ * @since 1.8.0
+ */
+ @Override
+ protected Class<?> getDefaultType() {
+ return Duration.class;
+ }
+
+ /**
+ * <p>Convert a String to a java.time.Duration.</p>
+ *
+ * @param <T> Target type of the conversion.
+ * @param type Data type to which this value should be converted.
+ * @param value The input value to be converted.
+ * @return The converted value.
+ * @throws Throwable if an error occurs converting to the specified type
+ */
+ @Override
+ protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
+ if (Duration.class.equals(type)) {
+ if (useTextFormat) {
+ return type.cast( Duration.parse( value.toString() ));
+ } else {
+ Integer amount = Integer.valueOf( value.toString() );
+ return type.cast(Duration.ofMillis(amount));
+ }
+ }
+
+ throw conversionException(type, value);
+ }
+
+}
Modified: db/torque/trunk/torque-runtime/src/test/resources/Torque.properties
URL: http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/test/resources/Torque.properties?rev=1912940&r1=1912939&r2=1912940&view=diff
==============================================================================
--- db/torque/trunk/torque-runtime/src/test/resources/Torque.properties (original)
+++ db/torque/trunk/torque-runtime/src/test/resources/Torque.properties Fri Oct 13 13:35:45 2023
@@ -15,6 +15,22 @@
# specific language governing permissions and limitations
# under the License.
+# dbcp2 pool
+torque.defaults.pool.defaultMaxIdle = 8
+torque.defaults.pool.maxTotal = 10
+
+# use ISO-8601 duration format like PnDTnHnMn.nS is true by default
+torque.defaults.pool.defaultDurationBetweenEvictionRuns=PT12S
+torque.defaults.pool.defaultMaxWait=PT15S
+torque.defaults.pool.defaultSoftMinEvictableIdle=PT10S
+
+# deprecated, but internally still introspectable by beanutils in dbcp2 2.10.0
+#torque.defaults.pool.defaultTimeBetweenEvictionRunsMillis=3000
+
+# if useDurationTextFormat is set to false, duration cold be set still in milliseconds
+#torque.defaults.pool.global.useDurationTextFormat=false
+#torque.defaults.pool.defaultDurationBetweenEvictionRuns=12000
+#torque.defaults.pool.defaultMaxWait=15000
torque.database.default = postgresql
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org