JDBC driver becoming stale in JVM?

43 views (last 30 days)
Thomas
Thomas on 16 Oct 2025 at 14:43
Answered: Tridib about 14 hours ago
I connect to a database using:
JDBCDriverClass = 'org.teiid.jdbc.TeiidDriver';
JDBCDriverWildcard = 'teiid-*-jdbc.jar';
% Find latest JDBC binary
teiidfiles = dir(fullfile('path:)', JDBCDriverWildcard));
latest_driver = fullfile(teiidfiles(1).folder, teiidfiles(1).name);
% Only add to Java class path if not already present
if ~any(strcmpi(latest_driver, javaclasspath('-dynamic')))
javaaddpath(latest_driver);
end
conn = database('', Username, Password, JDBCDriverClass, sprintf(URLFormat, Version, Server, Port, AccessKey, options.timeout));
c = oncleanup(@() close(conn))
... rest of code
this works fine for ~8-24 hours, but after that I get this error:
Error using database (line 59)
One or more output arguments not assigned during call to "getDatabaseConnection".
I have tried to remove the driver from my java dynamic path, add it, and run it again, but that does not fix it, clear java, and clear classes also don't fix the issue.
the only solution I have found is restarting Matlab (which is not always handy since this is supposed to run on a VDI machine and query stuff daily)

Answers (1)

Tridib
Tridib 1 minute ago
The error you are seeing usually happens because the JDBC connection or driver stops working properly after MATLAB has been running for a long time. MATLAB does not fully reset its Java system unless you restart the whole program, so using commands like "clear java" or "clear classes" will not fix the problem.
To help prevent this, make sure you always close your database connections with "close(conn)" and also close any result sets or statements you have used, so you don’t run out of resources. Also, try not to add the same JDBC driver multiple times in one MATLAB session, since that can sometimes cause problems.
A good workaround is to schedule periodic MATLAB restarts or to run your code in a fresh MATLAB session each time. As a suggestion, you can even automate this process using scripts or scheduling tools (like Task Scheduler on Windows or "cron" on Linux) so MATLAB starts fresh and runs your code automatically at set times.
Hope this helps!

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!