【发布时间】:2022-06-17 07:57:34
【问题描述】:
大家好,我正在从 Wildfly 迁移到 Quarkus,但无法理解如何以编程方式检索命名数据源。下面是迄今为止在 Wildfly 中用于以编程方式检索 datasourceName 的代码(数据源 datasourceName 已在 application.properties 以及许多其他代码中定义)
Properties p = new Properties();
p.put(AvailableSettings.DATASOURCE, datasourceName);
p.put(AvailableSettings.JTA_PLATFORM, JBossAppServerJtaPlatform.class.getName());
p.put("current_session_context_class", "jta");
p.put(AvailableSettings.SHOW_SQL, false);
p.put(AvailableSettings.FORMAT_SQL, false);
// Adding "hibernate.classLoaders" property is critical for this to work with keycloak!!!
p.put(AvailableSettings.CLASSLOADERS, Collections.singletonList(getClass().getClassLoader()));
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, p);
conf/quarkus.properties
quarkus.datasource.user-store.db-kind=mysql
quarkus.datasource.DATASOURCEA.db-kind=mysql
quarkus.datasource.DATASOURCEA.username=USER
quarkus.datasource.DATASOURCEA.password=PASSWORD
quarkus.datasource.DATASOURCEA.jdbc.url=jdbc:mysql://HOSTNAME/DATABASE
在 Quarkus 中运行此类代码时,抛出异常
The FastbootHibernateProvider PersistenceProvider can not support runtime provided properties. Make sure you set all properties you need in the configuration resources before building the application.
由FastBootHibernatePersistenceProvider.java line提供
@SuppressWarnings("rawtypes")
private void verifyProperties(Map properties) {
if (properties != null && properties.size() != 0) {
throw new PersistenceException(
"The FastbootHibernateProvider PersistenceProvider can not support runtime provided properties. "
+ "Make sure you set all properties you need in the configuration resources before building the application.");
}
}
意思是,不允许有任何属性。
试过
Arc.container().instance(EntityManagerFactory.class, new PersistenceUnit.PersistenceUnitLiteral(datasourceName))
但投掷
WARN [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (JPA Startup Thread: user-storage-jpa) HHH000342: Could not obtain connection to query metadata: java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68)
对此有任何提示吗?真的很难找到任何有用的参考来实现这一点。只能找到injection-based datasources。
非常感谢, 尼古拉
【问题讨论】:
-
您好,您无法通过
application.properties设置哪些属性? -
@geo 我可以全部设置,但我无法以编程方式访问指定的数据源。换句话说,我无法从
application.properties中定义的名称源A、B或C获得EntityManagerFactory- 我只能通过注解注入。 -
您是否尝试过使用以下方式以编程方式获取它:
Arc.container().instance(EntityManagerFactory.class, new io.quarkus.hibernate.orm.PersistenceUnit.PersistenceUnitLiteral("A")).get()? -
感谢@geoand - 尝试但出现异常,使用堆栈跟踪更新了上面的问题,因为无法在 cmets 中写入太多内容
-
您是否尝试自行启动 Hibernate?如果是这样,则不支持
标签: java datasource quarkus jta