createConnectionForPool
Initialize parallel pool using database connection
Description
initializes a parallel pool by creating a database connection on each worker in the pool
using a data source, user name, and password.c
= createConnectionForPool(pool
,DataSourceName
,username
,password
)
Note
If you use an ODBC data source for the database connection, each worker in the pool must have the required ODBC driver installed and a configured ODBC data source. Otherwise, an error occurs when you import data from the database.
Examples
Using an ODBC database connection, access a database using a parallel pool (requires Parallel Computing Toolbox™). Import data from multiple SQL queries in parallel.
Each worker in the pool has the ODBC driver installed. Also, each worker has a configured ODBC data source. For more information, see the configureODBCDataSource
function.
Start the parallel pool.
pool = gcp;
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).
Initialize the parallel pool using an ODBC data source. This data source configures an ODBC driver to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.
datasource = "MS SQL Server Auth"; username = ""; password = ""; c = createConnectionForPool(pool,datasource,username,password);
Define the SQL queries.
sqlqueries = ["SELECT * FROM invoice" ... "SELECT * FROM inventorytable" ... "SELECT * FROM producttable"];
Parallelize data access using the query basket by calling the parfor
function.
For each worker, retrieve the database connection object, execute the SQL queries, and import data locally.
parfor i = 1:length(sqlqueries) conn = c.Value; results = fetch(conn,sqlqueries(i)); allresults{i} = results; end
Display the results for all queries. The cell array contains three tables, one for each set of query results.
allresults
allresults = 1×3 cell array
{10×5 table} {13×4 table} {15×5 table}
Close the parallel pool.
delete(pool);
Using a JDBC database connection, access a database using a parallel pool (requires Parallel Computing Toolbox™). Import data from multiple SQL queries in parallel.
To initialize a parallel pool with a JDBC database connection, you must configure a JDBC data source. For more information, see the databaseConnectionOptions
function.
Start the parallel pool.
pool = gcp;
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).
Initialize the parallel pool using the JDBC data source MSSQLServerJDBCAuth
. This data source configures a JDBC driver to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.
datasource = "MSSQLServerJDBCAuth"; username = ""; password = ""; c = createConnectionForPool(pool,datasource,username,password);
Define the SQL queries.
sqlqueries = ["SELECT * FROM invoice" ... "SELECT * FROM inventorytable" ... "SELECT * FROM producttable"];
Parallelize data access using the query basket by calling the parfor
function.
For each worker, retrieve the database connection object, execute the SQL queries, and import data locally.
parfor i = 1:length(sqlqueries) conn = c.Value; results = fetch(conn,sqlqueries(i)); allresults{i} = results; end
Display the results for all queries. The cell array contains three tables, one for each set of query results.
allresults
allresults = 1×3 cell array
{10×5 table} {13×4 table} {15×5 table}
Close the parallel pool.
delete(pool);
Input Arguments
Parallel pool, specified as a parallel.Pool
(Parallel Computing Toolbox) object.
Data source name, specified as a character vector or string scalar. Database Explorer creates this name when you use the app to make a connection and store your credentials as part of the data source.
Username required to access the database, specified as a character
vector or string scalar. If no username is required, specify an
empty value ""
. Provide this username if you
do not store it as part of the data source when using the
Database Explorer app to make a connection.
Password required to access the database, specified as a character vector or string scalar. If
no password is required, specify an empty value
""
. Provide this password if you
do not store it as part of the data source when using the
Database Explorer app to make a connection.
Data Types: char
| string
Output Arguments
parallel.pool.Constant
, specified as a parallel.pool.Constant
(Parallel Computing Toolbox) object. The Value property of the
parallel.pool.Constant
object is available only on workers.
Version History
Introduced in R2019a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)