[#6803] Cache RecordMapper instances in DefaultRecordMapperProvider

This commit is contained in:
lukaseder 2017-11-08 15:42:45 +01:00
parent aea236dddd
commit 1d6450af19
5 changed files with 59 additions and 7 deletions

View File

@ -75,6 +75,8 @@ public class Settings
protected Boolean updatablePrimaryKeys = false;
@XmlElement(defaultValue = "true")
protected Boolean reflectionCaching = true;
@XmlElement(defaultValue = "true")
protected Boolean cacheRecordMappers = true;
@XmlElement(defaultValue = "THROW_ALL")
@XmlSchemaType(name = "string")
protected ThrowExceptions throwExceptions = ThrowExceptions.THROW_ALL;
@ -570,6 +572,30 @@ public class Settings
this.reflectionCaching = value;
}
/**
* Whether record mappers should be cached in the configuration.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isCacheRecordMappers() {
return cacheRecordMappers;
}
/**
* Sets the value of the cacheRecordMappers property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setCacheRecordMappers(Boolean value) {
this.cacheRecordMappers = value;
}
/**
* A strategy defining how exceptions from the database / JDBC driver should be propagated
*
@ -1004,6 +1030,11 @@ public class Settings
return this;
}
public Settings withCacheRecordMappers(Boolean value) {
setCacheRecordMappers(value);
return this;
}
public Settings withThrowExceptions(ThrowExceptions value) {
setThrowExceptions(value);
return this;

View File

@ -34,6 +34,9 @@
*/
package org.jooq.impl;
import static java.lang.Boolean.TRUE;
import static org.jooq.impl.Tools.DATA_CACHE_RECORD_MAPPERS;
import java.io.Serializable;
import org.jooq.Configuration;
@ -41,6 +44,8 @@ import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.RecordMapperProvider;
import org.jooq.RecordType;
import org.jooq.impl.Tools.Cache;
import org.jooq.impl.Tools.Cache.CachedOperation;
/**
* A default {@link RecordMapperProvider} implementation, providing a
@ -71,7 +76,15 @@ public class DefaultRecordMapperProvider implements RecordMapperProvider, Serial
}
@Override
public final <R extends Record, E> RecordMapper<R, E> provide(RecordType<R> rowType, Class<? extends E> type) {
return new DefaultRecordMapper<R, E>(rowType, type, configuration);
public final <R extends Record, E> RecordMapper<R, E> provide(final RecordType<R> rowType, final Class<? extends E> type) {
if (TRUE.equals(configuration.settings().isCacheRecordMappers()))
return Cache.run(configuration, new CachedOperation<RecordMapper<R, E>>() {
@Override
public RecordMapper<R, E> call() {
return new DefaultRecordMapper<R, E>(rowType, type, configuration);
}
}, DATA_CACHE_RECORD_MAPPERS, Cache.key(rowType, type));
else
return new DefaultRecordMapper<R, E>(rowType, type, configuration);
}
}

View File

@ -406,6 +406,9 @@ final class Fields<R extends Record> extends AbstractQueryPart implements Record
@Override
public boolean equals(Object that) {
if (this == that)
return true;
if (that instanceof Fields)
return Arrays.equals(fields, ((Fields<?>) that).fields);

View File

@ -471,6 +471,7 @@ final class Tools {
static final String DATA_REFLECTION_CACHE_GET_MATCHING_MEMBERS = new String("org.jooq.configuration.reflection-cache.get-matching-members");
static final String DATA_REFLECTION_CACHE_GET_MATCHING_SETTERS = new String("org.jooq.configuration.reflection-cache.get-matching-setters");
static final String DATA_REFLECTION_CACHE_HAS_COLUMN_ANNOTATIONS = new String("org.jooq.configuration.reflection-cache.has-column-annotations");
static final String DATA_CACHE_RECORD_MAPPERS = new String("org.jooq.configuration.cache.record-mappers");
// ------------------------------------------------------------------------
// Other constants
@ -2685,7 +2686,7 @@ final class Tools {
/**
* Create a single-value or multi-value key for caching.
*/
static final Object key(Serializable key1, Serializable key2) {
static final Object key(Object key1, Object key2) {
return new Key2(key1, key2);
}
@ -2697,11 +2698,11 @@ final class Tools {
/**
* Generated UID.
*/
private static final long serialVersionUID = 5822370287443922993L;
private final Serializable key1;
private final Serializable key2;
private static final long serialVersionUID = 5822370287443922993L;
private final Object key1;
private final Object key2;
Key2(Serializable key1, Serializable key2) {
Key2(Object key1, Object key2) {
this.key1 = key1;
this.key2 = key2;
}

View File

@ -115,6 +115,10 @@ UpdatableRecord.store() and UpdatableRecord.update().]]></jxb:javadoc></jxb:prop
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether reflection information should be cached in the configuration.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="cacheRecordMappers" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Whether record mappers should be cached in the configuration.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="throwExceptions" type="jooq-runtime:ThrowExceptions" minOccurs="0" maxOccurs="1" default="THROW_ALL">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[A strategy defining how exceptions from the database / JDBC driver should be propagated]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>