[英]Spring + R2DBC: How to close DatabaseClient and TransactionalOperator?
I can create DatabaseClient and TransactionalOperator in my Spring Boot app using: 我可以使用以下命令在Spring Boot应用程序中创建DatabaseClient和TransactionalOperator:
@Autowired public App(ConnectionFactory factory) {
DatabaseClient dc = DatabaseClient.create(factory);
TransactionalOperator to = TransactionalOperator.create(new R2dbcTransactionManager(factory));
}
But how do I close the DatabaseClient and TransactionalOperator and free the connections/resources? 但是,如何关闭DatabaseClient和TransactionalOperator并释放连接/资源?
Is this the best way to create the database connection for R2DBC in a Spring Boot app? 这是在Spring Boot应用程序中为R2DBC创建数据库连接的最佳方法吗?
TL;DR: There's nothing to close unless you use a connection pool . TL; DR:除非您使用连接池,否则没有什么可以关闭的。
Spring Data R2DBC's DatabaseClient
allocates a connection per invoked operation. Spring Data R2DBC的
DatabaseClient
为每个调用的操作分配一个连接。 It's similar to JdbcTemplate
that asks its DataSource
to create a Connection
for each method that you invoke. 它类似于
JdbcTemplate
,它要求其DataSource
为您调用的每个方法创建一个Connection
。 Once a method call is finished, it releases (closes) the connection. 方法调用完成后,将释放(关闭)连接。
Same applies for DatabaseClient
. 同样适用于
DatabaseClient
。 Once an operation finishes, the underlying connection is closed. 操作完成后,基础连接将关闭。
When using TransactionalOperator
or @Transactional
there's a slight change in the behavior but similar to JDBC: R2DBC Connections are bound during an active transaction to the transaction itself which makes DatabaseClient
to reuse the same connection until the transaction completes. 使用
TransactionalOperator
或@Transactional
时,行为上会有一些细微变化,但与JDBC类似:R2DBC连接在活动事务期间绑定到事务本身,这使DatabaseClient
可以重复使用同一连接,直到事务完成为止。 After a transaction is completed, the bound connection is released. 事务完成后,绑定的连接被释放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.