Micronaut,JPA,EntityManager:没有来自应用程序的数据源。yml给出错误';未配置事务管理器';
我正在尝试使用Micronaut+Postgresql将JPA事务管理与批插入结合起来 |Micronaut版本:1.1.3 |JVM版本:1.8.0_212 我已将Micronaut项目配置为在build.gradle中使用“Micronaut hibernate jpa”Micronaut,JPA,EntityManager:没有来自应用程序的数据源。yml给出错误';未配置事务管理器';,jpa,entitymanager,spring-transactions,micronaut,Jpa,Entitymanager,Spring Transactions,Micronaut,我正在尝试使用Micronaut+Postgresql将JPA事务管理与批插入结合起来 |Micronaut版本:1.1.3 |JVM版本:1.8.0_212 我已将Micronaut项目配置为在build.gradle中使用“Micronaut hibernate jpa” compile "io.micronaut.configuration:micronaut-hibernate-jpa" 高级方法是这样的,并且在application.yml中定义数据源时工作良好 我的界面 inter
compile "io.micronaut.configuration:micronaut-hibernate-jpa"
高级方法是这样的,并且在application.yml中定义数据源时工作良好
我的界面
interface myrepository
{
savebatch (@NotNull List<ConsumerRecord<String, MyBusinessClass>> buffer
}
如果使用application.yml中的datasource,上述代码运行良好
我的要求是:我的应用程序需要在运行时创建数据源。所以我从application.yml中删除了dagtasource和其他细节,并在运行时创建了它
对于动态方法。我正在用下面的代码片段初始化构造函数中的EntityManger,
但其抛出运行时错误为“未配置事务管理器”
HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider();
Map <String,Object> properties = new HashMap<String,Object>();
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_DRIVER, 'org.postgresql.Driver')
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_URL, 'jdbc:postgresql://...')
properties.put(org.hibernate.cfg.AvailableSettings.DIALECT, PostgreSQL10Dialect.class)
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_USER, '...')
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_PASSWORD, '...')
properties.put(org.hibernate.cfg.AvailableSettings.SHOW_SQL, false)
EntityManagerFactory entityManagerFactory = hibernatePersistenceProvider
.createContainerEntityManagerFactory(persistenceUnitInfo, properties);
def entityManager = entityManagerFactory.createEntityManager()
log.info "Setting entity manager factory "
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory((EntityManagerFactory)entityManagerFactory);
DataSource dataSource = DataSourceBuilder.create()
.driverClassName('org.postgresql.Driver')
.url('jdbc:postgresql://...')
.password('...')
.username('...')
.build()
if (dataSource!= null){
log.info "Valid datasource created"
txManager.setDataSource(dataSource);
}
context.registerSingleton(entityManager)
return entityManager
HibernatePersistenceProvider HibernatePersistenceProvider=新的HibernatePersistenceProvider();
映射属性=新的HashMap();
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_DRIVER,'org.postgresql.DRIVER')
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_URL,'JDBC:postgresql://...')
properties.put(org.hibernate.cfg.AvailableSettings.dialogue,postgresql10dialogue.class)
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_USER,“…”)
properties.put(org.hibernate.cfg.AvailableSettings.JPA_JDBC_密码“…”)
properties.put(org.hibernate.cfg.AvailableSettings.SHOW\u SQL,false)
EntityManagerFactory EntityManagerFactory=hibernatePersistenceProvider
.createContainerEntityManager工厂(persistenceUnitInfo,属性);
def entityManager=entityManagerFactory.createEntityManager()
log.info“设置实体管理器工厂”
JpaTransactionManager txManager=新的JpaTransactionManager();
txManager.setEntityManagerFactory((EntityManagerFactory)EntityManagerFactory);
DataSource DataSource=DataSourceBuilder.create()
.driverClassName('org.postgresql.Driver'))
.url('jdbc:postgresql://...')
.password(“…”)
.username(“…”)
.build()
if(数据源!=null){
log.info“已创建有效数据源”
txManager.setDataSource(数据源);
}
context.registerSingleton(entityManager)
返回实体管理器
}
跟踪了几个链接和谷歌
我不知道您为什么要手动创建实体管理器,这是不必要的,因为Micronaut已经具备为您自动设置实体管理器的功能。看 但是,如果您必须这样做,那么在注册单例时,您必须使用“default”的命名限定符注册它,因为Micronaut希望调用默认数据源,并且您还必须将数据源本身注册为bean
context.registerSingleton(dataSource, Qualifiers.byName("default"));
context.registerSingleton(entityManager, Qualifiers.byName("default"));
但是,正如我所说,这不是一种受支持或推荐的配置实体管理器工厂的方法。谢谢你,格雷姆,
@PersistentContext EntityManager
将自动绑定到application.yml中定义的数据源。但是,我的要求是在运行时创建数据库连接。因为我的业务需求是动态创建“租户”,所以将创建一个数据库,我希望在那个时候使用datasource。流程是:在应用程序启动时读取租户信息,通过progrm创建数据源,然后连接到数据。租户的信息将存储在一个单独的数据库中,具有不同的凭据,因此我避免使用application.yml。
context.registerSingleton(dataSource, Qualifiers.byName("default"));
context.registerSingleton(entityManager, Qualifiers.byName("default"));