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 1005555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:18:59+00:00 2026-05-16T08:18:59+00:00

I am using Glassfish v3 server. Usually the DB connection with EJB3 + JPA

  • 0

I am using Glassfish v3 server.

Usually the DB connection with EJB3 + JPA (Eclipselink) is done through injection, with @PersistenceUnit or @Persistencecontext.

However, there are 3 layers in my App :

  • Core (contains business logic, entities, exception handling etc)

  • an EJB on top of it, calling the right core objects and methods to do the job. This EJB is called by other internal modules of our ERP.

  • a REST layer on top of it for uses by frontend web sites.

I do not want to get the entityManager, nor the EMF (EM factory) in the EJB, because I want my middle layer to be unaware that there is a DB used under it. I could after all, later, decide to change my core implementation for a non-DB-using one.

I see only two bad solutions :

  • 1) Add an EM parameter every time i call a method of the core layer that needs DB connection. Very ugly and goes againt what I said above.

  • 2) In every method of core needing DB connection, I create a factory, an EM, use them, and then close them both.

I tried to cut things in the middle, having one factory per class of the Core level, and EMs are created and closed in every method. But I still have memory leaks like this :

javax.servlet.ServletException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: In-use connections equal max-pool-size and expired max-wait-time. Cannot allocate more connections.

I guess it’s because if one of my EJB methods uses 10 different objects, it creates 10 EM factories, and none of them is closed.

Example of typical use in a Core object:

 EntityManager em = emf.createEntityManager();
 em.getTransaction().begin();
 // do some stuff with em; for example persist, etc
 em.flush();
 em.close();

Should I go for solution 2?
Is there a way to use a single EM factory at this core level ? I seems like the JPA spec assumes you’re going to use the entities at the EJB level only, which is bad in a multi layers apps.

EDIT : here is the current status after trying @Inject :

  • Added an empty beans.xml file in the /META-INF directory on my CORE jar.

  • The parent DAO class is now like this :

    public class ExampleBZL {

     public EntityManagerFactory emf;
     @Inject public Emf emfobject;
    
     public ExampleBZL()
     {
        this.emf = emfobject.emf;
     }
    
  • The Emf class is very simple, and Stateless.

    @Stateless
    public class Emf implements EmfAbstract {

    @PersistenceUnit(unitName = Setup.persistenceUnitName)
    public EntityManagerFactory emf;
    
    public Emf()
    {
    }
    

    }

I must be doing something wrong, but the injection doesn’t work, altough in glassfish I see “[ejb, weld, web]” in the engines list, so the CDI is loaded.

Servlet.service() for servlet Jersey Web Application threw exception
java.lang.NullPointerException
    at com.blablabla.core.bizlogic.ExampleBZL.<init>(ExampleBZL.java:40)

Am I missing other annotations ?? Is it really working to do an inection in a JAR with those two small annotations (Stateless on one side, Inject on the other) ?

  • 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-16T08:19:00+00:00Added an answer on May 16, 2026 at 8:19 am

    What if you had two session beans? One with the injected EntityManager that can leverage JTA, and the other is your current session bean.

    I am currently putting together a series on my blog using a session bean as the REST service using EclipseLink & Glass Fish v3:

    • Part 1 – The Database Model
    • Part 2 – Mapping the Database Model to JPA Entities
    • Part 3 – Mapping JPA Entities to XML using JAXB
    • Part 4 – The RESTful Service

    Below is how I inject the EntityManager on my session bean that is serving as my REST service:

    package org.example.customer;
    
    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.PersistenceContextType;
    import javax.ws.rs.Path;
    
    import org.eclipse.persistence.rest.JPASingleKeyResource;
    
    @Stateless
    @LocalBean
    @Path("/customers")
    public class CustomerService  {
    
        @PersistenceContext(unitName="CustomerService", type=PersistenceContextType.TRANSACTION)
        EntityManager entityManager;
    
    }
    

    You can link your session beans using the @EJB annotation:

    package org.example;
    
    import javax.ejb.EJB;
    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    @Stateless
    @LocalBean
    @EJB(name = "someName", beanInterface = CustomerService.class)
    public class OtherSessionBean {
    
        public Customer read(long id) {
            try {
                Context ctx = new InitialContext();
                CustomerService customerService = (CustomerService) ctx.lookup("java:comp/env/someName");
                return customerService.read(id);
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.