ConcurrentModificationException while creating application context with bean factory

I have a bean factory exposed as a bean that allows me to dynamically create beans:
SDataSourceFactory.java
@Service
public class DataSourceFactory implements ApplicationContextAware
{
private ApplicationContext appContext;

public DataSource getDataSource (String host, String port, String database, String username, String password)
{
GenericApplicationContext ctx = (GenericApplicationContext)appContext;
String beanId = "dataSource-" + database;
BeanDefinitionBuilder ds = BeanDefinitionBuilder.rootBeanDefinition(AtomikosNonXADataSourceBean.class);
ds.addPropertyValue("url", "jdbc:postgresql://" + host + ":" + port + "/" + database);
ds.addPropertyValue("user", username);
ds.addPropertyValue("password", password);
ds.addPropertyValue("poolSize", 1);
ds.addPropertyValue("borrowConnectionTimeout", 60);
ds.addPropertyValue("uniqueResourceName", beanId);
ds.addPropertyValue("driverClassName", "org.postgresql.Driver");

ctx.registerBeanDefinition(beanId, ds.getBeanDefinition());
return (AtomikosNonXADataSourceBean)ctx.getBean(beanId);

//return new DriverManagerDataSource("jdbc:postgresql://" + host + ":" + port + "/" + database, username, password);
}

@Override
public void setApplicationContext(ApplicationContext appContext) throws BeansException
{
this.appContext = appContext;
}
}

Then I create a bean using this factory in a constructor of another bean:
SomeService.java
@Service
public class SomeManager
{
@Inject
private DataSourceFactory dataSourceFactory;

@Inject
public SomeManager (DataSourceFactory dataSourceFactory) throws KolmuException
{
this.dataSourceFactory = dataSourceFactory;

// get data source bean
DataSource ds = this.dataSourceFactory.getDataSource("test", "test", "test", "test", "test");
}
}

As a result, I get the following exception:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:559)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
... 24 more
graham on 16-05-2013 16:31
The reason for the exception was that I was creating the DataSource bean in the constructor of another bean, SomeService, which in turn was created automatically. Moving the call to DataSourceFactory.getDataSource outside of the constructor to some other method (which is not called at bean creation) solved the issue.
graham on 16-05-2013 16:33

Post an answer

Do you have a solution to this problem, or want to comment on it? Other users will appreciate your help!
Your answer or comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems