Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3964910
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:23:30+00:00 2026-05-20T03:23:30+00:00

In order to get RequestFactory to persist attached entities , I need to ensure

  • 0

In order to get RequestFactory to persist attached entities, I need to ensure that I use the same EntityManager for each request.

I cobbled together my own Factory class for this based on a ThreadLocal implementation, but I’m unsure how to properly release resources (e.g. how to know that the request has finished and call close()).

Is there a simple way to ensure that a single EntityManager is used throughout a given ServletRequest without resorting to full-on J2EE/CDI? I’ll take that route if I must, but I was hoping to keep things simple, especially since I’d like to continue using the lightweight development server that comes with GWT.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:23:31+00:00Added an answer on May 20, 2026 at 3:23 am

    Here is what I ultimately came up with, based on feedback from the GWT Google Group, and BobV.

    Create a thread-local holder for the EntityManager; reference this in your entities when they need to get an EntityManager:

    public class ThreadLocalEntityManager
    {
        private static ThreadLocal<EntityManager> holder = new ThreadLocal<EntityManager>();
    
        private ThreadLocalEntityManager()
        {
        }
    
        public static EntityManager get()
        {
            return holder.get();
        }
    
        public static void set(EntityManager em)
        {
            holder.set(em);
        }
    }
    

    Then create a filter that will set the initial EntityManager for the request:

    public class PersistenceFilter implements Filter
    {
        protected static final Logger log = Logger.getLogger(PersistenceFilter.class.getName());
        private EntityManagerFactory factory;
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException
        {
            factory = Persistence.createEntityManagerFactory("my_persistence");
        }
    
        @Override
        public void destroy()
        {
            factory.close();
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
        {
            EntityManager em = factory.createEntityManager();
            ThreadLocalEntityManager.set(em);
    
            EntityTransaction tx = em.getTransaction();
            tx.begin();
    
            try
            {
                chain.doFilter(req, res);
                tx.commit();
            }
            catch (Exception e)
            {
                tx.rollback();
            }
            finally
            {
                log.info("closing EntityManager: " + EMF.entityManager());
                em.close();
            }
    
        }
    }
    

    Then apply the filter to the /gwtRequest URL pattern:

    <filter>
        <filter-name>PersistenceFilter</filter-name>
        <filter-class>com.example.PersistenceFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>PersistenceFilter</filter-name>
        <url-pattern>/gwtRequest</url-pattern>
    </filter-mapping>
    

    Note that there is a flaw here- an EntityManager is created for each request that goes through this servlet, whether it’s used by your underlying code or not. It could probably stand to be made more robust and somehow lazily create the EntityManager (and the transaction) only when requested.

    But so far this code seems to work well with RequestFactory. Suggestions for improvements highly welcomed.

    Note: this experience has taught me that it’s probably worth moving to full-on CDI rather than trying to implement pieces of it such as this. I just didn’t have the time available for such a move during this project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's simple view model that I use: public class ViewModel { public Order Order
In order to get XML data from a server repeatedly, I'm attempting to use
I'have several forms that use the same Listboxes. The ListBoxes are populated from a
In order to get my setup a bit closer to one click deployment, I
In order to get better SEO and cleaner URLs, I tend to export certain
In order to get the next element in the list one just needs to
I'm attempting to get order data from a Yahoo store. I'm using the docs
What exactly needs to happen in order to get detailed SQL logs out of
I am using saxparser in order to get xml tag contents from file. All
How to fix this regex in order to get the mentioned outcome ? regex:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.