【JDBC 报错】Connections could not be acquired from the underlying database!

项目启动报错:

  

【JDBC  报错】Connections could not be acquired from the underlying database!【JDBC  报错】Connections could not be acquired from the underlying database!
 1 [2016-07-13 10:04:15,074] ERROR org.apache.ibatis.executor.BaseExecutor Could not get a databaseId from dataSource 
 2 java.sql.SQLException: Connections could not be acquired from the underlying database!
 3     at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:104)
 4     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:264)
 5     at com.mchange.v2.c3p0.PoolBackedDataSource.getConnection(PoolBackedDataSource.java:94)
 6     at com.mchange.v2.c3p0.ComboPooledDataSource.getConnection(ComboPooledDataSource.java:521)
 7     at org.apache.ibatis.mapping.VendorDatabaseIdProvider.getDatabaseProductName(VendorDatabaseIdProvider.java:77)
 8     at org.apache.ibatis.mapping.VendorDatabaseIdProvider.getDatabaseName(VendorDatabaseIdProvider.java:62)
 9     at org.apache.ibatis.mapping.VendorDatabaseIdProvider.getDatabaseId(VendorDatabaseIdProvider.java:50)
10     at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:445)
11     at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:336)
12     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
13     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
14     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
15     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
16     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
17     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
18     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
19     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
20     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
21     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
22     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
23     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
24     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
25     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
26     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210)
27     at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709)
28     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:802)
29     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
30     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:583)
31     at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1080)
32     at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1003)
33     at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:507)
34     at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1322)
35     at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:325)
36     at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
37     at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1068)
38     at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)
39     at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1060)
40     at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
41     at org.apache.catalina.core.StandardService.start(StandardService.java:525)
42     at org.apache.catalina.core.StandardServer.start(StandardServer.java:759)
43     at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
44     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
45     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
46     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
47     at java.lang.reflect.Method.invoke(Unknown Source)
48     at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
49     at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
50 Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
51     at com.mchange.v2.resourcepool.BasicResourcePool.awaitAcquire(BasicResourcePool.java:972)
52     at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:208)
53     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:260)
54     ... 45 more
View Code

 

解决办法:

1、驱动配置有误:driver=com.mysql.jdbc.Driver
2、数据库连接地址有误:url=jdbc:mysql://localhost:3306/test?3useUnicode=true&characterEncoding=utf8
3、密码或帐号有误:username=root
      password=root

4、数据库未启动或无权访问

5、项目未引入对应的驱动jar包mysql-connector-java-5.1.6-bin.jar

6、mysql root没有远程访问的权限,需要增加权限,增加权限的步骤如下:
 进入mysql数据库:
 grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
 flush privileges;

 

延伸:

1、在普通java项目中使用 mybatis+C3p0+spring时也遇见上述问题。

问题原因是:在xml配置文件中显示声明了自动装载  <beans default-autowire="byName">
解决方法:    把自动装载设置去掉,就没有问题了,应该使用默认的 byType

2、同一个ip在短时间内产生太多(超过mysql数据库max_connection_errors的最大值)中断的数据库连接而导致的阻塞,引发的。

解决方法:  重启数据库服务或者使用mysqladmin flush-hosts 命令清理一下hosts文件。

3、maven+spring 项目,用了org.apache.tomcat.jdbc.pool.DataSource;  报错:Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver

解决方法:  需要加入 tomcat-jdbc的引用

 

上一篇:Python基础之字符编码,文件操作流与函数


下一篇:多线程同步基础