Jan Jurcik created DBUTILS-145:
----------------------------------
Summary: org.apache.commons.dbutils.BeanProcessor doesn't work in
multi-thread environment
Key: DBUTILS-145
URL: https://issues.apache.org/jira/browse/DBUTILS-145
Project: Commons DbUtils
Issue Type: Bug
Affects Versions: 1.7
Environment: Checked on Windows 10 and CentOS 7.5 with Java 8 and Java
11.
Reporter: Jan Jurcik
Attachments: test-commons-dbutils-multithread.zip
Using of BeanProcessor class with multiple threads fails, even every thread has
its own instance. Th reason is this part of code:
/**
* ServiceLoader to find <code>ColumnHandler</code> implementations on the
classpath. The iterator for this is
* lazy and each time <code>iterator()</code> is called.
*/
// FIXME: I think this instantiates new handlers on each iterator() call. This
might be worth caching upfront.
private static final ServiceLoader<ColumnHandler> columnHandlers =
ServiceLoader.load(ColumnHandler.class);
/**
* ServiceLoader to find <code>PropertyHandler</code> implementations on the
classpath. The iterator for this is
* lazy and each time <code>iterator()</code> is called.
*/
// FIXME: I think this instantiates new handlers on each iterator() call. This
might be worth caching upfront.
private static final ServiceLoader<PropertyHandler> propertyHandlers =
ServiceLoader.load(PropertyHandler.class);
and then using as Iterable:
for (ColumnHandler handler : columnHandlers) {
if (handler.match(propType)) {
retval = handler.apply(rs, index);
break;
}
}
As per ServiceLoader javadoc, this class is not thread safe:
[https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html] and
using the same instance in multiple threads causes either
NoSuchElementException or ClassCastException.
Attached is very simple maven java 8 project (the same happens also in other
versions of Java), which executes simple query and tries to convert it to Java
Bean. Run it in multi processor environment to simulate the issue. part of the
project is also fixed version of the class *FixedBeanProcessor* which should
work on Java 6+.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)