此類別用於產生和取得關於工作階段(Session
物件)的資訊。若要建立執行個體,請使用 Node.js 的 require()
函式搭配驅動程式名稱,如下所示
var nosql = require("mysql-js");
ConnectionProperties
可用於擷取或設定指定工作階段的連線屬性。您可以使用 ConnectionProperties
建構函式來取得指定介面卡的完整預設連線屬性集合,如下所示,其中使用介面卡的名稱(字串)作為 nameOrProperties
的值
ConnectionProperties(nameOrProperties);
您也可以透過提供屬性名稱和值清單給新的 ConnectionProperties
物件來取代介面卡名稱,藉此建立您自己的 ConnectionProperties
物件。然後,您可以使用此物件來設定新工作階段的連線屬性,如下所示
var NdbConnectionProperties = {
"implementation" : "ndb",
"ndb_connectstring" : "localhost:1186",
"database" : "test",
"mysql_user" : "root",
"ndb_connect_retries" : 4,
"ndb_connect_delay" : 5,
"ndb_connect_verbose" : 0,
"linger_on_close_msec": 500,
"use_ndb_async_api" : false,
"ndb_session_pool_min" : 4,
"ndb_session_pool_max" : 100,
};
var sharePath = '/usr/local/mysql/share/nodejs'; // path to share/nodejs
var nosql = require(sharePath);
var dbProperties = nosql.ConnectionProperties(NdbConnectionProperties);
也可以取得具有介面卡預設連線屬性的物件,然後您可以更新選定的屬性數,接著使用修改過的物件來設定工作階段的連線屬性,如下所示
var sharePath = '/usr/local/mysql/share/nodejs'; // path to share/nodejs
var spi = require(sharePath + "/Adapter/impl/SPI"); // under share/nodejs
var serviceProvider = spi.getDBServiceProvider('ndb');
var NdbConnectionProperties = serviceProvider.getDefaultConnectionProperties();
NdbConnectionProperties.mysql_user = 'nodejs_user';
NdbConnectionProperties.database = 'my_nodejs_db';
var dbProperties = nosql.ConnectionProperties(NdbConnectionProperties);
ConnectionProperties
物件包含下列屬性
implementation
:對於使用 NDB Cluster 的 Node.js 應用程式,這永遠是 “ndb”。ndb_connectstring
:NDB Cluster 連線字串,用於連線至管理伺服器。database
:要使用的 MySQL 資料庫名稱。mysql_user
:MySQL 使用者名稱。ndb_connect_retries
:在逾時前重試失敗連線的次數;使用小於 0 的數字可使其持續嘗試連線而永不停止。ndb_connect_delay
:連線重試之間的間隔(以秒為單位)。ndb_connect_verbose
:1 或 0;1 會在連線期間啟用額外的控制台輸出。linger_on_close_msec
:當用戶端關閉DBConnectionPool
時,基礎連線會保持開啟狀態,持續此毫秒數,以防其他用戶端嘗試重複使用它。use_ndb_async_api
:如果為 true,則會使用非同步呼叫來執行某些作業,以改善並行性。如果為 false,則傳輸中的作業數量會限制為每個工作執行緒一個。ndb_session_pool_min
:每個NdbConnectionPool
的DBSession
物件的最小數量。-
ndb_session_pool_max
:每個NdbConnectionPool
的DBSession
物件的最大數量。每個
NdbConnectionPool
維護DBSession
物件的集區,以及它們的基礎Ndb
物件。此參數與ndb_session_pool_min
一起設定該集區大小的準則。
TableMapping
建構函式也以頂層函式的形式可見。您可以依名稱取得對應,或使用現有的對應
TableMapping(tableName);
TableMapping(tableMapping);
openSession(properties, mappings, Function(err, Session) callback);
連線至資料來源,並在 callback
函式中取得 Session
。這相當於呼叫 connect()
(請參閱本節稍後),然後在回呼函式中傳回的 SessionFactory
上呼叫 getSession()
。
執行此方法可能會導致連線至網路上的許多其他節點,等待它們準備就緒,並向它們發出多個請求。因此,您應避免不必要地開啟新的工作階段。
properties
物件的 implementation 成員會決定 Session
的實作。
如果 mappings
未定義、為 null 或空陣列,則不會載入或驗證對應。在此情況下,任何必要的對應會在執行期間需要時載入和驗證。如果 mappings
包含字串或建構函式,則會從資料庫載入資料表(或對應資料表)的中繼資料,並根據對應的需求進行驗證。
多個資料表和建構函式可以做為陣列中的元素傳遞給 openSession()
。
connect(properties, mappings, Function(err, SessionFactory) callback);
連線至資料來源,以在 callback
函式中取得 SessionFactory
。為了取得 Session
,您必須在 此 SessionFactory
上呼叫 getSession()
,其實作由 properties
物件的 implementation 成員決定。
如果 mappings
未定義、為 null 或空陣列,則不會載入或驗證對應。在此情況下,任何必要的對應會在需要時載入和驗證。如果 mappings
包含字串或建構函式,則會從資料庫載入資料表(或對應資料表)的中繼資料,並根據對應的需求進行驗證。
多個資料表和建構函式可以做為陣列中的元素傳遞。
Array getOpenSessionFactories()
取得此模組所建立的所有 SessionFactory
物件的陣列。
以下函式是公用 API 的一部分,但不適用於應用程式使用。它們構成 Mynode
和 SessionFactory
之間的合約一部分。
Connection()
getConnectionKey()
getConnection()
newConnection()
deleteFactory()