You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Rakesh Kumar (Jira)" <ji...@apache.org> on 2020/12/27 11:57:00 UTC

[jira] [Commented] (DBUTILS-145) org.apache.commons.dbutils.BeanProcessor doesn't work in multi-thread environment

    [ https://issues.apache.org/jira/browse/DBUTILS-145?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17255210#comment-17255210 ] 

Rakesh Kumar commented on DBUTILS-145:
--------------------------------------

This seems to be fixed in current code in GitHub, will it be released anytime soon with v1.8 or 2.0? Please let me know.

> 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
>            Priority: Blocker
>         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)