Results 1 to 5 of 5

Thread: entityManager problem

  1. #1
    Join Date
    May 2009
    Posts
    29

    Default entityManager problem

    Code:
    @Service
    @Transactional
    public class InitialIndexPerformer {
    
    	@PersistenceContext
    	transient EntityManager entityManager;
    
    	@PostConstruct
    	public void performInitialIndex() throws InterruptedException {
    //		entityManager=entityManager.getEntityManagerFactory().createEntityManager();
    		Session session = (Session) entityManager.unwrap(Session.class);
    		FullTextSession fullTextSession = Search.getFullTextSession(session);
    		fullTextSession.createIndexer().startAndWait();
    	}
    }
    If I uncomment the first line in a method, the code works. I found this simply by trial and error.
    In this version, it gives me a nasty
    Code:
    java.lang.IllegalStateException: No transactional EntityManager available
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
        at $Proxy40.unwrap(Unknown Source)
    Exception.

    Can anybody explain why? I have @Transaction specified and all... I simply do not know what I am doing wrong and why the trick with
    Code:
    entityManager=entityManager.getEntityManagerFactory().createEntityManager();
    worked.

    Thank you

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    Because it is an @PostConstruct method... That is invoked BEFORE the tranactional proxies are created, hence no transactional entitymanager is available yet.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2011
    Posts
    1

    Default

    What are the possible issues or downside you get from manually calling "createEntityManager" method in the PostConstruct method? Is there any?

  4. #4

    Default

    Is PostConstruct even the right place to call something like startAndWait()? What's the best practice for perfroming potentially long-running initialization tasks in EJBs?

  5. #5
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Default

    Closing as it's not a Roo issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •