Skip to content

Commit 75c4783

Browse files
mp911deschauder
authored andcommitted
Performance refinements.
1 parent 10b092c commit 75c4783

File tree

7 files changed

+43
-53
lines changed

7 files changed

+43
-53
lines changed

spring-data-jdbc/src/jmh/java/org/springframework/data/jdbc/CompositeIdBenchmarks.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.sql.Connection;
21+
import java.sql.SQLException;
2022
import java.util.List;
2123
import java.util.Map;
2224
import java.util.concurrent.atomic.AtomicLong;
@@ -39,8 +41,10 @@
3941
import org.springframework.data.relational.core.mapping.Embedded;
4042
import org.springframework.data.relational.core.mapping.Table;
4143
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
44+
import org.springframework.jdbc.datasource.ConnectionHolder;
4245
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4346
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
47+
import org.springframework.transaction.support.TransactionSynchronizationManager;
4448

4549
/**
4650
* Benchmarks for Composite Ids in Spring Data JDBC.
@@ -82,7 +86,7 @@ public static class BenchmarkState {
8286
SimpleEntity alpha;
8387

8488
@Setup
85-
public void setup() {
89+
public void setup() throws SQLException {
8690

8791
context = new AnnotationConfigApplicationContext();
8892
context.register(BenchmarkConfiguration.class);
@@ -91,6 +95,12 @@ public void setup() {
9195

9296
template = context.getBean(JdbcAggregateTemplate.class);
9397
named = context.getBean(NamedParameterJdbcTemplate.class);
98+
DataSource dataSource = context.getBean(DataSource.class);
99+
100+
Connection connection = dataSource.getConnection();
101+
ConnectionHolder holder = new ConnectionHolder(connection, true);
102+
holder.setSynchronizedWithTransaction(true);
103+
TransactionSynchronizationManager.bindResource(dataSource, holder);
94104

95105
alpha = template.insert(new SimpleEntity(new WrappedPk(l.incrementAndGet()), "alpha"));
96106
}
@@ -160,7 +170,7 @@ public void deleteSingleSimpleEntityWithEmbeddedPk(BenchmarkState state) {
160170
List<SimpleEntityWithEmbeddedPk> entities = (List<SimpleEntityWithEmbeddedPk>) state.template
161171
.insertAll(List.of(new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "x"), "alpha")));
162172

163-
state.template.delete(entities.get(1));
173+
state.template.delete(entities.get(0));
164174
}
165175

166176
@Benchmark

spring-data-jdbc/src/jmh/java/org/springframework/data/jdbc/Profiler.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BindParameterNameSanitizer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ abstract class BindParameterNameSanitizer {
2929
private static final Pattern parameterPattern = Pattern.compile("\\W");
3030

3131
static String sanitize(String rawName) {
32-
return parameterPattern.matcher(rawName).replaceAll("");
32+
33+
for (int i = 0; i < rawName.length(); i++) {
34+
char c = rawName.charAt(i);
35+
if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && c != '_') {
36+
return parameterPattern.matcher(rawName).replaceAll("");
37+
}
38+
}
39+
40+
return rawName;
3341
}
3442
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcColumnTypes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,28 @@
2424
import java.util.Map;
2525

2626
import org.springframework.util.ClassUtils;
27+
import org.springframework.util.ConcurrentLruCache;
2728

2829
/**
2930
* Utility that determines the necessary type conversions between Java types used in the domain model and types
3031
* compatible with JDBC drivers.
3132
*
3233
* @author Jens Schauder
34+
* @author Mark Paluch
3335
* @since 2.0
3436
*/
3537
public enum JdbcColumnTypes {
3638

3739
INSTANCE {
3840

41+
private final ConcurrentLruCache<Class<?>, Class<?>> cache = new ConcurrentLruCache<>(64, this::doResolve);
42+
3943
@SuppressWarnings({ "unchecked", "rawtypes" })
4044
public Class<?> resolvePrimitiveType(Class<?> type) {
45+
return cache.get(type);
46+
}
4147

48+
private Class<?> doResolve(Class<?> type) {
4249
return javaToDbType.entrySet().stream() //
4350
.filter(e -> e.getKey().isAssignableFrom(type)) //
4451
.map(e -> (Class<?>) e.getValue()) //

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlIdentifierParameterSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
*/
3636
class SqlIdentifierParameterSource extends AbstractSqlParameterSource {
3737

38-
private final Set<SqlIdentifier> identifiers = new HashSet<>();
39-
private final Map<String, Object> namesToValues = new HashMap<>();
38+
private final Set<SqlIdentifier> identifiers = new HashSet<>(16, 1f);
39+
private final Map<String, Object> namesToValues = new HashMap<>(16, 1f);
4040

4141
@Override
4242
public boolean hasValue(String paramName) {
@@ -73,7 +73,7 @@ void addAll(SqlIdentifierParameterSource others) {
7373

7474
for (SqlIdentifier identifier : others.getIdentifiers()) {
7575

76-
String name = BindParameterNameSanitizer.sanitize( identifier.getReference());
76+
String name = BindParameterNameSanitizer.sanitize(identifier.getReference());
7777
addValue(identifier, others.getValue(name), others.getSqlType(name));
7878
}
7979
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlParametersFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,11 @@ private BiFunction<Object, AggregatePath, Object> getIdMapper(@Nullable Relation
208208
return (id, aggregatePath) -> id;
209209
}
210210

211-
return (id, aggregatePath) -> complexId.getPropertyPathAccessor(id)
212-
.getProperty(aggregatePath.getRequiredPersistentPropertyPath());
211+
return (id, aggregatePath) -> {
212+
213+
PersistentPropertyAccessor<Object> accessor = complexId.getPropertyAccessor(id);
214+
return accessor.getProperty(aggregatePath.getRequiredLeafProperty());
215+
};
213216
}
214217

215218
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource,

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/support/JdbcUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.Map;
2828

2929
import org.springframework.jdbc.support.JdbcUtils;
30-
import org.springframework.util.Assert;
30+
import org.springframework.util.ConcurrentLruCache;
3131

3232
/**
3333
* Contains methods dealing with the quirks of JDBC, independent of any Entity, Aggregate or Repository abstraction.
@@ -58,7 +58,10 @@ public String toString() {
5858
return getName();
5959
}
6060
};
61+
6162
private static final Map<Class<?>, SQLType> sqlTypeMappings = new HashMap<>();
63+
private static ConcurrentLruCache<Class<?>, SQLType> sqlTypeCache = new ConcurrentLruCache<>(64,
64+
JdbcUtil::doGetSqlType);
6265

6366
static {
6467

@@ -97,9 +100,10 @@ private JdbcUtil() {
97100
* @return a matching {@link SQLType} or {@link #TYPE_UNKNOWN}.
98101
*/
99102
public static SQLType targetSqlTypeFor(Class<?> type) {
103+
return sqlTypeCache.get(type);
104+
}
100105

101-
Assert.notNull(type, "Type must not be null");
102-
106+
private static SQLType doGetSqlType(Class<?> type) {
103107
return sqlTypeMappings.keySet().stream() //
104108
.filter(k -> k.isAssignableFrom(type)) //
105109
.findFirst() //

0 commit comments

Comments
 (0)