【问题标题】:Adding a persistence unit in JNDI for EJB modules在 JNDI 中为 EJB 模块添加持久性单元
【发布时间】:2009-12-04 05:36:05
【问题描述】:

我正在使用示例代码开发一个简单的 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();
    }
}

【问题讨论】:

    标签: java jpa jakarta-ee


    【解决方案1】:

    如果你的 persistence.xml 有条目

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

    在您的 PersistenceService 类中,添加注释:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      相关资源
      最近更新 更多