Announcement Announcement Module
Collapse
No announcement yet.
OpenEntityManagerInViewFilter and Worker Thread Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenEntityManagerInViewFilter and Worker Thread

    Hi,

    I am using:
    • Spring 2.5.5
    • Hibernate 3.3.0.ga

    I have a web app where I am using OpenEntityManagerInViewFilter which works fine for http requests.

    However I have a need for a background thread to do some processing.
    Processing in the worker thread is trigger by events in the http request threads (ie not scheduled).
    I start a single thread when the application starts (Thread.start)

    However in this thread I am unable to access any data as I get:

    org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    Is it possible to create a session in the worker thread and bind it so that the normal JPA annotations, will work correctly?

    TIA

  • #2
    Try:
    Code:
            EntityManagerFactory entityManagerFactory = getEntityManagerFactory();
            EntityManager entityManager = entityManagerFactory.createEntityManager();
            TransactionSynchronizationManager.bindResource(entityManagerFactory, new EntityManagerHolder(entityManager));
    
            try {
    
                // your functionality
                
            } finally {
                EntityManagerHolder emHolder = (EntityManagerHolder)
                TransactionSynchronizationManager.unbindResource(entityManagerFactory);
                EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
            }

    Comment


    • #3
      That worked,
      Thank you.

      Comment


      • #4
        EntityManager instances are not threadsafe (because jdbc Connection instances are not threadsafe). It may appear that it works, but I wouldn't bet on it.

        Comment


        • #5
          The idea is to open EntityManager in one thread and not share it with other threads, the same as in OpenEntityManagerInViewFilter.

          Code:
                  new Thread(new Runnable() {
                      public void run() {
                          EntityManagerFactory entityManagerFactory = getEntityManagerFactory();
                          EntityManager entityManager = entityManagerFactory.createEntityManager();
                          TransactionSynchronizationManager.bindResource(entityManagerFactory, new EntityManagerHolder(entityManager));
          
                          try {
          
                              // your functionality
          
                          } finally {
                              EntityManagerHolder emHolder = (EntityManagerHolder)
                              TransactionSynchronizationManager.unbindResource(entityManagerFactory);
                              EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
                          }
                      }
                  }).start();

          Comment


          • #6
            Sorry, I missed the create part. Why not simply creating a new transaction in the thread then?

            Comment


            • #7
              Yes, alternative solutions is to use transaction management.

              Comment


              • #8
                Originally posted by dejanp View Post
                Sorry, I missed the create part. Why not simply creating a new transaction in the thread then?

                I tried to do this, but it is necessary to bind the transaction to spring otherwise you get an error when you enter an @Transactional method (nested transaction).

                Anyway with this solution you are free to start and finish transactions using the JPA notation.

                Comment


                • #9
                  Yes, but you can use TransactionTemplate for that instead of doing it manually.

                  Comment

                  Working...
                  X