3

I have multiple applications that needs to connect to MSSQL using windows authentication.

The First webApp which is loaded works fine. but the remaining fail prompting

Caused by: java.lang.UnsatisfiedLinkError: Native Library $tomcat/bin/sqljdbc_auth.dll already loaded in another classloader

Below is the code used

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection= DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=XXX;integratedSecurity=true");

I Have placed sqljdbc*.jar --> tomcat*/lib and sqljdbc_auth.dll --> tomcat*/bin

Seems like all my applications are trying to load the shared lib($tomcat/bin/dll) multiple times. Hence the first load works and the remaining fail.

Edit: I understand that the native library (DLL) can only be loaded into the JVM once, hence the error, but I after looking around the net I still have no solution.

How can i load the dll only once?

Please Help!!

divya krishna
  • 101
  • 1
  • 1
  • 4

1 Answers1

0

I would guess. That removing the line

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

would solve the problem.

As mentioned in the javadoc of DriverManager

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism.
...
Applications no longer need to explictly load JDBC drivers using Class.forName().

edit This requires a jdbc driver which supports the JDBC 4.0 API. Which should be the case from Microsoft JDBC driver 4.0 on (see: https://learn.microsoft.com/en-us/sql/connect/jdbc/system-requirements-for-the-jdbc-driver)

A matrix which Microsoft JDBC driver supports which SQL server version you can find at https://learn.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server-support-matrix

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • That throws an exception java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=xxx;integratedSecurity=true – divya krishna Jan 10 '18 at 09:46
  • @divyakrishna This would mean the driver you are using does not support the JDBC 4.0 API. You could update the driver. I updated my answer with some additional information. – SubOptimal Jan 10 '18 at 10:11
  • In my case the driver version and jar matches perfectly. When loading tomcat with only a single web application (anyone), the web application successfully connect to the DB. That's the reason i don't think its about versioning. – divya krishna Jan 10 '18 at 10:26
  • @divyakrishna Could you have a look in the `sqljdbc*.jar` if it contains the file `META-INF/services/java.sql.Driver`. If not, your used driver doesn't support the JDBC 4.0 API (see the javadoc of DriverManager). Btw. Which Microsoft JDBC driver version are you using? – SubOptimal Jan 10 '18 at 12:49
  • Issue is resolved. Thanks. I have removed the jar from WEB-INF/lib in all my application. And placed only in tomcat for sharing the native lib. Its working now. – divya krishna Jan 10 '18 at 15:19