English 中文(简体)
EJBException when calls entitiesManager.getTransaction()
原标题:EJBException when calling entityManager.getTransaction()

这可能是微不足道的,但我热爱一些帮助。

我收到了:

 javax.ejb.EJBException: java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager 
 11:54:37,105 ERROR [STDERR] at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:77)
 11:54:37,105 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
 11:54:37,105 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)

在这样做时:

@PersistenceContext(unitName = "someName")
private EntityManager em;

...

final EntityManager entityManager = getEntityManager();
final EntityTransaction tx = entityManager.getTransaction(); // here

谁能告诉我什么原因?

最佳回答

在Java EE管理的情况下,查阅与实体管理有关的实体交易案件是非法的。 http://download.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#get Transaction%28%29”

Return the resource-level EntityTransaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.

Returns:
    EntityTransaction instance 
Throws:
    IllegalStateException - if invoked on a JTA entity manager

最后一行是相关的。

当你将实体管理注入在应用服务器上部署的EJB应用服务器上,使用@PersistenceContext或@Inject说明时,实体管理者将由该集装箱管理,而不是通过申请管理。 集装箱管理实体经理必须是JTA实体管理人;申请管理的实体管理人员可以是资源-当地实体管理人员。 这一点由《警察法》的具体规定决定:

An entity manager whose underlying transactions are controlled through JTA is termed a JTA entity manager.

An entity manager whose underlying transactions are controlled by the application through the EntityTransaction API is termed a resource-local entity manager.

A container-managed entity manager must be a JTA entity manager. JTA entity managers are only specified for use in Java EE containers.

从第一点(关于非法国家接纳)来看,你不能获得集装箱注入实体管理者的实体交易参考。 但是,如果该集装箱只注入实体管理工具,如果你的申请通过援引<代码>获得实体管理条例参考,你可以这样做。 实体ManagerFactory.getEntityManager。

此外,应当指出,援引<代码>意向Manager.get Transaction()对联合信托机构实体经理来说毫无意义。 在实体交易接口的定义中,JPA的规格表明了这一点:

实体转账接口用于控制资源-当地实体管理人员的资源交易。

关于管理联合安排交易本身的专题,如果您需要管理交易界(即使用公证管理的交易),则采用<代码>User Transaction。 或者,如果你希望集装箱管理交易,那么简单地说明该方法或信标,并注明适当的

在应用服务器中使用有条理或集装箱管理的交易的资源-当地实体管理人员(和数据来源)通常不是一个好的想法,但可以这样做。

您将找到一个适当的例子,表明在。 如果你已经拿到附加说明的灯类或方法,则单是避免援引“<>植被转移()”方法进行CMTs工作的话,CMT就更是微不足道。

If you wish to understand further, I would recommend reading Chapter 7 of the JPA 2.0 specification, titled "Entity Managers and Persistence Contexts". The examples provided in the chapter demonstrate:

  • how JTA entity managers ought to be used in an application server (which is typically the place where they are used).
  • how resource-local entity managers may be used in an application server.
  • how resource-local entity managers can be used in a Java SE application.
问题回答

You don t need to instantiate the EntityManager by hand, your container does that for you because of the @PersistenceContext annotation. Also, you don t need to begin the transaction manually, it s also provided by your containter. Just use your em field and forget about the other ones.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签