问题描述
我有两个需要使用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:\ WINDOWS \system32 \
- C:\Program Files \ Amaache Software Foundation \ Tomcat 7.0 \\\\
如果我将sqljdbc_auth.dll放在以下某个文件夹中:
If I place the sqljdbc_auth.dll in one of the folders below:
- $ b每个Web应用程序的$ b
- / WEB-INF / lib目录
- C:\Program Files \ ApachePache Foundation \ Tomcat 7.0 \lib \
- /WEB-INF/lib directory of each web application
- C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\
两个应用程序都抛出异常:
Both apps throw the exception:
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应用程序都有自己的Classloader(隔离它们)。当您调用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应该从你的战争捆绑到 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已经加载到另一个类加载器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!