我的應用程序基於hibernate從MySQL服務器獲取數據。這個mysql服務器被複制到另一個mysql服務器實例。今天,由於主數據庫服務器在沒有任何通知的情況下停機,我得到了停機時間。爲了避免將來出現意外問題,我打算添加一項功能,以便系統在發現主服務器失敗時連接到輔助數據庫。如何在休眠時使用數據庫作爲備份/故障轉移?
是否存在一種方法,我可以利用hibernate庫來啓用此功能?
我的應用程序基於hibernate從MySQL服務器獲取數據。這個mysql服務器被複制到另一個mysql服務器實例。今天,由於主數據庫服務器在沒有任何通知的情況下停機,我得到了停機時間。爲了避免將來出現意外問題,我打算添加一項功能,以便系統在發現主服務器失敗時連接到輔助數據庫。如何在休眠時使用數據庫作爲備份/故障轉移?
是否存在一種方法,我可以利用hibernate庫來啓用此功能?
我已經創建了以下課程後得到一個想法形式this thread 它似乎運作良好。我不知道這是否是一個好方法。
package com.vsd.server.hibernate;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.connection.C3P0ConnectionProvider;
public class FailoverConnectionProvider extends C3P0ConnectionProvider {
String password;
String username;
String connectionString;
String failover_connstring;
@Override
public Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(connectionString, username, password);
} catch (Exception ex) {
conn = DriverManager.getConnection(failover_connstring, username, password);
}
if(conn == null){
throw new IllegalStateException("Database connection was not initialized");
}
return conn;
}
@Override
public void configure(Properties properties) throws HibernateException {
failover_connstring = properties.getProperty("hibernate.connection.failover.url");
if (StringUtils.isBlank(connectionString)
&& StringUtils.isBlank(failover_connstring)
&& StringUtils.isBlank(username)
&& StringUtils.isBlank(password)) {
throw new IllegalStateException("Unable to initialize connection provider for hibernate");
}
}
}
今天,我得到了停機時間作爲主數據庫服務器已經走了下來,恕不另行通知。
那麼,這是你應該修復的第一件事(通過適當的監測)。
(...)是否存在一種方式,我可以利用hibernate庫來啓用此功能?
據我所知,Hibernate並沒有真正爲此提供任何設施。就我個人而言,我將研究MySQL及其JDBC驅動程序的故障切換支持。我不能提供一個非常具體的答案,因爲我還沒有實現這個與MySQL,但這裏有一些指點:
,截至提到上面最新鏈接的底部(也在this comment):
您可能還想調查負載平衡JDBC池(lbpol)工具,該工具提供標準JDBC驅動程序的包裝,並使您能夠使用數據庫連接池,其中包括檢查系統故障和不均勻的負載分佈。有關更多信息,請參見Load Balancing JDBC Pool (lbpool)。
您如何看待我已添加到應用程序中的程序? – 2010-10-23 01:40:00
你可以建議一些監測過程,以進行適當的監測 – 2010-10-23 02:00:20
@Vijay這裏有一些:[Zenoss](http://community.zenoss.org/index.jspa),[Centreon](http://www.centreon.com /),[Opsview](http://www.opsview.com/downloads/download-opsview-community) – 2010-10-23 02:13:57
您可以嘗試添加故障轉移功能dataSource級別(任何類型的數據庫)。這方面很少有圖書館。例如:
請在這裏添加細節。只是提供鏈接作爲答案令人不悅。 – slm 2013-02-03 23:49:36
此鏈接可能是另一種方式。但是,您目前的實施非常基礎(無連接池),並且IMO不適合用於生產。 – 2010-10-23 01:39:26