Skip to content

ConcurrentModificationException in AggregatePath$ColumnInfos.toColumnList under concurrent entity updates #2414

Open

Description

@paasshme

Affects Version(s)


Description

Hello, I came accross the following issue:
when multiple concurrent worker threads perform entity updates on an application with a cold metadata cache (typically on application startup), Spring Data JDBC throws a ConcurrentModificationException:

java.util.ConcurrentModificationException: null
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1230)
	at org.springframework.data.relational.core.mapping.AggregatePath$ColumnInfos.toColumnList(AggregatePath.java:600)
	at org.springframework.data.jdbc.core.convert.SqlGenerator.getSubselectCondition(SqlGenerator.java:997)
	at org.springframework.data.jdbc.core.convert.SqlGenerator.createDeleteByPathAndCriteria(SqlGenerator.java:1163)
	at org.springframework.data.jdbc.core.convert.SqlGenerator.createDeleteByPath(SqlGenerator.java:561)
	at org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy.delete(DefaultDataAccessStrategy.java:235)
	at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.executeDelete(JdbcAggregateChangeExecutionContext.java:162)
	at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:96)
	at org.springframework.data.jdbc.core.AggregateChangeExecutor.lambda$executeSave$0(AggregateChangeExecutor.java:61)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
	at org.springframework.data.jdbc.core.AggregateChangeExecutor.executeSave(AggregateChangeExecutor.java:61)
	at org.springframework.data.jdbc.core.JdbcAggregateTemplate.store(JdbcAggregateTemplate.java:446)
	at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:189)
	at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:78)

Analysis

  1. RelationalMappingContext caches AggregatePath instances across all requests in a thread-safe map:
    // RelationalMappingContext.java:49
    private final Map<AggregatePathCacheKey, AggregatePath> aggregatePathCache = new ConcurrentHashMap<>();
  2. Each AggregatePath contains a TableInfo, which in turn holds ColumnInfos (for idColumnInfos and backReferenceColumnInfos).
  3. In AggregatePath.ColumnInfos, the internal columnCache is instantiated using a non-thread-safe java.util.HashMap:
    // AggregatePath.java:535
    this.columnCache = new HashMap<>(columnInfos.size(), 1f);
  4. In toColumnList(Table table):
    public List<Column> toColumnList(Table table) {
        return columnCache.computeIfAbsent(table, t -> toColumnList((__, ci) -> t.column(ci.name)));
    }
  5. When an aggregate with multi-level nested collections (!isFirstNonRoot(path)) is saved, SqlGenerator.createDeleteByPathAndCriteria calls SqlGenerator.getSubselectCondition(), which invokes parentPathTableInfo.idColumnInfos().toColumnList(subSelectTable).
  6. When multiple threads call this concurrently while columnCache is empty, multiple threads enter HashMap.computeIfAbsent(). The first thread increments modCount during node insertion, causing the other thread to detect modCount != mc and throw java.util.ConcurrentModificationException.

Minimal Reproducible Example (No Database Required)

The bug can be reproduced in in a standalone unit test that I put in ColumnInfosUnitTests.java:

@Test
void toColumnListIsThreadSafeUnderConcurrentAccess() throws Exception {

	int threads = 16;
	ExecutorService pool = Executors.newFixedThreadPool(threads);

	try {
		for (int iteration = 0; iteration < 50; iteration++) {
			AggregatePath.ColumnInfos columnInfos = basePath(DummyEntity.class).getTableInfo().idColumnInfos();
			CyclicBarrier barrier = new CyclicBarrier(threads);
			List<Future<List<Column>>> futures = new ArrayList<>(threads);

			for (int i = 0; i < threads; i++) {
				futures.add(pool.submit(() -> {
					barrier.await();
					return columnInfos.toColumnList(TABLE);
				}));
			}

			for (Future<List<Column>> future : futures) {
				assertThat(future.get(5, TimeUnit.SECONDS)).containsExactly(TABLE.column(ID));
			}
		}
	} finally {
		pool.shutdown();
		assertThat(pool.awaitTermination(5, TimeUnit.SECONDS)).isTrue();
	}
}

Running this test against current main / 4.1.1 fails immediately:

[ERROR] ColumnInfosUnitTests.toColumnListIsThreadSafeUnderConcurrentAccess:109 » Execution java.util.ConcurrentModificationException
    at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1230)
    at org.springframework.data.relational.core.mapping.AggregatePath$ColumnInfos.toColumnList(AggregatePath.java:602)

Suggested Fix

Change columnCache to ConcurrentHashMap in AggregatePath.ColumnInfos:

--- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/AggregatePath.java
+++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/AggregatePath.java
@@ -535,3 +535,3 @@
 			this.columnInfos.putAll(columnInfos);
-			this.columnCache = new HashMap<>(columnInfos.size(), 1f);
+			this.columnCache = new ConcurrentHashMap<>(columnInfos.size(), 1f);
 		}

I have a patch and unit test ready in #2415, feel free to reach to me if any details/modifications are required :)

Activity

  1. added a commit that references this issue on Oct 15, 2026
    6ece058
  2. self-assigned this
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions