Skip to content

Prevent race condition in TypeHandlerRegistry #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -393,9 +393,9 @@ private void register(Type javaType, JdbcType jdbcType, TypeHandler<?> handler)
Map<JdbcType, TypeHandler<?>> map = typeHandlerMap.get(javaType);
if (map == null || map == NULL_TYPE_HANDLER_MAP) {
map = new HashMap<>();
typeHandlerMap.put(javaType, map);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow...did this actually happen on production?

Regarding the change, how about updating typeHandlerMap at the end?
i.e.

      Map<JdbcType, TypeHandler<?>> map = typeHandlerMap.get(javaType);
      if (map == null || map == NULL_TYPE_HANDLER_MAP) {
        map = new HashMap<>();
      }
      map.put(jdbcType, handler);
      typeHandlerMap.put(javaType, map);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this happening a couple times in a week on startup. But our apps work like as "lamba" (many cold startups in a loop).

I agree, this will save a couple of code lines 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. That explains it.
The fix is merged and should be available in the latest 3.5.4-SNAPSHOT.
Please let me know if you noticed anything.

Thank you for the PR, @PVlyubinskiy !

map.put(jdbcType, handler);
typeHandlerMap.put(javaType, map);
}
allTypeHandlersMap.put(handler.getClass(), handler);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,11 @@
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.ibatis.domain.misc.RichType;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -215,4 +220,30 @@ class Address {
typeHandlerRegistry.register(Address.class, StringTypeHandler.class);
assertTrue(typeHandlerRegistry.hasTypeHandler(Address.class));
}

enum TestEnum {
ONE,
TWO
}

@Test
void shouldAutoRegisterEnutmTypeInMultiThreadEnvironment() throws Exception {
// gh-1820
ExecutorService executorService = Executors.newCachedThreadPool();
try {
for (int iteration = 0; iteration < 2000; iteration++) {
TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
List<Future<Boolean>> taskResults = IntStream.range(0, 2)
.mapToObj(taskIndex -> executorService.submit(() -> {
return typeHandlerRegistry.hasTypeHandler(TestEnum.class, JdbcType.VARCHAR);
})).collect(Collectors.toList());
for (int i = 0; i < taskResults.size(); i++) {
Future<Boolean> future = taskResults.get(i);
assertTrue(future.get(), "false is returned at round " + iteration);
}
}
} finally {
executorService.shutdownNow();
}
}
}