问题描述
我有 2 个需要使用 Windows 集成身份验证连接到 SQL Server 数据库的 Java Web 应用程序.
I have 2 java web apps that need to connect to SQL Server Database using Windows Integrated Authentication.
加载的第一个工作正常,但第二个抛出异常:
The first one that is loaded works fine but the second one throws the exception:
Native Library sqljdbc_auth.dll already loaded in another classloader
当我将 sqljdbc_auth.dll 放在其中一个文件夹中时会发生上述错误:
The error above occurs when I place the sqljdbc_auth.dll in one of the folders:
- C:WINDOWSsystem32
- C:Program FilesApache Software FoundationTomcat 7.0in
如果我将 sqljdbc_auth.dll 放在以下文件夹之一中:
If I place the sqljdbc_auth.dll in one of the folders below:
- /WEB-INF/lib 每个web应用的目录
- C:Program FilesApache Software FoundationTomcat 7.0lib
两个应用都抛出异常:
Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path
我正在使用此代码加载驱动程序:
I am using this code to load the driver:
Class.forName("jdbc:sqlserver://<HOST>;databaseName=<DBNAME>;integratedSecurity=true;");
我该如何解决?
推荐答案
每个 Web 应用程序都有自己的类加载器(将它们隔离).当您调用 Class.forName() 方法时,有一个静态块正在尝试加载共享库(dll 文件)-因此您的两个 Web 应用程序都在尝试加载共享库,因此在第二个应用程序时出现错误消息尝试加载.
Each web application has its own Classloader (isolating them). When you call the Class.forName() method, there is a static block which is trying to load the shared library (dll file) - so both your web apps are trying to load the shared lib, hence the error message when the second one attempts to load.
你的 sqlserver 的 JDBC jar 应该从与你的 wars 捆绑在一起移动到 tomcat 7.0/lib
文件夹并将 sqljdbc_auth.dll 复制到 tomcat/bin 文件夹 - 这样它就会在tomcat父类加载器中,该类只会加载一次.
The JDBC jar you have for sqlserver should be moved from being bundled with your wars, to the tomcat 7.0/lib
folder and copy the sqljdbc_auth.dll to tomcat/bin folder - this way it will be in the tomcat parent classloader, and the class will only be loaded once.
|----------------------------------|
| sqljdbc*.jar --> tomcat*/lib |
|----------------------------------|
| sqljdbc_auth.dll --> tomcat*/bin |
|----------------------------------|
这篇关于本机库 sqljdbc_auth.dll 已加载到另一个类加载器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!