簡體   English   中英

在JNDI中為EJB模塊添加一個持久性單元

[英]Adding a persistence unit in JNDI for EJB modules

我正在使用示例代碼來開發一個簡單的EJB模塊。 我正在嘗試通過SOAP Web服務實現CURD操作。 我在定義持久化單元persistence.xml

這是我的實現代碼。 問題是我無法創建PersistenceService的實例,因為JNDI列表中不存在持久性單元名稱。

如果不是使用代碼注入,而是使用

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

代碼運行,但是隨后由容器管理事務。 我正在閱讀一些教程,他們提到,在這種情況下,如果用戶想稍后回滾事務,則無法這樣做。

我該怎么辦?

用戶類

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

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

PersistenceService類-借用NetBeans生成的示例代碼

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 = 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();
    }
}

如果您的persistence.xml中有一個條目

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

在您的PersistenceService類中,添加注釋:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM