英文:
Connecting to DB via Quarkus
问题
我最近开始了一个新的Java应用程序,并尝试使用Quarkus框架。我有一个运行在Docker容器上的DB(MySQL),已经暴露了端口3306,我确认可以通过MySQL Workbench访问它。
阅读这个在线文档后,我按照设置JDBC连接的步骤进行了操作。我将agroal
和jdbc-mysql
扩展添加到了Quarkus,并在我的application.properties
中包含了以下内容:
quarkus.datasource.db-kind=mysql
quarkus.datasource.username=<username>
quarkus.datasource.password=<password>
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/messanger?useSSL=false&autoreconnect=true
quarkus.datasource.jdbc.acquisition-timeout=PT1M
quarkus.datasource.jdbc.min-size=1
quarkus.datasource.jdbc.max-size=16
然后,我创建了一个类来在应用程序首次运行时管理和设置数据库:
@ApplicationScoped
public class DatabaseManager {
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseManager.class);
private final Jdbi jdbi;
private final DataSource dataSource;
private boolean initialized;
@Inject
public DatabaseManager(DataSource dataSource) {
this.dataSource = dataSource;
jdbi = Jdbi.create(dataSource);
jdbi.installPlugin(new SqlObjectPlugin());
}
/**
* SoR DB的延迟初始化。
*/
@PostConstruct
public void init() {
LOGGER.info("Lazy init of {}", getClass().getSimpleName());
jdbi.getConfig(SqlStatements.class).setQueryTimeout(30);
Flyway flyway = Flyway.configure()
.locations("classpath:sql")
.baselineOnMigrate(true)
.dataSource(dataSource)
.load();
flyway.migrate();
if (LOGGER.isDebugEnabled()) {
setSqlLogging();
}
setInitialized(true);
LOGGER.info("Initialization complete");
}
}
我只包含了到PostConstruct
的部分,因为那是最重要的部分。但是当我运行应用程序时,我附加了一个调试器,并在PostConstruct
方法中设置了断点,它从未被命中!!!我不明白这一点,但后来我尝试查看Quarkus启动时的控制台输出,它只显示:
2020-07-31 07:34:37,932 WARN [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
2020-07-31 07:34:40,247 INFO [io.agr.pool] (Quarkus Main Thread) Datasource '<default>': Initial size smaller than min. Connections will be created when necessary
2020-07-31 07:34:40,495 INFO [io.quarkus] (Quarkus Main Thread) platform 1.0.0-SNAPSHOT on JVM (powered by Quarkus 1.6.1.Final) started in 2.616s. Listening on: http://0.0.0.0:8082
2020-07-31 07:34:40,497 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2020-07-31 07:34:40,497 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cache, cdi, flyway, jdbc-mysql, mutiny, narayana-jta, resteasy, smallrye-context-propagation, smallrye-fault-tolerance, smallrye-health]
尽管我按照上面的配置文档(上面的链接)进行了配置,但数据源被设置为default
,而我期望数据源应该是mysql
。
我是否漏掉了一些明显的东西?
英文:
I have recently started a new Java application and I am trying to use the Quarkus framework. I have a DB (MySQL) running on a docker container that has exposed the port 3306, I confirmed I can access this through MySQL Workbench.
Reading this doc online, I followed the steps to setup a JDBC connection. I added the agroal
and jdbc-mysql
extensions to Quarkus and have the following in my application.properties
quarkus.datasource.db-kind=mysql
quarkus.datasource.username=<username>
quarkus.datasource.password=<password>
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/messanger?useSSL=false&autoreconnect=true
quarkus.datasource.jdbc.acquisition-timeout=PT1M
quarkus.datasource.jdbc.min-size=1
quarkus.datasource.jdbc.max-size=16
I then created a class to manage and setup the DB at the start when the application first runs;
@ApplicationScoped
public class DatabaseManager {
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseManager.class);
private final Jdbi jdbi;
private final DataSource dataSource;
private boolean initialized;
@Inject
public DatabaseManager(DataSource dataSource) {
this.dataSource = dataSource;
jdbi = Jdbi.create(dataSource);
jdbi.installPlugin(new SqlObjectPlugin());
}
/**
* Lazy initialization of SoR DB.
*/
@PostConstruct
public void init() {
LOGGER.info("Lazy init of {}", getClass().getSimpleName());
jdbi.getConfig(SqlStatements.class).setQueryTimeout(30);
Flyway flyway = Flyway.configure()
.locations("classpath:sql")
.baselineOnMigrate(true)
.dataSource(dataSource)
.load();
flyway.migrate();
if (LOGGER.isDebugEnabled()) {
setSqlLogging();
}
setInitialized(true);
LOGGER.info("Initialization complete");
}
}
I have only included upto the PostConstruct
as that's the most important parts. But when I have the application running, I attach a debugger and put a breakpoint in the PostConstruct
method, it never gets hit!!! I don't understand this as but then I tried looking at the console output of Quarkus as it's starting up and all it shows is;
2020-07-31 07:34:37,932 WARN [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
2020-07-31 07:34:40,247 INFO [io.agr.pool] (Quarkus Main Thread) Datasource '<default>': Initial size smaller than min. Connections will be created when necessary
2020-07-31 07:34:40,495 INFO [io.quarkus] (Quarkus Main Thread) platform 1.0.0-SNAPSHOT on JVM (powered by Quarkus 1.6.1.Final) started in 2.616s. Listening on: http://0.0.0.0:8082
2020-07-31 07:34:40,497 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2020-07-31 07:34:40,497 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cache, cdi, flyway, jdbc-mysql, mutiny, narayana-jta, resteasy, smallrye-context-propagation, smallrye-fault-tolerance, smallrye-health]
The Datasource is set to default
even though I followed the config doc (link above) and was expecting the datasource to be mysql
.
Is there something obvious I am missing here?
答案1
得分: 1
以下是翻译好的部分:
Beans are lazilly created, and your @PostConstruct
methods will be called at bean instanciation time.
这意味着你的 @PostConstruct
方法将在 bean 实例化时被调用。
This means that your @PostConstruct
methods will be called the first time you use your DatabaseManager
bean, not at application start time.
这意味着你的 @PostConstruct
方法将在你第一次使用 DatabaseManager
bean 时被调用,而不是在应用程序启动时。
If you want a method to run when the application sart, you need to use our lyfecycle events: https://quarkus.io/guides/lifecycle#listening-for-startup-and-shutdown-events
如果你想在应用程序启动时运行一个方法,你需要使用我们的生命周期事件:https://quarkus.io/guides/lifecycle#listening-for-startup-and-shutdown-events
Be aware also that unused beans are remove by Arc (the CDI implementation of Quarkus) this can also be the cause of an initialization method not being called, more informations here: https://quarkus.io/guides/cdi-reference#remove_unused_beans
还要注意,未使用的 bean 会被 Arc(Quarkus 的 CDI 实现)移除,这也可能是初始化方法未被调用的原因,更多信息请参考:https://quarkus.io/guides/cdi-reference#remove_unused_beans
英文:
Beans are lazilly created, and your @PostConstruct
methods will be called at bean instanciation time.
This means that your @PostConstruct
methods will be called the first time you use your DatabaseManager
bean, not at application start time.
If you want a method to run when the application sart, you need to use our lyfecycle events: https://quarkus.io/guides/lifecycle#listening-for-startup-and-shutdown-events
Be aware also that unused beans are remove by Arc (the CDI implementation of Quarkus) this can also be the cause of an initialization method not being called, more informations here: https://quarkus.io/guides/cdi-reference#remove_unused_beans
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论