我正在尝试将webapp部署到Tomcat容器,但出现异常,说明找不到数据库驱动程序。我在本地测试了Jetty上的应用程序,而驱动程序jar由Maven提供。我将driver- jar复制到Tomcat主文件夹中的lib文件夹中,但是仍然出现此异常:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver:<address of the database> at java.sql.DriverManager.getConnection(DriverManager.java:604) ~[?:1.7.0_21] at java.sql.DriverManager.getConnection(DriverManager.java:243) ~[?:1.7.0_21] at com.mypackage.ConnectionFactory.createConnection(ConnectionFactory.java:34) ~[ConnectionFactory.class:?] at com.mypackage.SimpleWebSession.authenticate(SimpleWebSession.java:63) [SimpleWebSession.class:?] at org.apache.wicket.authroles.authentication.AuthenticatedWebSession.signIn(AuthenticatedWebSession.java:65) [AuthenticatedWebSession.class:6.15.0] at com.mypackage.SimpleWebSession.signIn(SimpleWebSession.java:118) [SimpleWebSession.class:?] at com.mypackage.SignInPage$1.onSubmit(SignInPage.java:67) [SignInPage$1.class:?] at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1272) [Form.class:6.15.0] at org.apache.wicket.markup.html.form.Form.process(Form.java:938) [Form.class:6.15.0] at org.apache.wicket.markup.html.form.StatelessForm.process(StatelessForm.java:96) [StatelessForm.class:6.15.0] at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:770) [Form.class:6.15.0] at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:703) [Form.class:6.15.0] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_21] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_21] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_21] at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0_21] at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:258) [RequestListenerInterface.class:6.15.0] at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:216) [RequestListenerInterface.class:6.15.0] at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:243) [ListenerInterfaceRequestHandler.class:6.15.0] at org.apache.wicket.core.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:236) [ListenerInterfaceRequestHandler.class:6.15.0] at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:862) [RequestCycle$HandlerExecutor.class:6.15.0] at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64) [RequestHandlerStack.class:6.15.0] at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:261) [RequestCycle.class:6.15.0] at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:218) [RequestCycle.class:6.15.0] at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:289) [RequestCycle.class:6.15.0] at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:259) [WicketFilter.class:6.15.0] at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:201) [WicketFilter.class:6.15.0] at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:282) [WicketFilter.class:6.15.0] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.8] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.8] at org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66) [Log4jServletFilter.class:2.0-rc1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.8] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.8] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) [catalina.jar:8.0.8] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.8] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) [catalina.jar:8.0.8] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136) [catalina.jar:8.0.8] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:78) [catalina.jar:8.0.8] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) [catalina.jar:8.0.8] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.8] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526) [catalina.jar:8.0.8] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1033) [tomcat-coyote.jar:8.0.8] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652) [tomcat-coyote.jar:8.0.8] at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) [tomcat-coyote.jar:8.0.8] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566) [tomcat-coyote.jar:8.0.8] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523) [tomcat-coyote.jar:8.0.8] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_21] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_21] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.8] at java.lang.Thread.run(Thread.java:722) [?:1.7.0_21]
驱动程序也位于应用程序的lib文件夹中,这WEB-INF/lib是否可能导致异常?我正在使用Java 7和MSSQL JDBC驱动程序的版本4。
WEB-INF/lib
更新资料
如果我写
try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (final ClassNotFoundException e) { // Should not happen. e.printStackTrace(); }
之前,我已建立连接。为什么会这样呢?
我想我有原因。Tomcat试图防止由于某些特定的java / javax API而导致的内存泄漏,并且sql server驱动程序似乎就是其中之一。如果禁用该功能,它将自动加载驱动程序。要禁用,请如下修改server.xml JreMemoryLeakPreventionListener条目。
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />