How to bind the EntityManager in JNDI?

By default the persistence unit are available in the java: Context. If you wish to make them available also in the global naming Context you have to add two properties to your persistence.xml configuration file:

  1. jboss.entity.manager.jndi.name gives you a transaction scoped entity manager you can interact with
  2. jboss.entity.manager.factory.jndi.name binds the entity manager factory into global JNDI.
<persistence>
   <persistence-unit name="manager1">
      <jta-data-source>java:/MySQLDS</jta-data-source>
      <properties>
         <property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/>
         <property name="jboss.entity.manager.factory.jndi.name" value="java:/Manager1Factory"/>
      </properties>
   </persistence-unit>
</persistence>

The typical use case for binding the EntityManager is to share the Persistence Unit across deployments. To achieve that:

  1. Firstly, the secondary deployment would declare a classloading dependency on the persistence jar deployment via jboss-deployment-structure.xml
  2. Then, just inject the EntityManager or look it up such as:
@Resource(lookup="java:/Manager1")
private EntityManager entityManager;

 


Recommended Articles

Master JPA Entity Listeners: Enhance Your Java Persistence API with Callbacks

Learn how to leverage JPA Entity Listeners for customizing entity lifecycle events in WildFly 31 and beyond. #JPA #WildFly #JavaPersistenceAPI

Enhance WildFly Logging for Detailed Persistence Service Debugging

Increase WildFly logging levels for detailed persistence service debugging. Learn how to trace Entity Manager operations and JDBC interactions using TRACE level.

Fixing SQL Syntax Errors Caused by Reserved Words in JPA Entity Classes

Learn how to resolve errors caused by using reserved words in JPA entity classes and avoid SQL syntax issues.

Dynamic Data Source Specification in Persistence.xml for WildFly and Cloud Environments

Learn how to dynamically specify data sources in persistence.xml for improved flexibility, especially useful in cloud environments like OpenShift.