Preemptively and gracefully check that org.hibernate.Session is still connected (via c3p0)

匆匆过客 提交于 2019-12-04 06:16:07

问题


I am working in a multisharded database environment in which downtime of a shard is not a fatal occurrence. When the application starts, all the shard info is loaded in a cache and respective Session objects loaded, however, it is expected that during the application uptime some of the shards may go down, in which case the application fails over to one of the remaining shards. It is almost a standard use case scenario.

In some cases, I need to do a cross-shard scan and the way I do it is iterate through the map of sessions described above and retrieve data. The problem comes when I have a Hibernate session to a database that went down in the meantime since the session was open. I would like to have a preemptive way of checking that the DB is down without attempting a transaction and getting org.hibernate.exception.GenericJDBCException, which is the way I am finding it out now but that is bad cause it is a runtime exception, among all the other evils. I also tried Session.isOpen() and Session.isConnected()but both methods return true even when the database is down.

Is there a way to check connectivity of a Hibernate Session preemptively and gracefully? Something like send a ping to the DB. Also it may be worthwhile knowing that we are using c3p0 for connection pooling. Is there a way I can configure it to tell Hibernate that a connection is down and close/disconnect the associated session?


回答1:


I think those only work is you explicity called the method session.close()



来源:https://stackoverflow.com/questions/19645509/preemptively-and-gracefully-check-that-org-hibernate-session-is-still-connected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!