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

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

问题描述

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




  • 我们如何动态更改db.pass属性? Play.application()。configuration()看起来
    是不可变的,因为Play 2.1。 (或者我们至少不知道mutator)

  • 如果我们能够更改db.pass,我们如何重新加载数据库配置,使JPA.em()返回一个EntityManager使用新密码?



我们试图避免的是使用
EntityManagerFactory重新创建EntityManager。我们希望继续让Play在
中管理JPA助手类。



背景 / p>

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

 #! bash 
stty -echo
read -pPassword:PASS
stty echo
curl -k https://127.0.0.1:8443/someUrl/pwd --data应用程序接收到此数据,然后重新创建Hibernate
SessionFactory,然后重新创建Hibernate
SessionFactory。







$ b $ p $我们的新的Play应用程序将需要做某事
类似。

解决方案

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


  1. 如果配置表明production.level是PROD

  2. 从stdin中读取密码

  3. 通过将旧的配置转换为地图并使用ConfigFactory.parseMap构建新配置,使用新参数

  4. 创建新配置返回super.onLoadConfig

但是,这仍然没有解决重新加载数据库配置的问题。最后,我的同事创造了一个Play! plugin



更新

这些插件基本上是一些JPA类的副本,具有重新加载配置属性的映射的能力。

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


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

  • How can we dynamically change the db.pass property? Play.application().configuration() seems to be immutable as of Play 2.1. (or we're at least not aware of the mutators)
  • If we are able to change db.pass, how can we reload the DB configuration so that JPA.em() returns an EntityManager using the new password?

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.

Background

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" 

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

解决方案

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. If configuration indicates that production.level is PROD
  2. Read the password from stdin
  3. Create a new configuration by converting the old one to a map and building a new one with ConfigFactory.parseMap, with the new parameter as well
  4. Return super.onLoadConfig

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.

Update

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天全站免登陆