我正在从WF 7.4.0.FINAL迁移到16.0.0.FINAL。解决了不同模块的升级问题之后,我遇到了以下问题:WELD Ambiguousdependent exception。如何保持与两个数据库的连接?
似乎SessionFactory的hibernate-core-5.3.9现在从EntityManagerFactory扩展了,而对于hibernate-core-4.2.14(WF7.4)则没有。 SessionFactory在HibernateProducer中定义,并且一个单独的实例注入到EnvironmentProducer中(专门用于jbpm)。似乎这两个相互干扰,而Wildfly 7.4中却没有。
它连接到2个数据库:一个jbpm和一个特定于应用程序的数据库。两者都定义了persistence.xml。
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.PersistenceUnit;
public class HibernateProducer {
@Produces
@ApplicationScoped
public SessionFactory getSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
return configuration.buildSessionFactory();
}
public void closeSessionFactory(@Disposes SessionFactory sessionFactory) {
sessionFactory.close();
}
}
import org.jbpm.persistence.JpaProcessPersistenceContextManager;
import org.jbpm.persistence.jta.ContainerManagedTransactionManager;
import org.jbpm.process.audit.JPAWorkingMemoryDbLogger;
import org.jbpm.process.core.timer.GlobalSchedulerService;
import org.jbpm.runtime.manager.impl.DefaultRuntimeEnvironment;
import org.jbpm.services.cdi.impl.manager.InjectableRegisterableItemsFactory;
import org.jbpm.services.task.persistence.JPATaskPersistenceContextManager;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.task.UserGroupCallback;
import org.kie.internal.runtime.manager.RuntimeEnvironment;
import org.kie.internal.runtime.manager.cdi.qualifier.PerProcessInstance;
import org.kie.internal.runtime.manager.cdi.qualifier.PerRequest;
import org.kie.internal.runtime.manager.cdi.qualifier.Singleton;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ApplicationScoped
public class EnvironmentProducer {
@Inject
protected BeanManager beanManager;
@Inject
protected CDIGlobalResolver globalResolver;
@Inject
protected GlobalSchedulerService schedulerService;
@Inject
protected UserGroupCallback userGroupCallback;
@PersistenceUnit(unitName = "org.jbpm.domain")
protected EntityManagerFactory emf;
// @Inject
// protected ContainerManagedTransactionManager transactionManager;
@Inject
private Instance<ResourceProducer> resourceProducers;
@Produces
@Singleton
@PerRequest
@PerProcessInstance
public RuntimeEnvironment produceEnvironment() {
DefaultRuntimeEnvironment runtimeEnvironment = new DefaultRuntimeEnvironment();
runtimeEnvironment.addToConfiguration("drools.commandService",
SingleThreadCommandService.class.getName());
runtimeEnvironment.setEmf(emf);
runtimeEnvironment.setSchedulerService(schedulerService);
runtimeEnvironment.setUserGroupCallback(userGroupCallback);
runtimeEnvironment
.setRegisterableItemsFactory(InjectableRegisterableItemsFactory
.getFactory(beanManager, new JPAWorkingMemoryDbLogger(
emf)));
Environment env = runtimeEnvironment.getEnvironmentTemplate();
runtimeEnvironment.addToConfiguration("drools.commandService",
SingleThreadCommandService.class.getName());
runtimeEnvironment.addToEnvironment(EnvironmentName.GLOBALS,
globalResolver);
runtimeEnvironment.addToEnvironment(
EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
runtimeEnvironment.addToEnvironment(
EnvironmentName.TRANSACTION_MANAGER, new ContainerManagedTransactionManager());
runtimeEnvironment.addToEnvironment(
EnvironmentName.PERSISTENCE_CONTEXT_MANAGER,
new JpaProcessPersistenceContextManager(env));
runtimeEnvironment.addToEnvironment(
EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER,
new JPATaskPersistenceContextManager(env));
for (ResourceProducer resourceProducer : resourceProducers) {
ResourceType resourceType = resourceProducer.getResourceType();
for (Resource resource : resourceProducer.getResources()) {
runtimeEnvironment.addAsset(resource, resourceType);
}
}
runtimeEnvironment.init();
return runtimeEnvironment;
}
@Produces
@ApplicationScoped
//@Named
public EntityManagerFactory getEntityManagerFactory() {
return emf;
}
}
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type EntityManagerFactory with qualifiers @Default
at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedMethod] @Inject public be.bside.process.jbpm.service.ProcessEngineService.init(EntityManagerFactory)
at be.bside.process.jbpm.service.ProcessEngineService.init(ProcessEngineService.java:0)
Possible dependencies:
- Producer Method [SessionFactory] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @ApplicationScoped public be.bside.common.store.HibernateProducer.getSessionFactory()],
- Producer Method [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @ApplicationScoped public be.bside.process.jbpm.service.EnvironmentProducer.getEntityManagerFactory()]
一个简单的解决方法是将@Typed(SessionFactory.class)
放在您的@Typed(SessionFactory.class)
方法上。