如何检查 JPA/hibernate 数据库是否已启用二级缓存
我有一个使用 Hibernate/JPA 连接到数据库的 JSP/Spring 应用程序。我有一个外部程序每 5 分钟检查一次 Web 服务器是否启动。
该程序调用特定的 URL 来检查 Web 服务器是否仍在运行。服务器返回“SUCCESS”。显然,如果服务器现在在,则不会返回任何内容。请求超时并发出警报,通知系统管理员出现问题...
我想在此过程中添加另一层:如果数据库服务器关闭,我希望服务器返回“错误”。有没有办法使用 Hibernate 检查数据库服务器是否正常运行?
我要做的就是拿起一个物体并尝试保存它。这会起作用,但我认为这对于我想要的来说可能太多了。我还可以从数据库读取(加载)一个对象。但由于我们对所有对象使用二级缓存,因此将从缓存加载对象,而不是从数据库加载。
我正在寻找的是这样的东西:
HibernateUtils.checkDatabase()
Hibernate 中是否存在这样的功能?
I have a JSP/Spring application using Hibernate/JPA connected to a database. I have an external program that check if the web server is up every 5 minutes.
The program call a specific URL to check if the web server is still running. The server returns "SUCCESS". Obviously if the server is now, nothing is returned. The request timesout and an alert is raised to inform the sysadmin that something is wrong...
I would like to add another layer to this process: I would like the server to return "ERROR" if the database server is down. Is there a way using Hibernate to check if the database server is alive and well?
What I tought to do was to take an object and try to save it. This would work, but I think it's probably too much for what I want. I could also read(load) an object from the database. But since we use second-level caching for all our objects, the object will be loaded from the cache, and not the database.
What I'm looking for is something like:
HibernateUtils.checkDatabase()
Does such a function exist in Hibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用本机查询,例如,
这应该强制休眠使用池中的连接。如果数据库关闭,我不知道 hibernate 会返回什么错误,因为它无法从池中获取连接。如果数据库已启动,您将获得查询结果。
You could use a native query, e.g.
That should force hibernate to use a connection from the pool. If the db is down, I don't know exactly what will be the error returned by hibernate given that it can't get the connection from the pool. If the db is up, you get the result of your query.