[英]Catch a configuration error in Play! Framework 2.0.4
My Play! 我的玩! 2.0.4 web application currently connects to several RDS MySQL databases, with the configuration file as such:
2.0.4 Web应用程序当前使用以下配置文件连接到多个RDS MySQL数据库:
# Africa
db.afr.url="jdbc:mysql://<africa-server>:3306/users"
db.afr.driver=com.mysql.jdbc.Driver
db.afr.user=user1
db.afr.password=****
db.afr.logStatements=true
# Europe
db.eur.url="jdbc:mysql://<europe-server>:3306/users"
db.eur.driver=com.mysql.jdbc.Driver
db.eur.user=user1
db.eur.password=****
db.eur.logStatements=true
This works perfectly, while all the databases are running. 当所有数据库都在运行时,这可以完美运行。 However, if one of the databases is down (for whatever reason) the entire application fails, throwing a Configuration error (Cannot connect to databases [afr]).
但是,如果数据库之一关闭(由于某种原因),则整个应用程序将失败,并抛出配置错误(无法连接到数据库[afr])。
How would I catch the Configuration Error on startup to catch and ignore/log these messages instead of killing the server completely? 我如何在启动时捕获配置错误以捕获和忽略/记录这些消息,而不是完全杀死服务器? I've looked into using Global.java's onError() override but I'm having no luck there.
我已经研究过使用Global.java的onError()重写,但是我没有运气。
Thanks, David 谢谢大卫
Here's what I ended up doing. 这就是我最终要做的。 Not a great solution but an OK work-around maybe.
这不是一个很好的解决方案,但可以解决。
Copy the contents of play.api.db.BoneCPPlugin to your own file SafeDBPlugin.scala with class name SafeDBPlugin. 将play.api.db.BoneCPPlugin的内容复制到您自己的文件名为SafeDBPlugin的文件SafeDBPlugin.scala中。 It needs to be in the package play.api.db (though you can put the file anywhere you want in your code base as usual).
它必须位于包play.api.db中(尽管您可以照常将文件放置在代码库中所需的任何位置)。 The important part is changing the line in method onStartup() that reports the config error and changing that to say logger.warn().
重要的是更改方法onStartup()中报告配置错误的行,并将其更改为logger.warn()。
Disable the built-in dbplugin (BoneCPPlugin) by adding the following line to your application.conf: 通过将以下行添加到application.conf中,禁用内置的dbplugin(BoneCPPlugin):
dbplugin=disabled
Add your new plugin by adding the following line to play.plugins: 通过在play.plugins中添加以下行来添加新插件:
600:play.api.db.SafeDBPlugin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.