tags:

views:

230

answers:

1

Hi!

I am working on a simple ejb module, sample code where i am trying to implement CURD operation through a SOAP webservice. My problem is like this. I have a persistence unit defined in persistence.xml file.

Here is the code for my implementation. The problem is that I cannot create the instance of PersistenceService class since, the persistence unit name is not present in the jndi listings.

If instead of using code injection if I use

EntityManagerFactory emf = Persistence.createEntityManagerFactory(PU_NAME); em = emf.createEntityManager();

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 rollback the transaction later, he cannot do so.

Any pointers?

// Source code for users.java

@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);

 }

//source code databaseentitymanager

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;
  }

} }

//Source code for PersistenceService - borrowed from sample code generated by Netbeans ide

public class PersistenceService { private static String DEFAULT_PU = "pers-ejbPU";

private static ThreadLocal<PersistenceService> instance = new ThreadLocal<PersistenceService>() {
    @Override
    protected PersistenceService initialValue() {
        return new PersistenceService();
    }
};


private EntityManager em;
private UserTransaction utx;



private PersistenceService() {

    try {

       //This code runs 
       // EntityManagerFactory emf = Persistence.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();
}

}

A: 

If your persistence.xml has an entry

 <persistence-unit name="PU_NAME" transaction-type="JTA">

In your PersistenceService class, add the annotation:

@PersistenceContext (name = "PU_NAME") private EntityManager em;
vinny