How can I use sqlwrite with backgroundPool?

13 views (last 30 days)
Dave
Dave on 11 Sep 2025 at 20:07
Answered: Raymond Norris on 2 Oct 2025 at 16:11
I am receiving real-time data and saving the data to a MS SQL Server database using sqlwrite with no problem. I want to run this on the backgroundPool to ensure the main thread remains responsive to user input. I have Parallel Computing Toolbox and a JDBC connection object that reads and writes to the MS SQL Server database with no issue.
However, when I create a parallel.pool.Constant object and pass it to parfeval, the connection object fails the check for a valid connection (~isopen(conn)) that is found in both sqlwrite (line 56) and fetch (line 69).
conn = database( ... );
bgConn = parallel.pool.Constant(conn);
f = parfeval(backgroundPool,@(c)isopen(c.Value),1,bgConn);
disp(['Bg Pool // ConnIsOpen: ' num2str(fetchOutputs(f))]);
returns
Bg Pool // ConnIsOpen: 0
If I try a function handle, I get a different error message.
fhConn = @()database( ... );
bgFhConn = parallel.pool.Constant(fhConn);
f = parfeval(backgroundPool,@(c)isopen(c.Value),1,bgFhConn);
disp(['Bg Pool // ConnIsOpen: ' num2str(fetchOutputs(f))]);
returns
One or more futures resulted in an error.
Caused by:
Unable to create parallel.pool.Constant on the workers.
Unable to resolve the name 'com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager'.
How can I use parfeval to run sqlwrite? I see the same behaviour in both MATLAB 2024a and 2025a. Thanks for your help.

Answers (1)

Raymond Norris
Raymond Norris ongeveer 17 uur ago
@Dave rather than creating the connection on the client side and passing it to each worker, take a look at createConnectionForPool - Initialize parallel pool using database connection - MATLAB. createConnectionForPool creates a connection for each worker in the pool.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!