org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
问题:在连接数据库时,url,用户名,密码都正确,然后报错:
1 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!] with root cause 2 com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source. 3 at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319) 4 at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557) 5 at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477) 6 at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525) 7 at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128) 8 at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) 9 at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) 10 at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81) 11 at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67) 12 at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279) 13 at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72) 14 at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59) 15 at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267) 16 at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137) 17 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96) 18 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77) 19 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108) 20 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102) 21 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 22 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 23 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 24 at java.lang.reflect.Method.invoke(Method.java:601) 25 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358) 26 at com.sun.proxy.$Proxy9.selectList(Unknown Source) 27 at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198) 28 at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119) 29 at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63) 30 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) 31 at com.sun.proxy.$Proxy10.findAllItems(Unknown Source) 32 at com.gyf.backoffice.service.impl.ItemsServiceImpl.findAllItems(ItemsServiceImpl.java:18) 33 at com.gyf.backoffice.web.controller.ItemsController.list(ItemsController.java:19) 34 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 35 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 36 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 37 at java.lang.reflect.Method.invoke(Method.java:601) 38 at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 39 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 40 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 41 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) 42 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) 43 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 44 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 45 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 46 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:917) 47 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:813) 48 at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) 49 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:798) 50 at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 51 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 52 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 53 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 54 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 55 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 56 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 57 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 58 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543) 59 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) 60 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) 61 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688) 62 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 63 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) 64 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615) 65 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) 66 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818) 67 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623) 68 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 69 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 70 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 71 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 72 at java.lang.Thread.run(Thread.java:722) 73 31-Mar-2020 16:19:59.986 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log Destroying Spring FrameworkServlet 'DispatcherServlet' 74 31-Mar-2020 16:19:59.989 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext 75 31-Mar-2020 16:20:00.040 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log SessionListener: contextDestroyed() 76 31-Mar-2020 16:20:00.041 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log ContextListener: contextDestroyed()
原因分析:
这个是导入的数据库连接包的问题,如果你导的包版本太低或者不符合,比如我之前用的是5.1.5,就会出现相关的错误
解决方法:
换成最新的包,就不会出现错误了:https://dev.mysql.com/downloads/connector/j/5.1.html
在解决这个问题的时候,在博客上看到了另外一种比较常见的错误的情况:
https://blog.csdn.net/liwuyang2013/article/details/25986597
这种情况是如果将spring配置文件的命名空间都改到了4.0版本之后,需要在EL表达式中加上jdbc,如下:
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/ssmbs 3 jdbc.username=Emove 4 jdbc.password=123456
1 <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"> 2 <property name="driverClassName" value="${jdbc.driver}"></property> 3 <property name="username" value="${jdbc.username}"></property> 4 <property name="password" value="${jdbc.password}"></property> 5 <property name="url" value="${jdbc.url}"></property> 6 </bean>
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/ssmbs 3 jdbc.username=Emove 4 jdbc.password=