How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

Johannes Dorn

A deadlock occurs in my application while one thread performs a query on a mongo db, causing an application event to be fired, while another thread attempts to create a bean with an AutowireCapableBeanFactory.

This happens in the constructor of a @Service and prevents the startup of the entire application as the service is created on startup.

Pseudocode:

@Service
public class MyService {

    @Autowired
    public MyService(AutowireCapableBeanFactory factory, TaskScheduler scheduler) {
        MyFirstCommand command1 = factory.getBean(MyFirstCommand.class);
        scheduler.schedule(command1, new Date());

        MySecondCommand command2 = factory.getBean(MySecondCommand.class);
        scheduler.schedule(command2, new Date());
    }
}

public class MyFirstCommand extends Runnable {

    @Autowired MongoOperations mongo;

    @Override
    public void run() {
       mongo.findById("some-id", MyData.class); 
    }
}

Note that i have some AbstractMongoEventListeners registered as @Components.

The deadlock occurs while one thread in MyFirstCommand.run() performs mongo.findById(). The other thread is in the constructor of MyService performing factory.getBean(MySecondCommand.class):

Found one Java-level deadlock:
=============================
"pool-1-thread-1":
  waiting to lock monitor 0x00007fd05d5bf5d8 (object 0x00000000816d0970, a java.util.concurrent.ConcurrentHashMap),
  which is held by "main"
"main":
  waiting for ownable synchronizer 0x0000000081ed8468, (a java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync),
  which is held by "pool-1-thread-1"


Java stack information for the threads listed above:
===================================================
"pool-1-thread-1":
at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:179)
- waiting to lock <0x00000000816d0970> (a java.util.concurrent.ConcurrentHashMap)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:326)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:179)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:139)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:66)
at org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:1976)
at org.springframework.data.mongodb.core.MongoTemplate.findById(MongoTemplate.java:607)


"main":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for  <0x0000000081ed8468> (a java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireShared(AbstractQueuedSynchronizer.java:967)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireShared(AbstractQueuedSynchronizer.java:1283)
at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lock(ReentrantReadWriteLock.java:727)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:160)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:139)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:66)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getEntityInformation(MongoRepositoryFactory.java:118)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:92)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 

How come Spring isn't thread safe here? What can i do to avoid this deadlock?

Johannes Dorn

This deadlock occurs due to a bug in Spring. I have created an issue in their JIRA: DATACMNS-901.

As a workaround i am now creating all runnable beans before scheduling them:

@Autowired
public MyService(AutowireCapableBeanFactory factory, TaskScheduler scheduler) {
    MyFirstCommand command1 = factory.getBean(MyFirstCommand.class);
    MySecondCommand command2 = factory.getBean(MySecondCommand.class);

    scheduler.schedule(command2, new Date());
    scheduler.schedule(command1, new Date());
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I run code locally with dependencies and exclude them when creating a Jar with Maven

分類Dev

How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

分類Dev

How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

分類Dev

How can I set Jenkins build description while triggering build via curl?

分類Dev

How can I avoid "no input files" error from sed, when run from xargs?

分類Dev

How can I avoid "environment hell" in postman?

分類Dev

inotifywait triggering event twice while converting docx to PDF

分類Dev

How can I avoid losing precision with ftFmtBcd?

分類Dev

How can I avoid a circular reference situation

分類Dev

How can I disable the Vim scroll-while-typing when I do a search?

分類Dev

How can I kill a child process without the child process 'exit' event triggering?

分類Dev

Triggering powershell scripts when creating/updating/deleting Git branches on TFS

分類Dev

How can I avoid my program to keep running when I close my main frame?

分類Dev

Error creating bean named `conversionServicePostProcessor` when using spring-boot-admin-server

分類Dev

How can I assure consistency when using an event-carried state transfer approach in Kafka

分類Dev

How can I avoid Firefox's "you have chosen to open" dialog when I try to download a file?

分類Dev

How can i get access to the same instance of the class without creating one more bean id

分類Dev

How can I delete a row from a Table in SAPUI5 Application when I used Model as XMLModel?

分類Dev

Django conditional queries: How can I avoid this?

分類Dev

How can I Write an Event that Happens when Mouse Scrolls in TextBox?

分類Dev

How can I avoid duplicate templates in Meteor?

分類Dev

How can I avoid a black background when fading in an overlay with ffmpeg?

分類Dev

How can I send application health data to nagios from a spring application?

分類Dev

How i can avoid the distinct() of watchdog?

分類Dev

Spring creating bean but not injecting it

分類Dev

Error creating bean in Spring

分類Dev

How can I catch an EHOSTDOWN socket error when creating an http request with iojs?

分類Dev

How can I avoid problems with CPU power?

分類Dev

When installing the MongoDB library in PHP, how can I avoid this error about a missing extension?

Related 関連記事

  1. 1

    How can I run code locally with dependencies and exclude them when creating a Jar with Maven

  2. 2

    How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

  3. 3

    How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

  4. 4

    How can I set Jenkins build description while triggering build via curl?

  5. 5

    How can I avoid "no input files" error from sed, when run from xargs?

  6. 6

    How can I avoid "environment hell" in postman?

  7. 7

    inotifywait triggering event twice while converting docx to PDF

  8. 8

    How can I avoid losing precision with ftFmtBcd?

  9. 9

    How can I avoid a circular reference situation

  10. 10

    How can I disable the Vim scroll-while-typing when I do a search?

  11. 11

    How can I kill a child process without the child process 'exit' event triggering?

  12. 12

    Triggering powershell scripts when creating/updating/deleting Git branches on TFS

  13. 13

    How can I avoid my program to keep running when I close my main frame?

  14. 14

    Error creating bean named `conversionServicePostProcessor` when using spring-boot-admin-server

  15. 15

    How can I assure consistency when using an event-carried state transfer approach in Kafka

  16. 16

    How can I avoid Firefox's "you have chosen to open" dialog when I try to download a file?

  17. 17

    How can i get access to the same instance of the class without creating one more bean id

  18. 18

    How can I delete a row from a Table in SAPUI5 Application when I used Model as XMLModel?

  19. 19

    Django conditional queries: How can I avoid this?

  20. 20

    How can I Write an Event that Happens when Mouse Scrolls in TextBox?

  21. 21

    How can I avoid duplicate templates in Meteor?

  22. 22

    How can I avoid a black background when fading in an overlay with ffmpeg?

  23. 23

    How can I send application health data to nagios from a spring application?

  24. 24

    How i can avoid the distinct() of watchdog?

  25. 25

    Spring creating bean but not injecting it

  26. 26

    Error creating bean in Spring

  27. 27

    How can I catch an EHOSTDOWN socket error when creating an http request with iojs?

  28. 28

    How can I avoid problems with CPU power?

  29. 29

    When installing the MongoDB library in PHP, how can I avoid this error about a missing extension?

ホットタグ

アーカイブ