我的团队在$JAVA_HOME / jre / lib / ext /文件夹中有我们的应用程序的com. mysql.jdbc.Driver jar,但我正在尝试删除对JDK的这种依赖,以使用Gradle创建更可重复的构建. 我将驱动程序添加到我的build.
我将驱动程序添加到我的build.gradle中,您可以在我的依赖项顶部看到它:
dependencies { compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.+' compile group: 'com.notnoop.apns', name: 'apns', version: '1.0.0.Beta6' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21' compile group: 'com.google.code.gson', name: 'gson', version: '1.7.2' compile group: 'net.sf.jazzy', name: 'jazzy', version: '0.5.2-rtext-1.4.1' compile group: 'org.apache.lucene', name: 'com.springsource.org.apache.lucene.search.spell', version: '2.4.1' compile group: 'com.rabbitmq', name: 'com.springsource.com.rabbitmq.client', version: '1.8.1' compile group: 'com.sun.xml', name: 'com.springsource.com.sun.xml.bind', version: '2.2.0' compile group: 'com.sun.xml.rpc', name: 'jaxrpc-impl', version: '1.1.3_01' compile group: 'com.rabbitmq', name: 'amqp-client', version: '3.6.5' testCompile group: 'junit', name: 'junit', version: '4.11' compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.3.1' compile fileTree(dir: 'local_lib', include: '*.jar') }
从$JAVA_HOME / jre / lib / ext /中删除jar并将其放入gradle后,我在尝试访问数据库后在Catalina日志中收到此错误:
database.BaseDbao.getConnection - We could not create any connection. org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver' at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880) at database.BaseDbao.getConnection(BaseDbao.java:116) at database.UserDbao.getAuthorizedResultBean(UserDbao.java:357) at database.Dbao.getAuthorizedResultBean(Dbao.java:3635) at AuthServlet.doPost(AuthServlet.java:71) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at servlets.filters.servletContextFilter.doFilter(servletContextFilter.java:11) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130) ... 22 more Dbao.getAuthorizationResultBean() Error - Cannot load JDBC driver class 'com.mysql.jdbc.Driver' LoginFailed for :suburbantest:because ::10.0.0.22
任何见解都将受到高度赞赏!
您可以在Tomcat的配置中定义数据源,而不是将其放入应用程序配置中.Tomcat具有类加载器的层次结构.你可以在它的website上阅读它,但是为了简而言之,有三个有趣的类加载器:JDK类加载器(Bootstrap),Tomcat类加载器(系统)和你的应用程序的类加载器(事实上还有更多,但让它变得简单).
将驱动程序的jar放入$JAVA_HOME / jre / lib / ext /时,将其添加到JDK类加载器中.将它添加到tomcat / lib时,将其添加到Tomcat类加载器.当您在应用程序的gradle中定义它时,将其添加到应用程序类加载器.
由于您的案例中的数据源配置位于Tomcat的配置中,因此它将仅搜索JDK和Tomcat的类加载器.
一种解决方案是在您的应用程序中定义数据源配置(为什么在Tomcat中需要它?您是否有多次使用相同数据源的战争?)