0

I'm getting the following exception when I run the app

Could someone advise what might be the issues?

Exception in thread "task-2" java.lang.IllegalStateException: EntityManagerFactory is closed
at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:509)
at org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:503)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)


***************************
APPLICATION FAILED TO START
***************************
Description:


Parameter 0 of constructor in ru.javamentor.ecommerce.service.impl.ReadWriteServiceImpl required a single bean, but 4 were found:
- productCategoryDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\ProductCategoryDaoImpl.class]
- productDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\ProductDaoImpl.class]
- roleDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\RoleDaoImpl.class]
- userDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\UserDaoImpl.class]

ReadWriteServiceImpl class:

@Service

public class ReadWriteServiceImpl<T, PK> implements ReadWriteService<T, PK> { private final ReadWriteDao<T, PK> readWriteDao;

@Autowired
public ReadWriteServiceImpl(ReadWriteDao<T, PK> readWriteDao) {
    this.readWriteDao = readWriteDao;
}

@Override
@Transactional
public void persist(T t) {
    readWriteDao.persist(t);
}

@Override
@Transactional
public void update(T t) {
    readWriteDao.update(t);
}

@Override
@Transactional
public void delete(T t) {
    readWriteDao.delete(t);
}

@Override
public boolean existsById(PK id) {
    return readWriteDao.existsById(id);
}

@Override
public T getByKey(PK id) {
    return readWriteDao.getByKey(id);
}

@Override
public List<T> getAll() {
    return readWriteDao.getAll();
}

}

3
  • 1
    Can you please show ReadWriteServiceImpl Commented Jun 22, 2020 at 11:09
  • Yeah, sure. Sorry Commented Jun 22, 2020 at 11:22
  • 1
    You cannot inject the generic ReadWriteDao you have either use a concrete type or qualifiy it by name Commented Jun 22, 2020 at 11:25

2 Answers 2

4

In this case the reason for exception is clear:

There are 4 candidates for injection to ReadWriteServiceImpl and spring doesn't know which of the beans to inject:

public ReadWriteServiceImpl(ReadWriteDao<T, PK> readWriteDao) {
    this.readWriteDao = readWriteDao;
}

There are 4 candidates - implementations of ReadWriteDao and spring lists them all in the exception...

There are three ways you can go with in this case:

  1. Use Concrete type (or create an interface for that concrete implementation and inject by that interface so that spring won't be confused)

  2. Use @Qualifier annotation as a hint to spring framework what to inject

  3. Use @Primary annotation if you think that one repository should be used "by default" to resolve confusions like this.

Sign up to request clarification or add additional context in comments.

Comments

-1

It showed me the error because I was writing wrong on the path

@GetMapping(myEndpoint+ "/${id}")

I should have written:

@GetMapping(myEndpoint+ "/{id}")

1 Comment

This is not relevant to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.