我试图通过在我的应用程序中分离模式来实现多租户.在这样做时,我有一个Tenant实体,它包含一个String schemaName,我有一个Singleton Startup EJB,它在启动时创建一个EntityManagerFactory的映射;每个租户分配一个工厂.
这是我的EJB:
@Startup
@Singleton
public class TenantManagementServiceImpl implements TenantManagementService {
private Map entityManagerFactoryMap;
@PersistenceContext
private EntityManager entityManager;
@PostConstruct
private void init()
{
buildEntityManagerFactories();
}
private List getAllTenants() {
return entityManager.createNamedQuery("Tenant.getAll", Tenant.class).getResultList();
}
private void buildEntityManagerFactories() {
entityManagerFactoryMap = new HashMap<>();
for (Tenant tenant : getAllTenants()) {
Map properties = new HashMap<>();
properties.put("hibernate.default_schema", tenant.getSchemaName());
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyApp", properties);
entityManagerFactoryMap.putIfAbsent(tenant, entityManagerFactory);
}
}
@Override
public EntityManagerFactory getEntityManagerFactory(Tenant tenant) {
return entityManagerFactoryMap.get(tenant);
}
}
并使用NamedQuery:
@NamedQuery(name = "Tenant.getAll", query = "SELECT t FROM Tenant t")
不幸的是,在启动时,我收到此错误:
java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"myapp-1.0-SNAPSHOT.war\".component.TenantManagementServiceImpl.START" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"myapp-1.0-SNAPSHOT.war\".component.TenantManagementServiceImpl.START: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
Caused by: javax.ejb.EJBException: javax.persistence.PersistenceException: [PersistenceUnit: MyApp] Unable to build Hibernate SessionFactory
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyApp] Unable to build Hibernate SessionFactory
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open JDBC connection for schema management target
Caused by: java.sql.SQLException: IJ031017: You cannot set autocommit during a managed transaction"}}
错误发生在这一行:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyApp", properties);
我使用this guide作为参考.我不明白为什么我收到这个错误.我正在使用WildFly 10.出了什么问题,我怎么能纠正它?

266

被折叠的 条评论
为什么被折叠?



