我正在开发我的第一个微型应用程序,但我在使用 ucp 配置 oracle 多个数据源时遇到了问题。
我正在按照官方教程 (https://micronaut-projects.github.io/micronaut-sql/latest/guide/( 进行操作,当我尝试执行选择时,出现错误:
io.micronaut.transaction.jdbc.exceptions.CannotGetJdbcConnectionException: No current JDBC Connection found. Consider wrapping this call in transactional boundaries.
我检查了数据源工厂和池数据源设置正确
我错过了什么?
谢谢!
micronaut:
application:
name: myapp
datasources:
first:
url: url
connectionFactoryClassName: oracle.jdbc.pool.OracleDataSource
username: user
password: password
minPoolSize: 1
maxPoolSize: 10
second:
url: url
connectionFactoryClassName: oracle.jdbc.pool.OracleDataSource
username: user
password: password
minPoolSize: 1
maxPoolSize: 10
@JdbcRepository(dialect = Dialect.ORACLE)
public abstract class MyRepository {
@Inject
@Named("first")
protected final DataSource dataSource;
protected final JdbcOperations jdbcOperations;
public MyRepository(JdbcOperations jdbcOperations, DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcOperations = jdbcOperations;
}
}
@Singleton
public class MyDao extends MyRepository {
public MyDao(JdbcOperations jdbcOperations, DataSource dataSource) {
super(jdbcOperations, dataSource);
}
@Transactional
public Long find() {
String sql = "select * from table where id = 1";
return this.jdbcOperations.prepareStatement(sql, st -> {
return 1L;
});
}
第一个数据源需要称为"默认"。 此外,当将事务与非默认数据源一起使用时,您需要将@TransactionAdvice与数据源名称一起使用,例如@TransactionalAdvice("Second")
.