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
,datasource
,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
Initialize Parallel Pool with ODBC Database Connection
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);
Initialize Parallel Pool with JDBC Database Connection
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
pool
— Parallel pool
parallel.Pool
object
Parallel pool, specified as a parallel.Pool
(Parallel Computing Toolbox) object.
datasource
— Data source name
character vector | string scalar
Data source name, specified as a character vector or string scalar. Specify the name of an existing data source.
Example: "myDataSource"
Data Types: char
| string
username
— User name
character vector | string scalar
User name required to access the database, specified as a character vector or string scalar.
If no user name is required, specify an empty value ""
.
Data Types: char
| string
password
— Password
character vector | string scalar
Password required to access the database, specified as a character vector or string scalar. If
no password is required, specify an empty value ""
.
Data Types: char
| string
Output Arguments
c
— parallel.pool.Constant
parallel.pool.Constant
object
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)