Results 1 to 9 of 9

Thread: OpenEntityManagerInViewFilter and Worker Thread

  1. #1
    Join Date
    Dec 2008
    Posts
    3

    Default 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. #2
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    217

    Default

    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());
            }

  3. #3
    Join Date
    Dec 2008
    Posts
    3

    Default

    That worked,
    Thank you.

  4. #4
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    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.

  5. #5
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    217

    Default

    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();

  6. #6
    Join Date
    Sep 2004
    Posts
    1,086

    Default

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

  7. #7
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    217

    Default

    Yes, alternative solutions is to use transaction management.

  8. #8
    Join Date
    Dec 2008
    Posts
    3

    Default

    Quote 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.

  9. #9
    Join Date
    Sep 2004
    Posts
    1,086

    Default

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

Posting Permissions

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