注入WildFly时,EntityManager为空10 EJB
问题描述:
我一直在努力将应用程序从JBoss AS 6移动到Wildfly 10.我一直坚持的主要问题是EntityManager没有注入到EJB中。我一直在研究这一段时间,并试图找到一切,但没有任何帮助。注入WildFly时,EntityManager为空10 EJB
我没有一个简单的应用程序来重新创建问题,但这里有一些细节和代码片段。
我们正在使用SAR文件进行部署。这是一个Spring框架应用程序。暂时关闭我们的二级缓存。这是我需要解决的另一个问题。
的persistence.xml:从独立-full.xml数据源的
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="IpsDb" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/HarmonyServerDS</jta-data-source>
<jar-file>../harmonyserver-model.jar</jar-file>
<properties>
<!-- pessimistic lock timeout in milliseconds (Integer or String), this is a hint used by Hibernate but requires support by your underlying database. -->
<property name="javax.persistence.lock.timeout" value="15000"/>
<!-- query timeout in milliseconds (Integer or String), this is a hint used by Hibernate but requires support by your underlying database -->
<property name="javax.persistence.query.timeout" value="15000"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
</properties>
</persistence-unit>
</persistence>
的起点:
<datasource jta="true" jndi-name="java:jboss/datasources/HarmonyServerDS" pool-name="HarmonyServerDS" enabled="true" use-java-context="true" spy="false" use-ccm="true" connectable="false">
从EJB类(这些都是重要的组成部分,但不是所有的代码该类):
@Stateless
@Clustered
public class DataModificationBean implements DataModificationLocal {
@PersistenceUnit(unitName="IpsDb")
private EntityManagerFactory entityMgrFactory;
@PersistenceContext(unitName="IpsDb")
private EntityManager entityMgr;
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
@InterruptOnTransactionTimeout(value=true)
public DataModificationResponse handleModification(DataModification mod, ModificationHandler handler, AdaptationContext adaptation, boolean bulk_lock)
{
try {
// Handler's data update monitor for delta changes.
DataUpdateMonitor updateMonitor = null;
// Pre-process the request and gather up the dataContext for processing.
logger.info("DataModificationBean EntityMgr=" + (entityMgr == null ? "null" : entityMgr.toString()));
logger.info("DataModificationBean EntityMgrFactory=" + (entityMgrFactory == null ? "null" : entityMgrFactory.toString()));
handler.preProcess(this, entityMgr);
...
该片段底部的日志记录显示entityMgr和ent ityMgrFactory为null。
还有什么我不见了?或者其他我能证明的事情会有帮助吗?
答
我发现了这个问题。我不得不将JNDI bean名称从我认为是旧格式的新名称改为新格式。我改变了其中一些,但不是这个豆的一个。没有从上下文中找到该bean,因此在代码中创建了一个新的bean。我们在这里只有测试用例的代码,但我没有注意到它正在做什么。现在我修复了JNDI名称,找到了bean并且EntityMgr被注入了。