如何在运行时更改 Play 2 Framework DB 配置? [英] How to change Play 2 Framework DB configuration at runtime?

查看:23
本文介绍了如何在运行时更改 Play 2 Framework DB 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Play 2.1.1 及其内置的 JPA 集成 (JPA.em()等等).

We are using Play 2.1.1 and its built-in JPA integration (JPA.em() etc).

  • 我们如何动态更改 db.pass 属性?Play.application().configuration() 似乎从 Play 2.1 开始是不可变的.(或者我们至少不知道突变体)
  • 如果我们能够更改 db.pass,我们如何重新加载数据库配置,以便 JPA.em() 使用新密码返回一个 EntityManager?

我们试图避免的是必须使用以下方法重新创建 EntityManager实体管理器工厂.我们希望继续让 Play 管理JPA 助手类.

What we are trying to avoid is having to recreate the EntityManager using EntityManagerFactory. We want to continue to let Play manage that in the JPA helper class.

背景

系统具有本地运行的默认数据库配置.当部署到服务器时,DB 密码是使用以下脚本在运行的应用程序上动态设置的:

The system has a default DB configuration for running locally. When deployed to a server, the DB password is dynamically set on the running application using the following script:

#!/bin/bash 
stty -echo 
read -p "Password: " PASS 
stty echo 
curl -k https://127.0.0.1:8443/someUrl/pwd --data "password=$PASS" 

应用程序接收此数据,然后重新创建 Hibernate会话工厂.我们的新 Play 应用程序将需要做一些事情相似的.

The application receives this data and then recreates the Hibernate SessionFactory. Our new Play app will be required to do something similar.

推荐答案

为了回答我自己的问题,首先我们通过使用以下内容覆盖 Configuration.onLoadConfig 解决了在运行时更新不可变配置的问题:

To answer my own question, at first we solved the problem of updating the immutable configuration at runtime by overriding Configuration.onLoadConfig with the following:

  1. 如果配置表明production.level是PROD
  2. 从标准输入读取密码
  3. 通过将旧配置转换为映射并使用 ConfigFactory.parseMap 构建新配置以及新参数来创建新配置
  4. 返回 super.onLoadConfig

然而,这仍然没有解决重新加载数据库配置的问题.最后,我的同事创建了一个 Play!插件,本质上是一些 JPA 类的副本,具有使用配置属性映射重新加载的附加功能.

However, this still didn't address that the problem of reloading the DB configuration. In the end, my colleague created a Play! plugin which essentially a copy of some JPA classes with the added capability of being reloaded with a Map of configuration properties.

更新

hook"是插件添加到 JPA 类的附加静态方法(例如 reloadWithProperties).此方法创建一个新的数据源,然后在 JNDI 中重新绑定.

The "hook" is the additional static method which the plugin adds to the JPA class (e.g. reloadWithProperties). This method creates a new data source which is then rebound in JNDI.

这篇关于如何在运行时更改 Play 2 Framework DB 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆