I have an application that uses Java, mybatis, and SQL Anywhere. Because of some circumstances, I have to manually load in the SQLA driver jar at runtime. I got this working with this code:
public static URLClassLoader ucl;
public static Driver saDriver;
public static final void loadSqlAnywhere(SqlAnywhereVersions version)
{
if (saDriver == null)
{
try
{
File jarPath = "path\\to\\sajdbc4.jar";
URL jarUrl = new URL("jar:file:" + jarPath.getAbsolutePath() + "!/");
String driverClass = "sap.jdbc4.sqlanywhere.IDriver";
ucl = URLClassLoader.newInstance(new URL[]{ jarUrl });
saDriver = new DriverShim((Driver) Class.forName(driverClass, true, ucl).newInstance());
DriverManager.registerDriver(saDriver);
}
catch (Exception ex)
{
// Could not load SQL Anywhere driver
}
}
}
While the application is running, it's possible for SQL Anywhere to be upgraded. So, I need to completely unload the old driver, let the update happen, then init with the latest jar. I tried this:
private static List<SqlSession> sessions = new ArrayList<>();
private static SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder();
public static final void unloadSqlAnywhere()
{
if (saDriver != null)
{
// Close out any existing sessions
for (SqlSession session : sessions)
{
session.close();
}
sessions.clear();
// Kill the session factory
sessionFactory = null;
try
{
DriverManager.deregisterDriver(saDriver);
saDriver = null;
ucl.close();
ucl = null;
}
catch (Exception ex)
{
// Failed to unregister SQL Anywhere driver
}
}
}
But using jprofiler, I can see that DriverManager is still referencing sap.jdbc4.sqlanywhere.IDriver
, which is causing the SQL Anywhere upgrade to kill my application.
Is there something more I can do to ensure all driver references are removed? Or is there a better way to go about this?