Note that there are some explanatory texts on larger screens.

plurals
  1. POAddding a persistence unit in JNDI for EJB modules
    text
    copied!<p>I am working on a simple EJB module using sample code. I am trying to implement CURD operations through a SOAP web service. I have a persistence unit defined in <code>persistence.xml</code>.</p> <p>Here is the code for my implementation. The problem is that I cannot create an instance of <code>PersistenceService</code> because the persistence unit name is not present in the JNDI listings.</p> <p>If, instead of using code injection, I use</p> <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory(PU_NAME); em = emf.createEntityManager(); </code></pre> <p>the code runs, but then the transaction is managed by the container. I was reading through some tutorials and they mentioned that, in this scenario, if the user wants to roll the transaction back later, he cannot do so.</p> <p>What can I do instead?</p> <h3>users class</h3> <pre><code>@WebService() @Stateless() public class users { public users() { } /** * Web service operation */ @WebMethod(operationName = "addUser") public Integer addUser( @WebParam(name = "UserName") final String UserName, @WebParam(name = "LastName") final String LastName) { DatabaseEntityManager dem = new DatabaseEntityManager(); Integer result = null; try { result = dem.addUser(UserName, LastName, false); dem.commitTx(); return result; } catch(Exception E) { } return new Integer(-1); } </code></pre> <h3>DatabaseEntityManager class</h3> <pre><code>public class DatabaseEntityManager { PersistenceService ps_bck = null; public DatabaseEntityManager() { } public SiteUsers addUser( String Username, String LastName, boolean commit) throws Exception { AppUser appUser = new appUser(UserName, LastName); //AppUser is an entity class PersistenceService ps = PersistenceService.getInstance(); try { ps.beginTx(); EntityManager em = ps.getEntityManager(); em.persist(appUser); if (commit) ps.commitTx(); else ps_bck = ps; } catch (Exception E) { ps.rollbackTx(); } finally { ps.close(); } return appUser.getId(); } void commitTx() throws Exception { try { ps_bck.commitTx(); } catch(Exception E) { throw E; } finally { ps_bck =null; } } } </code></pre> <h3>PersistenceService class &#8212; borrowed from sample code generated by NetBeans</h3> <pre><code>public class PersistenceService { private static String DEFAULT_PU = "pers-ejbPU"; private static ThreadLocal&lt;PersistenceService&gt; instance = new ThreadLocal&lt;PersistenceService&gt;() { @Override protected PersistenceService initialValue() { return new PersistenceService(); } }; private EntityManager em; private UserTransaction utx; private PersistenceService() { try { //This code runs // EntityManagerFactory emf = ersistence.createEntityManagerFactory(DEFAULT_PU); // em = emf.createEntityManager(); //This code throws an exception this.em = (EntityManager) new InitialContext().lookup("java:comp/env/persistence/"+ DEFAULT_PU); this.utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); } catch (NamingException ex) { throw new RuntimeException(ex); } } /** * Returns an instance of PersistenceService. * * @return an instance of PersistenceService */ public static PersistenceService getInstance() { return instance.get(); } private static void removeInstance() { instance.remove(); } /** * Returns an instance of EntityManager. * * @return an instance of EntityManager */ public EntityManager getEntityManager() { return em; } /** * Begins a resource transaction. */ public void beginTx() { try { utx.begin(); em.joinTransaction(); } catch (Exception ex) { throw new RuntimeException(ex); } } /** * Commits a resource transaction. */ public void commitTx() { try { utx.commit(); } catch (Exception ex) { throw new RuntimeException(ex); } } /** * Rolls back a resource transaction. */ public void rollbackTx() { try { utx.rollback(); } catch (Exception ex) { throw new RuntimeException(ex); } } /** * Closes this instance. */ public void close() { removeInstance(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload