%% Initialize Parallel Pool with JDBC Database Connection
%
% Using a JDBC database connection, access a database using a parallel pool. 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 configureJDBCDataSource function.
%
% Start the parallel pool.

pool = gcp;

%
% 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"];

%
% Use the parfor function to parallelize data access using the query basket.
% 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

%
% Close the parallel pool.

delete(pool);

% Copyright 2018 The MathWorks, Inc.