我正在开发一个web应用程序,我必须使用我从未使用过的JTA。我开始使用EntityManager,但它在这里似乎不起作用。当我使用EntityManager时,我得到这样的消息:
Only persistence units with transaction type JTA can be used as a container managed entity manager.
简而言之,我有这段代码:
@PersistenceContext(unitName = "zJSF2PU")
private EntityManager em;
em.getTransaction().begin();
//some code
em.getTransaction().commit();
没有EntityManager我该怎么做呢?
发布于 2013-03-17 17:01:20
我终于能够解决我的问题了。根据我的搜索,例如,当你在ManagedBeans中使用JTA时,你不能使用EntityManager。但是,它可以在无状态bean中使用,然后我们可以将这个无状态Bean注入到ManagedBean中并使用它的方法。具体步骤如下:
EJB
有关更多信息,请参阅另一篇文章:JTA & MySQL
发布于 2013-03-17 03:52:56
在您的ejb项目META-INF/persistence.xml
中,您必须具有以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="myPersistenceUnitNamePersonalised" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/MySQL</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="FINE" />
</properties>
</persistence-unit>
</persistence>
你必须在你的Application Server
(jboss,tomcat,glassfish)中声明这一点
您需要搜索如何在Application Server
中添加数据源和持久性单元...
就是这样..。他们通过jndi交流。
发布于 2013-03-17 04:10:30
transaction-type="RESOURCE_LOCAL"
的em.getTransaction()
调用中删除em.getTransaction()
。注入javax.transaction.UserTransaction
(JTA)并使用其begin/commit/rollback方法。或者,将EM注入无状态的EJB,并允许EJB容器自动管理事务。https://stackoverflow.com/questions/15453797
复制相似问题