2012-10-29 33 views
0


我使用spring和hibernate在多個數據庫服務器上工作保存,使用2個數據源配置同時更新。 我正在使用如何使用spring和hibernate處理多個數據庫連接的錯誤/異常?

org.springframework.jdbc.datasource.DriverManagerDataSource 

創建數據源的類。 如果兩個數據庫服務器都啓動/運行,那麼它工作正常。 如果我關閉了其中一臺數據庫服務器並試圖捕獲異常,那麼它不會進入捕獲塊 ,並且在我的Web瀏覽器上顯示堆棧跟蹤錯誤。

Stack Trace: 
 
    ** BEGIN NESTED EXCEPTION ** 
    java.net.ConnectException 
    MESSAGE: Connection refused: connect 
STACKTRACE: 

java.net.ConnectException: Connection refused: connect 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) 
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 
    at java.net.Socket.connect(Socket.java:525) 
    at java.net.Socket.connect(Socket.java:475) 
    at java.net.Socket.(Socket.java:372) 
    at java.net.Socket.(Socket.java:215) 
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) 
    at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) 
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) 
    at com.mysql.jdbc.Connection.(Connection.java:1555) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
    at java.sql.DriverManager.getConnection(DriverManager.java:582) 
    at java.sql.DriverManager.getConnection(DriverManager.java:154) 
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173) 
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164) 
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149) 
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119) 
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82) 
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) 
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) 
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116) 
    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:54) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666) 
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) 
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) 
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) 
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) 
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) 
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562) 
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550) 
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546) 
    at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686) 
    at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1) 
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) 
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
    at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683) 
    at com.service.RegistrationServiceImpl.add(RegistrationServiceImpl.java:56) 
    at com.controller.RegistrationController.onSubmit(RegistrationController.java:48) 
    at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272) 
    at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268) 
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153) 
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:536) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879) 
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) 
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) 
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) 
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) 
    at java.lang.Thread.run(Thread.java:619) 


** END NESTED EXCEPTION * 

我如何處理這個異常。 我想在我的GUI中顯示我的一個數據庫服務器關閉如果我的數據庫服務器有任何故障。

請幫忙。

回答

1

我覺得有兩種選擇:

  • 列表項,您應該抓住在服務層異常(例如像在RegistrationServiceImpl),但你必須要做到這一點在所有的服務類。
  • 創建一個Servlet篩選器,捕獲此特定異常並將用戶重定向到顯示類似「Sorry,the database is down」之類的頁面。
1

春天有@ExceptionHandler註釋。你可以在你的一些常見的類中創建一個處理程序方法。每當拋出一個異常,你可以方便地顯示消息你想依賴於異常拋出這樣的事情

@Component 
public class BaseController { 

@ExceptionHandler(Throwable.class) 
public ModelAndView handleException(Throwable throwable, HttpServletRequest request){ 

    ModelAndView view = null; 
    if (throwable instanceof ConnectException){ 
     view = //set message and return view; 
    }else if (throwable instanceof someOtherException) { 
     //do something else; 

//handle as many exceptions as you want 
    }else { 
      //do something by default for other unhandled/generic exceptions 
      } 

    return view; 
} 

} 

documentation詳細解釋了所支持的返回類型。

您也可以使處理程序來處理只有你想要的那種異常的,是這樣的:

@ExceptionHandler(ConnectException.class) 
public ModelAndView handleException(ConnectException ex, HttpServletRequest request){ 
// handle it 
} 

您可以檢查此example這是類似的路線。

我希望這會有所幫助。歡呼:)

相關問題