简体   繁体   English

如何处理数据库崩溃(Glassfish / MySQL)?

[英]How to handle database crashes (Glassfish/MySQL)?

I have a 3-tiered application with Glassfish 3.1.2.2, MySQL database and a Swing client application. 我有一个使用Glassfish 3.1.2.2,MySQL数据库和Swing客户端应用程序的3层应用程序。 Is it somehow possible to handle a database server crash? 是否有可能处理数据库服务器崩溃?

I tried to stop the MySQL service during my application is running. 我试图在我的应用程序运行期间停止MySQL服务。 Then I get javax.ejb.EJBAccessException exceptions everytime I try to access the database through one of my facade session beans. 每次我尝试通过我的一个Facade会话bean访问数据库时,我都会得到javax.ejb.EJBAccessException异常。

I want to notify the user that the database is currently down. 我想通知用户数据库当前已关闭。 furthermore i want to set my application in kind of a "sleep-mode" until the database is up and running again. 此外,我想将我的应用程序设置为“睡眠模式”,直到数据库启动并再次运行。

What would be a good (and possibly easy) approach to handle such a scenario? 处理这种情况会有什么好处(也可能是简单的)方法?

Thanks for your help in advance! 感谢您的帮助!

A) Ensure you configure Glassfish Connection Pool for automatic recovery/reconnection A)确保配置Glassfish连接池以进行自动恢复/重新连接

In the Glassfish JDBC connection pool configuration, set the values for: 在Glassfish JDBC连接池配置中,设置以下值:

  • Attributes: is-connection-validation-required, validate-atmost-once-period-in-seconds, connection-creation-retry-attempts, connection-validation-method, connection-creation-retry-interval-in-seconds, ping 属性: is-connection-validation-required, validate-atmost-once-period-in-seconds, connection-creation-retry-attempts, connection-validation-method, connection-creation-retry-interval-in-seconds, ping

Glassfish Configuration jdbc-connection-pool Properties Glassfish配置jdbc-connection-pool属性
Glassfish Admin - create-jdbc-connection-pool subcommand Glassfish管理员 - create-jdbc-connection-pool子命令

Steps: 脚步:

  1. Assuming Glassfish is running (eg start server in Netbeans Services Tab, by opening Servers and right-clicking Glassfish), then you should have the Domain Admin Server running 假设Glassfish正在运行(例如,在Netbeans服务选项卡中启动服务器,打开服务器并右键单击Glassfish),那么您应该运行Domain Admin Server
  2. Open Admin Console in web browser: http://localhost:4848 (or use whatever port you specified during install. 在Web浏览器中打开管理控制台: http://localhost:4848 (或使用您在安装期间指定的任何端口。
  3. On left, under Common Tasks menu, open Resources -> JDBC and click on JDBC Connection Pools 在左侧的Common Tasks菜单下,打开Resources - > JDBC并单击JDBC Connection Pools
  4. Click on your connection pool POOL NAME (or create a new one by clicking New button) 单击您的连接池POOL NAME(或通过单击“新建”按钮创建一个新的)
  5. Select Advanced Tab & Enter: 选择高级选项卡并输入:
  6. Validate At Most Once: (eg 60) in seconds 最多验证一次:(例如60)秒
  7. Creation Retry Attempts: (eg 3) 创作重试尝试:(例如3)
  8. Retry Interval: (eg 10) in seconds 重试间隔:(例如10)秒
  9. Connection Validation: (Tick) Required 连接验证:(勾选)必需
  10. Validation Method: (eg auto-commit) 验证方法:(例如自动提交)
  11. Other Advanced Properties as Desired 其他所需的高级属性
  12. Select General Tab & Enter: 选择常规选项卡并输入:
  13. Ping (Tick) Ping(勾选)
  14. Other General Properties as Desired 其他一般属性如期望

B) Implement Application Error Handling / DB Monitoring & Alerts B)实现应用程序错误处理/数据库监视和警报

  1. Mandatory: trap "infrastructure-level" fatal user errors and alert support staff. 强制性:陷阱“基础架构级”致命用户错误和警报支持人员。

    Create a simple JMX class to send a notification & call its method when a fatal error occurs JMX MBean Class which sends notifications . 创建一个简单的JMX类,以便在发生致命错误时发送通知并调用其方法发送通知的JMX MBean Class You can use a JMX monitor console to observe state of servers - some of these consoles send email alerts (eg JManage and RHQ) and there are bridges for access from HTTP/AJax or other languages (eg Jolokia can use javascript/perl/java API to access JMX notifications). 您可以使用JMX监视器控制台来观察服务器状态 - 其中一些控制台发送电子邮件警报(例如JManage和RHQ),并且有从HTTP / AJax或其他语言访问的桥接器(例如Jolokia可以使用javascript / perl / java API访问JMX通知)。

    Use Google Calendar API to send the error msg to support staff's google calendar (1 or 2 mins into future). 使用Google Calendar API将错误消息发送给支持员工的Google日历(将来1或2分钟)。 Then configure google calendar to send email/sms notifications - will give error alert straight to support staff in near-realtime. 然后配置谷歌日历以发送电子邮件/短信通知 - 将直接提供错误警报,以近乎实时的支持人员。 This is subject to Google usage restrictions (a courtesy limit of 10,000 queries per day, so make sure your apps not super buggy and use logic to limit the number of messages sent in an hour/day/week) 这受到Google使用限制(每天10,000次查询的礼貌限制,因此请确保您的应用不是超级错误,并使用逻辑来限制每小时/每天/每周发送的邮件数量)

  2. Desirable: monitor DB (& probably app server) and alert support staff of outages 理想的:监控数据库(可能是应用服务器)并提醒支持人员中断

    • Zabbix open source has inbuilt mySQL monitoring and alerting - is lightweight but requires setup & configuration Zabbix开源内置mySQL监控和警报 - 重量轻但需要设置和配置
    • Hyperic open source has extension plug-ins for mySQL monitoring and inbuilt alerting - is heavy-weight, can be complex to setup & configure Hyperic开源有用于mySQL监控和内置警报的扩展插件 - 重量很大,设置和配置都很复杂
    • Nagois open source is the default for *nix OSes - is heavy weight, can be complex to setup and configure Nagois开源是* nix操作系统的默认设置 - 重量很大,设置和配置都很复杂

    In all cases, setup will not happen instantly - best to implement as a separate mini project & do it right - best to have support staff involved in this. 在所有情况下,设置不会立即发生 - 最好作为一个单独的迷你项目实施并做正确 - 最好让支持人员参与其中。

    If these are "out of scope", then create your own simple monitor: 如果这些“超出范围”,那么创建自己的简单监视器:

    • An EJB timer to run scheduled simple test query against your DB - if it fails send an alert (via JMX/Google Calendar/Java Mail/ SMS gateway API). 用于针对您的数据库运行计划的简单测试查询的EJB计时器 - 如果失败则发送警报(通过JMX / Google Calendar / Java Mail / SMS网关API)。 Or use Quartz open source scheduler to do the same job 或者使用Quartz开源调度程序来完成同样的工作

On it's own Connection Pool will monitor the state of the database. 在它自己的连接池将监视数据库的状态。 You could create an interceptor and pick up when db goes down. 您可以创建一个拦截器,并在db关闭时启动。 The container will try to reconnect, but you won't necessarily know when it's successful. 容器将尝试重新连接,但您不一定知道它何时成功。 Once you detect the error you'd need to set a semaphore somewhere. 一旦检测到错误,就需要在某处设置信号量。 Then you'd have to fire up a timer to periodically check the state of the db and to reset the semaphore once it comes back. 然后你必须启动一个计时器来定期检查数据库的状态,并在它返回后重置信号量。 Last, you'd have to code your app to respect the semaphore. 最后,您必须编写应用程序以尊重信号量。

This is a high level suggestion. 这是一个高级别的建议。 good luck. 祝好运。

Alternatively, you might be able to hook all the events using JMX... I'm not sure though. 或者,您可以使用JMX挂钩所有事件......但我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM