parfeval with background pool fails when using Java classes
I’m trying to either use an existing database connection, based on com.mysql.cj.jdbc.Driver or to get that created in a parfeval call using the background worker.
Minimal examples using all the workarounds with and without function handles, parallel.pool.Constant(), external functions, etc.
db = com.mysql.cj.jdbc.Driver
fun1 = @()db.acceptsURL("asdf")
fun2 = @(db)db.acceptsURL("asdf")
fun3 = @(c)c.Value.acceptsURL("asdf")
C1 = parallel.pool.Constant(db);
getDriver = @()com.mysql.cj.jdbc.Driver;
C2 = parallel.pool.Constant(@getDriver);
C3 = parallel.pool.Constant(@getDriverExt);
fun1() % no error, direct call, outputs false
fun2(db) % no error, direct call, outputs false
fetchOutputs(parfeval(backgroundPool, fun1, 1)) % error serializing
fetchOutputs(parfeval(backgroundPool, fun2, 1, db)) % error serializing
fetchOutputs(parfeval(backgroundPool, fun3, 1, C1)) % error serializing, The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
fetchOutputs(parfeval(backgroundPool, fun3, 1, C2)) % Unrecognized function or variable ‘getDriver’.
fetchOutputs(parfeval(backgroundPool, fun3, 1, C3)) % Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
getDriverExt.m
function conn = getDriverExt()
conn = com.mysql.cj.jdbc.Driver;
end
Here are the results of all runs:
>> fun1() % no error, direct call, outputs false
ans =
logical
0
>> fun2(db) % no error, direct call, outputs false
ans =
logical
0
>> fetchOutputs(parfeval(backgroundPool, fun1, 1)) % error serializing
Warning: com.mysql.cj.jdbc.Driver@3662c887 is not serializable
Error using parallel.BackgroundPool/parfeval
Internal error occurred when passing the input argument of type "function_handle" to the thread-based worker:
MATLAB:Serialize
Error during serialization
>> fetchOutputs(parfeval(backgroundPool, fun2, 1, db)) % error serializing
Warning: com.mysql.cj.jdbc.Driver@3662c887 is not serializable
Error using parallel.BackgroundPool/parfeval
Internal error occurred when passing the input argument of type "com.mysql.cj.jdbc.Driver" to the thread-based worker:
MATLAB:Serialize
Error during serialization
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C1)) % error serializing, The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 191)
The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C2)) % Unrecognized function or variable ‘getDriver’.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 196)
Error occurred while creating parallel.pool.Constant on the workers.
Unrecognized function or variable ‘getDriver’.
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C3)) % Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 196)
Error occurred while creating parallel.pool.Constant on the workers.
Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
Is it impossible to use Java classes in the workers? If no, how to do it properly?
Thanks!I’m trying to either use an existing database connection, based on com.mysql.cj.jdbc.Driver or to get that created in a parfeval call using the background worker.
Minimal examples using all the workarounds with and without function handles, parallel.pool.Constant(), external functions, etc.
db = com.mysql.cj.jdbc.Driver
fun1 = @()db.acceptsURL("asdf")
fun2 = @(db)db.acceptsURL("asdf")
fun3 = @(c)c.Value.acceptsURL("asdf")
C1 = parallel.pool.Constant(db);
getDriver = @()com.mysql.cj.jdbc.Driver;
C2 = parallel.pool.Constant(@getDriver);
C3 = parallel.pool.Constant(@getDriverExt);
fun1() % no error, direct call, outputs false
fun2(db) % no error, direct call, outputs false
fetchOutputs(parfeval(backgroundPool, fun1, 1)) % error serializing
fetchOutputs(parfeval(backgroundPool, fun2, 1, db)) % error serializing
fetchOutputs(parfeval(backgroundPool, fun3, 1, C1)) % error serializing, The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
fetchOutputs(parfeval(backgroundPool, fun3, 1, C2)) % Unrecognized function or variable ‘getDriver’.
fetchOutputs(parfeval(backgroundPool, fun3, 1, C3)) % Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
getDriverExt.m
function conn = getDriverExt()
conn = com.mysql.cj.jdbc.Driver;
end
Here are the results of all runs:
>> fun1() % no error, direct call, outputs false
ans =
logical
0
>> fun2(db) % no error, direct call, outputs false
ans =
logical
0
>> fetchOutputs(parfeval(backgroundPool, fun1, 1)) % error serializing
Warning: com.mysql.cj.jdbc.Driver@3662c887 is not serializable
Error using parallel.BackgroundPool/parfeval
Internal error occurred when passing the input argument of type "function_handle" to the thread-based worker:
MATLAB:Serialize
Error during serialization
>> fetchOutputs(parfeval(backgroundPool, fun2, 1, db)) % error serializing
Warning: com.mysql.cj.jdbc.Driver@3662c887 is not serializable
Error using parallel.BackgroundPool/parfeval
Internal error occurred when passing the input argument of type "com.mysql.cj.jdbc.Driver" to the thread-based worker:
MATLAB:Serialize
Error during serialization
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C1)) % error serializing, The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 191)
The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C2)) % Unrecognized function or variable ‘getDriver’.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 196)
Error occurred while creating parallel.pool.Constant on the workers.
Unrecognized function or variable ‘getDriver’.
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C3)) % Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 196)
Error occurred while creating parallel.pool.Constant on the workers.
Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
Is it impossible to use Java classes in the workers? If no, how to do it properly?
Thanks! I’m trying to either use an existing database connection, based on com.mysql.cj.jdbc.Driver or to get that created in a parfeval call using the background worker.
Minimal examples using all the workarounds with and without function handles, parallel.pool.Constant(), external functions, etc.
db = com.mysql.cj.jdbc.Driver
fun1 = @()db.acceptsURL("asdf")
fun2 = @(db)db.acceptsURL("asdf")
fun3 = @(c)c.Value.acceptsURL("asdf")
C1 = parallel.pool.Constant(db);
getDriver = @()com.mysql.cj.jdbc.Driver;
C2 = parallel.pool.Constant(@getDriver);
C3 = parallel.pool.Constant(@getDriverExt);
fun1() % no error, direct call, outputs false
fun2(db) % no error, direct call, outputs false
fetchOutputs(parfeval(backgroundPool, fun1, 1)) % error serializing
fetchOutputs(parfeval(backgroundPool, fun2, 1, db)) % error serializing
fetchOutputs(parfeval(backgroundPool, fun3, 1, C1)) % error serializing, The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
fetchOutputs(parfeval(backgroundPool, fun3, 1, C2)) % Unrecognized function or variable ‘getDriver’.
fetchOutputs(parfeval(backgroundPool, fun3, 1, C3)) % Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
getDriverExt.m
function conn = getDriverExt()
conn = com.mysql.cj.jdbc.Driver;
end
Here are the results of all runs:
>> fun1() % no error, direct call, outputs false
ans =
logical
0
>> fun2(db) % no error, direct call, outputs false
ans =
logical
0
>> fetchOutputs(parfeval(backgroundPool, fun1, 1)) % error serializing
Warning: com.mysql.cj.jdbc.Driver@3662c887 is not serializable
Error using parallel.BackgroundPool/parfeval
Internal error occurred when passing the input argument of type "function_handle" to the thread-based worker:
MATLAB:Serialize
Error during serialization
>> fetchOutputs(parfeval(backgroundPool, fun2, 1, db)) % error serializing
Warning: com.mysql.cj.jdbc.Driver@3662c887 is not serializable
Error using parallel.BackgroundPool/parfeval
Internal error occurred when passing the input argument of type "com.mysql.cj.jdbc.Driver" to the thread-based worker:
MATLAB:Serialize
Error during serialization
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C1)) % error serializing, The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 191)
The ‘Value’ field cannot be retrieved because the parallel.pool.Constant is invalid.
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C2)) % Unrecognized function or variable ‘getDriver’.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 196)
Error occurred while creating parallel.pool.Constant on the workers.
Unrecognized function or variable ‘getDriver’.
>> fetchOutputs(parfeval(backgroundPool, fun3, 1, C3)) % Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
Error using parallel.Future/fetchOutputs
One or more futures resulted in an error.
Caused by:
Error using parallel.pool.Constant/get.Value (line 196)
Error occurred while creating parallel.pool.Constant on the workers.
Unable to resolve the name ‘com.mysql.cj.jdbc.Driver’.
Is it impossible to use Java classes in the workers? If no, how to do it properly?
Thanks! java, matlab, parallel computing, parfeval, functions MATLAB Answers — New Questions