How to use JPA from a JBoss Web application ?

In simple terms, if you are not using EJB or CDI, the user is expected to interact with javax.transaction.UserTransaction which defines the transaction boundaries.

Here is an example Servlet which persists an Entity by injecting the UserTransaction and adding the EntityManager via the @PersistenceContext annotation:

@WebServlet(name="JPAServlet", urlPatterns={"/jpademo"})
public class JPAServlet extends HttpServlet {
    private static final Logger LOG = Logger.getLogger(TestServlet.class);

    private UserTransaction txn;

    private EntityManager em;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            em.persist(new Customer(1, "John Smith"));

          } catch (NotSupportedException beginExceptions) {
              LOG.errorf(beginExceptions, "Cannot start transaction: '%s'", txn);
          } catch (SecurityException | IllegalStateException | RollbackException
                  | HeuristicMixedException | HeuristicRollbackException commitExceptions) {
              LOG.errorf(commitExceptions, "Cannot commit transaction: '%s'", txn);
          } catch (SystemException systemException) {
              LOG.errorf(systemException, "Unexpected error condition on work with transaction: '%s'", txn);

In the above example, the transaction-scoped @PersistenceContext coordinates with the (active) JTA transaction. When the transaction commits, the persistence context is flushed to the datasource (entity objects are detached but may still be referenced by application code). All entity changes that are expected to be saved to the datasource, must be made during a transaction.

Aside from that, you will only need, as usual a persistence.xml to provide a valid Database connection:

<persistence version="2.1"
   xmlns="" xmlns:xsi=""
   <persistence-unit name="primary">
         <!-- Properties for Hibernate -->
         <property name="" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />

The above example, uses the default Datasource available in WildFly, which is bound to “java:jboss/datasources/ExampleDS”