java中createfactory_java – 当调用createEntityManagerFactory时,“在...

我试图通过在我的应用程序中分离模式来实现多租户.在这样做时,我有一个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.出了什么问题,我怎么能纠正它?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值