We are currently developing a project using Apache Camel and Quarkus, in which we have introduced a new database called "db03" (MSSQL). Several tables have been migrated to this database. To facilitate queries from "db03", we have implemented the following changes using annotation @DataSource. But it seems code is not picking the correct connection. Please help.
application.properties file
#Quarkus DB
quarkus.datasource.jdbc.max-size=100
quarkus.datasource.db-kind=mssql
quarkus.datasource.jdbc.url=jdbc:sqlserver://localhost:1433;database=db01;encrypt=true;trustServerCertificate=true;username=abc;password=xyz
#Quarkus DB03
quarkus.datasource.db03.jdbc.max-size=100
quarkus.datasource.db03.db-kind=mssql
quarkus.datasource.db03.jdbc.url=jdbc:sqlserver://localhost:1433;database=db03;encrypt=true;trustServerCertificate=true;username=abc;password=xyz
quarkus.hibernate-orm.db03.database=db03
getDataFromQuoteAudit.java
@ApplicationScoped
public class GetDataFromQuoteAudit implements Processor {
private final QuoteAuditRepository quoteAuditRepository;
@Inject
public GetDataFromQuoteAudit(@DataSource("db03") QuoteAuditRepository quoteAuditRepository) {
this.quoteAuditRepository = quoteAuditRepository;
}
@Override
public void process(Exchange exchange) throws Exception {
String quoteId = (String) exchange.getIn().getHeader("quoteId");
try {
QuoteAudit quoteAudit = quoteAuditRepository.findById(Integer.parseInt(quoteId));
} catch (Exception e) {
throw new InvalidQuoteIdException("Id does not exists");
}
}
}
and QuoteAuditRepository.java
@ApplicationScoped
@DataSource("db03")
public class QuoteAuditRepository implements PanacheRepository<QuoteAudit> {
public QuoteAuditRepository() {
}
public QuoteAudit findById(int quoteId, long versionId, int itemId) {
return (QuoteAudit)this.find("quoteId = ?1 and versionId = ?2 and itemId = ?3", new Object[]{quoteId, versionId, itemId}).firstResult();
}
public QuoteAudit findById(int quoteId) {
return (QuoteAudit)this.find("quoteId = ?1", new Object[]{quoteId}).firstResult();
}
public void update(QuoteAudit quoteAudit) {
this.getEntityManager().merge(quoteAudit);
}
}
We are currently developing a project using Apache Camel and Quarkus, in which we have introduced a new database called "db03" (MSSQL). Several tables have been migrated to this database. To facilitate queries from "db03", we have implemented the following changes using annotation @DataSource. But it seems code is not picking the correct connection. Please help.
application.properties file
#Quarkus DB
quarkus.datasource.jdbc.max-size=100
quarkus.datasource.db-kind=mssql
quarkus.datasource.jdbc.url=jdbc:sqlserver://localhost:1433;database=db01;encrypt=true;trustServerCertificate=true;username=abc;password=xyz
#Quarkus DB03
quarkus.datasource.db03.jdbc.max-size=100
quarkus.datasource.db03.db-kind=mssql
quarkus.datasource.db03.jdbc.url=jdbc:sqlserver://localhost:1433;database=db03;encrypt=true;trustServerCertificate=true;username=abc;password=xyz
quarkus.hibernate-orm.db03.database=db03
getDataFromQuoteAudit.java
@ApplicationScoped
public class GetDataFromQuoteAudit implements Processor {
private final QuoteAuditRepository quoteAuditRepository;
@Inject
public GetDataFromQuoteAudit(@DataSource("db03") QuoteAuditRepository quoteAuditRepository) {
this.quoteAuditRepository = quoteAuditRepository;
}
@Override
public void process(Exchange exchange) throws Exception {
String quoteId = (String) exchange.getIn().getHeader("quoteId");
try {
QuoteAudit quoteAudit = quoteAuditRepository.findById(Integer.parseInt(quoteId));
} catch (Exception e) {
throw new InvalidQuoteIdException("Id does not exists");
}
}
}
and QuoteAuditRepository.java
@ApplicationScoped
@DataSource("db03")
public class QuoteAuditRepository implements PanacheRepository<QuoteAudit> {
public QuoteAuditRepository() {
}
public QuoteAudit findById(int quoteId, long versionId, int itemId) {
return (QuoteAudit)this.find("quoteId = ?1 and versionId = ?2 and itemId = ?3", new Object[]{quoteId, versionId, itemId}).firstResult();
}
public QuoteAudit findById(int quoteId) {
return (QuoteAudit)this.find("quoteId = ?1", new Object[]{quoteId}).firstResult();
}
public void update(QuoteAudit quoteAudit) {
this.getEntityManager().merge(quoteAudit);
}
}
Share
Improve this question
edited Apr 2 at 6:20
M. Deinum
125k22 gold badges233 silver badges249 bronze badges
asked Apr 1 at 11:34
user5489618user5489618
157 bronze badges
1
|
3 Answers
Reset to default 0Quarkus supports multiple datasources but you must follow reference syntax, see https://quarkus.io/guides/datasource#configure-multiple-datasources
example
quarkus.datasource.db-kind=h2
quarkus.datasource.username=username-default
quarkus.datasource.jdbc.url=jdbc:h2:mem:default
quarkus.datasource.jdbc.max-size=13
quarkus.datasource.users.db-kind=h2
quarkus.datasource.users.username=username1
quarkus.datasource.users.jdbc.url=jdbc:h2:mem:users
quarkus.datasource.users.jdbc.max-size=11
quarkus.datasource.inventory.db-kind=h2
quarkus.datasource.inventory.username=username2
quarkus.datasource.inventory.jdbc.url=jdbc:h2:mem:inventory
quarkus.datasource.inventory.jdbc.max-size=12
You need to add @ActivateRequestContext
on the process()
method.
This is wrong:
@ApplicationScoped
@DataSource("db03")
public class QuoteAuditRepository implements PanacheRepository<QuoteAudit> {
The @DataSource
annotation on a repository does not make sense, because a repository is bound to a persistence unit, not to a datasource. Depending on the scenario, a persistence unit could use multiple datasources.
@PersistenceUnit
might have made sense, but that's not how it works: you're supposed to assign entity types to persistence units.
Either add a package-info.java
in your entity type's package:
@PersistenceUnit("db03")
package .acme.model.db03;
import io.quarkus.hibernate.orm.PersistenceUnit;
Or point to that package it in your configuration (application.properties
):
quarkus.hibernate-orm.db03.packages=.acme.model.db03
See https://quarkus.io/guides/hibernate-orm#multiple-persistence-units-attaching-model-classes
spring-boot
at here, have you got a mistake? – Vy Do Commented Apr 2 at 2:10