<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=ractive" rel="nofollow">Jean-Pierre Bergamin</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8785?redirect=false" rel="nofollow">SPR-8785</a></strong> and commented</p>
<p dir="auto">We face a deadlock in virgo 3.0.0.RELEASE when starting our spring dm powered bundles during the creation of the application contexts. Two bundles get in a deadlock while their contexts get created simultaneously.</p>
<p dir="auto">Please see the <a href="http://www.eclipse.org/forums/index.php/t/248977/" rel="nofollow">discussion in the eclipse forum</a> for full stacktraces of the blocking threads.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.6</p>
<p dir="auto"><strong>Reference URL:</strong> <a href="http://www.eclipse.org/forums/index.php/t/248977/" rel="nofollow">http://www.eclipse.org/forums/index.php/t/248977/</a></p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398113127" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13117" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13117/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13117">#13117</a> Performance bottleneck and potential thread deadlock in DefaultSingletonBeanRegistry (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=harloshmark" rel="nofollow">Mark Davies</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8471?redirect=false" rel="nofollow">SPR-8471</a></strong> and commented</p>
<p dir="auto">A less significant version of this problem has already been raised under <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398092384" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10033" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10033/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10033">#10033</a> - a performance bottleneck affecting Wicket. However, the same issue causes a serious thread deadlock in our application, occasionally preventing application startup.</p>
<p dir="auto">The basic issue seems to be that DefaultSingletonBeanRegistry takes a global synchronization lock when creating a singleton bean. Here is the code in that class:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
     Assert.notNull(beanName, &quot;'beanName' must not be null&quot;);
     synchronized (this.singletonObjects) {
          Object singletonObject = this.singletonObjects.get(beanName);
          if (singletonObject == null) {
          ... stuff ...
                        singletonObject = singletonFactory.getObject();
          ... stuff ...
}"><pre class="notranslate"><code class="notranslate">public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
     Assert.notNull(beanName, "'beanName' must not be null");
     synchronized (this.singletonObjects) {
          Object singletonObject = this.singletonObjects.get(beanName);
          if (singletonObject == null) {
          ... stuff ...
                        singletonObject = singletonFactory.getObject();
          ... stuff ...
}
</code></pre></div>
<p dir="auto">The synchronized block (the same this.singletonObjects reference is used by all threads entering the method) means that Spring can only create singleton beans one at a time, regardless of their type (beanName). This clearly introduces a performance penalty if an application has a large number of singleton beans to construct, e.g. at startup.</p>
<p dir="auto">That is not the issue affecting our code, though. We see a deadlock, caused by the following two sets of behaviour:</p>
<ol dir="auto">
<li>
<p dir="auto">We have Spring-managed singleton beans which perform database access in their constructor (basically loading and caching configuration sets from the database). In order to do this they obtain database connections, which are pooled, with relatively small pool sizes. If a pooled connection is not available, the calling thread blocks and waits until one becomes free. This is usually not a problem since queries are small and rapid, so pool wait times are low, and the maximum pool size is sufficient to work the databasea at full capacity anyway.</p>
</li>
<li>
<p dir="auto">We also have non-Spring code doing database access. Such code obtains a database connection from the same pool, purely for the lifetime of running a query and processing a result set, so again very quick for almost cases in our system. But sometimes, whilst processing the result set, we need to use a Spring-managed bean, which may have singleton scope.</p>
</li>
</ol>
<p dir="auto">You now have a deadlock - thread number one is trying to get a Spring-managed singleton bean, which is waiting for a JDBC connection in its constructor; thread number two is running database code which has the JDBC connection and is waiting to create a Spring-managed singleton of a completely different type. Both thread own the resource needed by the other, so will wait forever. (OK, if the database pool size is two or higher you need two or more threads in the second state, but this has happened to us in customer environments, in production).</p>
<p dir="auto">Obviously our application code is almost certainly less than ideal in how we use Spring, but it seems to me that we ought to be able to use Spring-managed beans which do database access in the manner I've described, without encountering unpleasant deadlocks such as this. Note that the problem is actually much more general than this particular example of database connections: if your singleton beans require any kind of global monitor, which is also used outside of the context of Spring, you have a deadlock condition.</p>
<p dir="auto">Both the originally-reported performance problem, and this deadlock, could be solved by a simple improvement to  DefaultSingletonBeanRegistry. The getSingleton() method should not synchronize on a global monitor, but instead a monitor specific to the beanName you are instantiating. This is the correct level at which to lock - what you are trying to do is prevent a bean of a certain type being created more than once, but allowing two beans of different types to construct at the same time is perfectly reasonable. Of course you have to synchronize access to the underlying collections with a global lock, but the construction of the singleton bean does not need to be protected with the same global monitor. For instance I believe this kind of thing would do it:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
     Object bean;
     Object perBeanMonitor;
     synchronized (GLOBAL_MONITOR) {
          perBeanMonitor = getPerBeanMonitor(beanName);
     }
     synchronized (perBeanMonitor) {
          synchronized (GLOBAL_MONITOR) {
               bean = getBeanFromRegistry(beanName);
          }
          if (bean==null) {
               Object bean = doConstructBean(beanName, singletonFactory);
               synchronized (GLOBAL_MONITOR) {
                        addToRegistry(beanName, bean);
               }
          }
     }
     return bean;
}"><pre class="notranslate"><code class="notranslate">public Object getSingleton(String beanName, ObjectFactory singletonFactory) {
     Object bean;
     Object perBeanMonitor;
     synchronized (GLOBAL_MONITOR) {
          perBeanMonitor = getPerBeanMonitor(beanName);
     }
     synchronized (perBeanMonitor) {
          synchronized (GLOBAL_MONITOR) {
               bean = getBeanFromRegistry(beanName);
          }
          if (bean==null) {
               Object bean = doConstructBean(beanName, singletonFactory);
               synchronized (GLOBAL_MONITOR) {
                        addToRegistry(beanName, bean);
               }
          }
     }
     return bean;
}
</code></pre></div>
<p dir="auto">It would be extremely helpful if this approach could be implemented in this core part of the Spring framework. We rely on singleton construction to be performant and thread-safe; the fact that DefaultSingletonBeanRegistry behaves as it currently does is causing us serious production problems at client sites, and, whilst we can modify our application code to work around the issue, I do see this as a fundamental flaw in Spring - would be nice to get a quick fix please.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.6</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/19616/test-case.zip" rel="nofollow">test-case.zip</a> (<em>3.09 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398115131" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13428" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13428/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13428">#13428</a> Deadlock during application context creation (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398153704" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14470" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14470/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14470">#14470</a> A Java Thread deadlock has occured (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398117774" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13837" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13837/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13837">#13837</a> Deadlock on the beanDefinitionMap and singletonObjects (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398108538" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12374" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12374/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12374">#12374</a> Deadlock between DefaultListableBeanFactory and DefaultSingletonBeanRegistry, perhaps due to lazily-instantiated aspect</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398153593" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14452" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14452/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14452">#14452</a> Non-singleton beans performance issue</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398155048" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14654" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14654/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14654">#14654</a> Deadlock in DefaultListableBeanFactory/DefaultSingletonBeanRegistry</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398157745" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14985" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14985/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14985">#14985</a> AbstractApplicationEventMulticaster#getApplicationListeners() should have double check for 'this.retrieverCache.get(cacheKey)' within synchronized block</li>
</ul>
<p dir="auto"><strong>Referenced from:</strong> commits <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/dd68fecbf2509faa189234807f30bbf364a691f1/hovercard" href="https://github.com/spring-projects/spring-framework/commit/dd68fecbf2509faa189234807f30bbf364a691f1"><tt>dd68fec</tt></a>, <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/52124fa31b17fee7b6ffdfc92ec9fe14ad9a0561/hovercard" href="https://github.com/spring-projects/spring-framework/commit/52124fa31b17fee7b6ffdfc92ec9fe14ad9a0561"><tt>52124fa</tt></a></p>
<p dir="auto">19 votes, 26 watchers</p>