當前位置:編程學習大全網 - 源碼下載 - TopLink源代碼分析 JPA中的如何實現多數據庫平臺的訪問

TopLink源代碼分析 JPA中的如何實現多數據庫平臺的訪問

[問題的由來]壹般來說,通用的商業產品都可以訪問多數據庫。TopLink也不例外,其中的JPA號稱支持上十種數據庫平臺,而且還可以動態增加。那麽具體的技術細節是怎麽樣呢?怎麽實現的呢?我們來看壹看TopLink的源代碼是如何實現的。 [源代碼分析]第壹步:在登陸訪問數據庫時,首先執行下面的函數publicvoid loginAndDetectDatasource() throws DatabaseException { preConnectDatasource(); Connection conn = null; try{ conn = (Connection)getReadLogin().connectToDatasource(null); getLogin().setPlatformClassName(DBPlatformHelper.getDBPlatform(conn.getMetaData().getDatabaseProductName(), getSessionLog())); }catch (SQLException ex){DatabaseException dbEx = DatabaseException.errorRetrieveDbMetadataThroughJDBCConnection(); // Typically exception would occur if user did not provide correct connection// parameters. The root cause of exception should be propogated up dbEx.initCause(ex); throw dbEx; }finally{ if (conn != null){ try{ conn.close(); }catch (SQLException ex){ DatabaseException dbEx = DatabaseException.errorRetrieveDbMetadataThroughJDBCConnection(); // Typically exception would occur if user did not provide correct connection // parameters. The root cause of exception should be propogated up dbEx.initCause(ex); throw dbEx; } } } connect(); postConnectDatasource(); } 第二步:在上面的函數中,有壹個特別重要的函數,大家註意看,叫做DBPlatformHelper.getDBPlatform()下面我們來看看這個函數到底做了什麽?publicstatic String getDBPlatform(String vendorName, SessionLog logger) { initializeNameToVendorPlatform(logger); String detectedDbPlatform = null; if(vendorName != null) { detectedDbPlatform = matchVendorNameInProperties(vendorName, _nameToVendorPlatform, logger); } if (logger.shouldLog(SessionLog.FINE) ) { logger.log(SessionLog.FINE, "dbPlaformHelper_detectedVendorPlatform", detectedDbPlatform ); // NOI18N } if (detectedDbPlatform == null) { if(logger.shouldLog(SessionLog.INFO)) { logger.log(SessionLog.INFO, "dbPlaformHelper_defaultingPlatform", vendorName, DEFAULTPLATFORM); // NOI18N } detectedDbPlatform = DEFAULTPLATFORM; } return detectedDbPlatform; }大家註意看,上面這個函數的第壹行是:initializeNameToVendorPlatform(logger);這個函數是幹什麽的?是不是就是訪問多數據庫強大功能的秘密呢?我們再往下面看。

  • 上一篇:物探星空(探索宇宙奧秘)
  • 下一篇:2022支股票做波段的技巧
  • copyright 2024編程學習大全網